Modularize calculator into separate source files
- Split 3470-line iframe.html into manageable components - Created src/ directory with styles.css, template.html, calculator.js - Updated build.js to assemble from modular source files - Maintains identical functionality with improved maintainability - Single source of truth: edit in src/, run build.js to generate both outputs
This commit is contained in:
parent
976e7d9136
commit
ba3e0a6a6a
152
build.js
152
build.js
@ -1,46 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Dog Calculator Build System - PRODUCTION VERSION
|
||||
* Dog Calculator Build System - MODULAR VERSION
|
||||
*
|
||||
* This FIXED build script generates iframe.html and dog-calculator-widget.js
|
||||
* with EXACTLY the same functionality from iframe.html as the single source of truth.
|
||||
* This build script generates iframe.html and sundog-dog-food-calculator.js
|
||||
* from modular source files in the src/ directory.
|
||||
*
|
||||
* Source files:
|
||||
* - src/styles.css - All CSS styles
|
||||
* - src/template.html - HTML structure
|
||||
* - src/calculator.js - JavaScript functionality
|
||||
*
|
||||
* Output files:
|
||||
* - iframe.html - Standalone calculator page
|
||||
* - sundog-dog-food-calculator.js - Embeddable widget
|
||||
*
|
||||
* Usage: node build.js
|
||||
*
|
||||
* ✅ WORKS CORRECTLY - Fixed the previous broken implementation
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
console.log('🎯 Dog Calculator Build System - FIXED & WORKING');
|
||||
console.log('🎯 Dog Calculator Build System - MODULAR VERSION');
|
||||
console.log('');
|
||||
|
||||
/**
|
||||
* Extract and parse components from the master iframe.html
|
||||
* Read modular components from src directory
|
||||
*/
|
||||
function parseIframeComponents() {
|
||||
if (!fs.existsSync('iframe.html')) {
|
||||
throw new Error('iframe.html not found - this is the master file that should exist');
|
||||
function readSourceComponents() {
|
||||
const srcDir = 'src';
|
||||
|
||||
// Check if src directory exists
|
||||
if (!fs.existsSync(srcDir)) {
|
||||
throw new Error('src directory not found - please ensure modular files exist');
|
||||
}
|
||||
|
||||
const content = fs.readFileSync('iframe.html', 'utf8');
|
||||
// Read CSS
|
||||
const cssPath = path.join(srcDir, 'styles.css');
|
||||
if (!fs.existsSync(cssPath)) {
|
||||
throw new Error('src/styles.css not found');
|
||||
}
|
||||
const css = fs.readFileSync(cssPath, 'utf8').trim();
|
||||
|
||||
// Extract CSS (everything between <style> and </style>)
|
||||
const cssMatch = content.match(/<style>([\s\S]*?)<\/style>/);
|
||||
if (!cssMatch) throw new Error('Could not extract CSS from iframe.html');
|
||||
const css = cssMatch[1].trim();
|
||||
// Read HTML template
|
||||
const htmlPath = path.join(srcDir, 'template.html');
|
||||
if (!fs.existsSync(htmlPath)) {
|
||||
throw new Error('src/template.html not found');
|
||||
}
|
||||
const html = fs.readFileSync(htmlPath, 'utf8').trim();
|
||||
|
||||
// Extract HTML body (everything between <body> and <script>)
|
||||
const htmlMatch = content.match(/<body>([\s\S]*?)<script>/);
|
||||
if (!htmlMatch) throw new Error('Could not extract HTML from iframe.html');
|
||||
const html = htmlMatch[1].trim();
|
||||
|
||||
// Extract JavaScript (everything between <script> and </script>)
|
||||
const jsMatch = content.match(/<script>([\s\S]*?)<\/script>/);
|
||||
if (!jsMatch) throw new Error('Could not extract JavaScript from iframe.html');
|
||||
const js = jsMatch[1].trim();
|
||||
// Read JavaScript
|
||||
const jsPath = path.join(srcDir, 'calculator.js');
|
||||
if (!fs.existsSync(jsPath)) {
|
||||
throw new Error('src/calculator.js not found');
|
||||
}
|
||||
const js = fs.readFileSync(jsPath, 'utf8').trim();
|
||||
|
||||
return { css, html, js };
|
||||
}
|
||||
@ -50,16 +64,19 @@ function parseIframeComponents() {
|
||||
*/
|
||||
function backupFiles() {
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const filesToBackup = ['iframe.html', 'sundog-dog-food-calculator.js'];
|
||||
|
||||
if (fs.existsSync('sundog-dog-food-calculator.js')) {
|
||||
const backupName = `sundog-dog-food-calculator.js.backup-${timestamp}`;
|
||||
fs.copyFileSync('sundog-dog-food-calculator.js', backupName);
|
||||
console.log(`📦 Backed up existing widget to: ${backupName}`);
|
||||
}
|
||||
filesToBackup.forEach(file => {
|
||||
if (fs.existsSync(file)) {
|
||||
const backupName = `${file}.backup-${timestamp}`;
|
||||
fs.copyFileSync(file, backupName);
|
||||
console.log(`📦 Backed up ${file} to: ${backupName}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate iframe.html (regenerate from components for consistency)
|
||||
* Generate iframe.html from modular components
|
||||
*/
|
||||
function generateIframe(css, html, js) {
|
||||
const content = `<!DOCTYPE html>
|
||||
@ -69,6 +86,9 @@ function generateIframe(css, html, js) {
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Dog Calorie Calculator - Canine Nutrition and Wellness</title>
|
||||
<style>
|
||||
/* Sundog Dog Food Calorie Calculator Styles */
|
||||
|
||||
/* CSS Variables for theming */
|
||||
${css}
|
||||
</style>
|
||||
</head>
|
||||
@ -80,30 +100,14 @@ function generateIframe(css, html, js) {
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
// Since iframe.html is our source, we don't overwrite it
|
||||
// This function is here for consistency and testing
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* No transformation needed - keep original class names for consistency
|
||||
*/
|
||||
function transformCSSForWidget(css) {
|
||||
return css;
|
||||
}
|
||||
|
||||
/**
|
||||
* No transformation needed - keep original class names for consistency
|
||||
*/
|
||||
function transformHTMLForWidget(html) {
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create production-ready widget JavaScript that uses the ACTUAL extracted JS from iframe.html
|
||||
* Create production-ready widget JavaScript
|
||||
*/
|
||||
function createWidgetJS(css, html, js) {
|
||||
// Transform the extracted JavaScript from iframe.html to work as a widget
|
||||
// Transform the JavaScript from calculator.js to work as a widget
|
||||
|
||||
// Replace the iframe's DOMContentLoaded listener with widget initialization
|
||||
let transformedJS = js
|
||||
@ -175,12 +179,18 @@ function createWidgetJS(css, html, js) {
|
||||
}
|
||||
}$2`);
|
||||
|
||||
// Add CSS variables to widget CSS
|
||||
const widgetCSS = `/* Sundog Dog Food Calorie Calculator Styles */
|
||||
|
||||
/* CSS Variables for theming */
|
||||
${css}`;
|
||||
|
||||
const widgetCode = `/**
|
||||
* Dog Calorie Calculator Widget
|
||||
* Embeddable JavaScript widget for websites
|
||||
*
|
||||
* THIS CODE IS AUTO-GENERATED FROM iframe.html - DO NOT EDIT MANUALLY
|
||||
* Edit iframe.html and run 'node build.js' to update this file
|
||||
* THIS CODE IS AUTO-GENERATED FROM src/ FILES - DO NOT EDIT MANUALLY
|
||||
* Edit files in src/ directory and run 'node build.js' to update
|
||||
*
|
||||
* Usage:
|
||||
* <script src="sundog-dog-food-calculator.js"></script>
|
||||
@ -197,7 +207,7 @@ function createWidgetJS(css, html, js) {
|
||||
'use strict';
|
||||
|
||||
// Inject widget styles
|
||||
const CSS_STYLES = \`${css}\`;
|
||||
const CSS_STYLES = \`${widgetCSS}\`;
|
||||
|
||||
function injectStyles() {
|
||||
if (document.getElementById('dog-calculator-styles')) return;
|
||||
@ -208,7 +218,7 @@ function createWidgetJS(css, html, js) {
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
// ACTUAL JavaScript from iframe.html (transformed for widget use)
|
||||
// JavaScript from src/calculator.js (transformed for widget use)
|
||||
${transformedJS}
|
||||
|
||||
// Auto-initialize widgets on page load
|
||||
@ -249,37 +259,57 @@ function createWidgetJS(css, html, js) {
|
||||
*/
|
||||
function build() {
|
||||
try {
|
||||
console.log('📖 Reading components from iframe.html (master source)...');
|
||||
const { css, html, js } = parseIframeComponents();
|
||||
console.log('📖 Reading modular components from src/ directory...');
|
||||
const { css, html, js } = readSourceComponents();
|
||||
console.log(' ✅ src/styles.css (' + css.split('\n').length + ' lines)');
|
||||
console.log(' ✅ src/template.html (' + html.split('\n').length + ' lines)');
|
||||
console.log(' ✅ src/calculator.js (' + js.split('\n').length + ' lines)');
|
||||
|
||||
console.log('');
|
||||
console.log('📦 Backing up existing files...');
|
||||
backupFiles();
|
||||
|
||||
console.log('🏗️ Generating sundog-dog-food-calculator.js...');
|
||||
console.log('');
|
||||
console.log('🏗️ Building output files...');
|
||||
|
||||
// Generate iframe.html
|
||||
const iframeContent = generateIframe(css, html, js);
|
||||
fs.writeFileSync('iframe.html', iframeContent);
|
||||
console.log(' ✅ Generated iframe.html');
|
||||
|
||||
// Generate widget
|
||||
const widgetCode = createWidgetJS(css, html, js);
|
||||
fs.writeFileSync('sundog-dog-food-calculator.js', widgetCode);
|
||||
console.log('✅ Generated sundog-dog-food-calculator.js');
|
||||
console.log(' ✅ Generated sundog-dog-food-calculator.js');
|
||||
|
||||
console.log('');
|
||||
console.log('🎉 Build completed successfully!');
|
||||
console.log('');
|
||||
console.log('📋 Summary:');
|
||||
console.log(' ✅ iframe.html - Master source (no changes needed)');
|
||||
console.log(' ✅ sundog-dog-food-calculator.js - Generated with identical functionality');
|
||||
console.log(' Source files (easy to edit):');
|
||||
console.log(' • src/styles.css - All styles and theming');
|
||||
console.log(' • src/template.html - HTML structure');
|
||||
console.log(' • src/calculator.js - JavaScript logic');
|
||||
console.log('');
|
||||
console.log(' Generated files:');
|
||||
console.log(' • iframe.html - Standalone calculator');
|
||||
console.log(' • sundog-dog-food-calculator.js - Embeddable widget');
|
||||
console.log('');
|
||||
console.log('🔄 Your new workflow:');
|
||||
console.log(' 1. Edit iframe.html for any changes (calculations, design, layout)');
|
||||
console.log(' 1. Edit files in src/ directory');
|
||||
console.log(' 2. Run: node build.js');
|
||||
console.log(' 3. Both files now have the same functionality!');
|
||||
console.log(' 3. Both output files are regenerated!');
|
||||
console.log('');
|
||||
console.log('💡 No more maintaining two separate files!');
|
||||
console.log('💡 Now you can easily read and edit each part separately!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Build failed:', error.message);
|
||||
console.error(error.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run build if called directly
|
||||
if (require.main === module) {
|
||||
build();
|
||||
}
|
||||
|
||||
@ -2022,9 +2022,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<script>
|
||||
/**
|
||||
* Dog Calorie Calculator - iframe version
|
||||
|
||||
1440
src/calculator.js
Normal file
1440
src/calculator.js
Normal file
File diff suppressed because it is too large
Load Diff
1822
src/styles.css
Normal file
1822
src/styles.css
Normal file
File diff suppressed because it is too large
Load Diff
189
src/template.html
Normal file
189
src/template.html
Normal file
@ -0,0 +1,189 @@
|
||||
<div class="dog-calculator-container" id="dogCalculator">
|
||||
<div class="dog-calculator-section">
|
||||
<div class="dog-calculator-section-header">
|
||||
<h2>Dog's Characteristics</h2>
|
||||
<div class="dog-calculator-unit-switch">
|
||||
<span class="dog-calculator-unit-label active" id="metricLabel">Metric</span>
|
||||
<label class="dog-calculator-switch">
|
||||
<input type="checkbox" id="unitToggle">
|
||||
<span class="dog-calculator-slider"></span>
|
||||
</label>
|
||||
<span class="dog-calculator-unit-label" id="imperialLabel">Imperial</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-form-group">
|
||||
<label for="dogType">Dog Type / Activity Level:</label>
|
||||
<select id="dogType" aria-describedby="dogTypeHelp">
|
||||
<option value="">Select dog type...</option>
|
||||
<option value="3.0">Puppy (0-4 months)</option>
|
||||
<option value="2.0">Puppy (4 months - adult)</option>
|
||||
<option value="1.2">Adult - inactive/obese</option>
|
||||
<option value="1.6">Adult (neutered/spayed) - average activity</option>
|
||||
<option value="1.8">Adult (intact) - average activity</option>
|
||||
<option value="1.0">Adult - weight loss</option>
|
||||
<option value="1.7">Adult - weight gain</option>
|
||||
<option value="2.0">Working dog - light work</option>
|
||||
<option value="3.0">Working dog - moderate work</option>
|
||||
<option value="5.0">Working dog - heavy work</option>
|
||||
<option value="1.1">Senior dog</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-form-group">
|
||||
<label for="weight" id="weightLabel">Dog's Weight (kg):</label>
|
||||
<input type="number" id="weight" min="0.1" step="0.1" placeholder="Enter weight in kg" aria-describedby="weightHelp">
|
||||
<div id="weightError" class="dog-calculator-error dog-calculator-hidden">Please enter a valid weight (minimum 0.1 kg)</div>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-results" id="calorieResults" style="display: none;">
|
||||
<div class="dog-calculator-result-item">
|
||||
<span class="dog-calculator-result-label">Resting Energy Requirement (RER):</span>
|
||||
<span class="dog-calculator-result-value" id="rerValue">- cal/day</span>
|
||||
</div>
|
||||
<div class="dog-calculator-result-item">
|
||||
<span class="dog-calculator-result-label">Maintenance Energy Requirement (MER):</span>
|
||||
<span class="dog-calculator-result-value" id="merValue">- cal/day</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-collapsible active" id="foodCalculator">
|
||||
<div class="dog-calculator-collapsible-header">
|
||||
<h3>How much should I feed?</h3>
|
||||
</div>
|
||||
<div class="dog-calculator-collapsible-content">
|
||||
<div class="dog-calculator-collapsible-inner">
|
||||
<!-- Food Sources Container -->
|
||||
<div class="dog-calculator-food-sources" id="foodSources">
|
||||
<!-- Initial food source will be added by JavaScript -->
|
||||
</div>
|
||||
|
||||
<!-- Add Food Source Button -->
|
||||
<button class="dog-calculator-add-food-btn" id="addFoodBtn" type="button">
|
||||
<span>+</span>
|
||||
<span>Add another food source</span>
|
||||
</button>
|
||||
|
||||
<!-- Per-Food Results -->
|
||||
<div class="dog-calculator-food-results" id="foodBreakdownResults" style="display: none;">
|
||||
<div id="foodBreakdownList">
|
||||
<!-- Individual food breakdowns will be populated by JavaScript -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Unit Selection Buttons -->
|
||||
<div class="dog-calculator-unit-buttons" id="unitButtons" style="display: none;">
|
||||
<button type="button" class="dog-calculator-unit-btn active" data-unit="g">g</button>
|
||||
<button type="button" class="dog-calculator-unit-btn" data-unit="kg">kg</button>
|
||||
<button type="button" class="dog-calculator-unit-btn" data-unit="oz">oz</button>
|
||||
<button type="button" class="dog-calculator-unit-btn" data-unit="lb">lb</button>
|
||||
</div>
|
||||
|
||||
<!-- Daily Total Results -->
|
||||
<div class="dog-calculator-results" id="dailyFoodResults" style="display: none;">
|
||||
<div class="dog-calculator-result-item">
|
||||
<span class="dog-calculator-result-label">Total Daily Amount:</span>
|
||||
<span class="dog-calculator-result-value" id="dailyFoodValue">- g/day</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Hidden select for compatibility -->
|
||||
<select id="unit" class="dog-calculator-unit-select-hidden" aria-describedby="unitHelp">
|
||||
<option value="g">grams (g)</option>
|
||||
<option value="kg">kilograms (kg)</option>
|
||||
<option value="oz">ounces (oz)</option>
|
||||
<option value="lb">pounds (lb)</option>
|
||||
</select>
|
||||
|
||||
<div class="dog-calculator-food-amounts-section" id="foodAmountsSection" style="display: none;">
|
||||
<h4 class="dog-calculator-section-title">
|
||||
Calculate amounts for
|
||||
<input type="number" id="days" min="1" step="1" value="1" placeholder="1" aria-describedby="daysHelp" class="dog-calculator-inline-days">
|
||||
<span id="dayLabel">day</span>:
|
||||
</h4>
|
||||
<div id="daysError" class="dog-calculator-error dog-calculator-hidden">Please enter a valid number of days (minimum 1)</div>
|
||||
<div id="foodAmountsList" class="dog-calculator-food-amounts-list">
|
||||
<!-- Individual food amounts will be populated here -->
|
||||
</div>
|
||||
<div class="dog-calculator-total-row" id="totalAmountRow">
|
||||
<span class="dog-calculator-total-label">Total Amount:</span>
|
||||
<span class="dog-calculator-total-value" id="totalAmountDisplay"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-action-buttons">
|
||||
<button class="dog-calculator-btn dog-calculator-btn-share" id="shareBtn">
|
||||
Share
|
||||
</button>
|
||||
<button class="dog-calculator-btn dog-calculator-btn-embed" id="embedBtn">
|
||||
Embed
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-footer">
|
||||
<a href="https://caninenutritionandwellness.com" target="_blank" rel="noopener noreferrer">
|
||||
by caninenutritionandwellness.com
|
||||
</a>
|
||||
</div>
|
||||
<!-- Share Modal -->
|
||||
<div id="shareModal" class="dog-calculator-modal" style="display: none;">
|
||||
<div class="dog-calculator-modal-content">
|
||||
<span class="dog-calculator-modal-close" id="shareModalClose">×</span>
|
||||
<h3>Share Calculator</h3>
|
||||
<div class="dog-calculator-share-buttons">
|
||||
<button class="dog-calculator-share-btn dog-calculator-share-facebook plausible-event-name=Calculator+Usage plausible-event-action=calculator-share-facebook" id="shareFacebook">
|
||||
Facebook
|
||||
</button>
|
||||
<button class="dog-calculator-share-btn dog-calculator-share-twitter plausible-event-name=Calculator+Usage plausible-event-action=calculator-share-twitter" id="shareTwitter">
|
||||
Twitter
|
||||
</button>
|
||||
<button class="dog-calculator-share-btn dog-calculator-share-linkedin plausible-event-name=Calculator+Usage plausible-event-action=calculator-share-linkedin" id="shareLinkedIn">
|
||||
LinkedIn
|
||||
</button>
|
||||
<button class="dog-calculator-share-btn dog-calculator-share-email plausible-event-name=Calculator+Usage plausible-event-action=calculator-share-email" id="shareEmail">
|
||||
Email
|
||||
</button>
|
||||
<button class="dog-calculator-share-btn dog-calculator-share-copy plausible-event-name=Calculator+Usage plausible-event-action=calculator-share-copy-link" id="shareCopy">
|
||||
Copy Link
|
||||
</button>
|
||||
</div>
|
||||
<div class="dog-calculator-share-url">
|
||||
<input type="text" id="shareUrl" readonly>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Embed Modal -->
|
||||
<div id="embedModal" class="dog-calculator-modal" style="display: none;">
|
||||
<div class="dog-calculator-modal-content dog-calculator-modal-embed">
|
||||
<span class="dog-calculator-modal-close" id="embedModalClose">×</span>
|
||||
<h3>⚡ Embed the Calculator</h3>
|
||||
|
||||
<div class="dog-calculator-embed-options">
|
||||
<div class="dog-calculator-embed-option">
|
||||
<h4>⚡ JavaScript Widget</h4>
|
||||
<div class="dog-calculator-code-container">
|
||||
<pre><code id="widgetCode"></code></pre>
|
||||
<button class="dog-calculator-copy-btn plausible-event-name=Calculator+Usage plausible-event-action=calculator-embed-js" id="copyWidget">
|
||||
Copy
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="dog-calculator-embed-option">
|
||||
<h4>🛡️ iframe Embed</h4>
|
||||
<div class="dog-calculator-code-container">
|
||||
<pre><code id="iframeCode"></code></pre>
|
||||
<button class="dog-calculator-copy-btn plausible-event-name=Calculator+Usage plausible-event-action=calculator-embed-iframe" id="copyIframe">
|
||||
Copy
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -2,8 +2,8 @@
|
||||
* Dog Calorie Calculator Widget
|
||||
* Embeddable JavaScript widget for websites
|
||||
*
|
||||
* THIS CODE IS AUTO-GENERATED FROM iframe.html - DO NOT EDIT MANUALLY
|
||||
* Edit iframe.html and run 'node build.js' to update this file
|
||||
* THIS CODE IS AUTO-GENERATED FROM src/ FILES - DO NOT EDIT MANUALLY
|
||||
* Edit files in src/ directory and run 'node build.js' to update
|
||||
*
|
||||
* Usage:
|
||||
* <script src="sundog-dog-food-calculator.js"></script>
|
||||
@ -1855,7 +1855,7 @@
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
// ACTUAL JavaScript from iframe.html (transformed for widget use)
|
||||
// JavaScript from src/calculator.js (transformed for widget use)
|
||||
/**
|
||||
* Dog Calorie Calculator - iframe version
|
||||
* by Canine Nutrition and Wellness
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user