-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-static.js.bak
More file actions
417 lines (354 loc) · 15.3 KB
/
Copy pathbuild-static.js.bak
File metadata and controls
417 lines (354 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const fs = require('fs');
const path = require('path');
const { minify } = require('html-minifier-terser');
const viewsDirectory = './src/views/';
const layoutDirectory = './src/layout/';
const componentsDirectory = './src/components/';
const distDirectory = './dist/';
// Meta tags configuration for each view
const viewMeta = {
'home': {
title: 'Home - Dissent.js',
description: 'Welcome to Dissent.js - A lightweight JavaScript framework for building modern web applications'
},
'about': {
title: 'About - Dissent.js',
description: 'Learn about Dissent.js - A clean, lightweight JavaScript library for simplified web development'
},
'404': {
title: 'Page Not Found - Dissent.js',
description: 'The page you are looking for could not be found'
}
};
function getDefaultMeta() {
return {
title: 'Dissent.js',
description: 'Discover Dissent.js, a lightweight, flexible JavaScript framework for building modern web applications'
};
}
function injectComponents(content) {
const componentRegex = /<div class="([\w-]+)"><\/div>/g;
let processedContent = content;
let match;
while ((match = componentRegex.exec(content)) !== null) {
const componentName = match[1];
const componentHtmlPath = path.join(componentsDirectory, componentName, `${componentName}.html`);
if (fs.existsSync(componentHtmlPath)) {
const componentContent = fs.readFileSync(componentHtmlPath, 'utf8');
const replacement = `<div class="${componentName}">${componentContent}</div>`;
processedContent = processedContent.replace(match[0], replacement);
}
}
return processedContent;
}
function generateStaticPages() {
// Check for existing assets before cleaning
const existingAssets = {};
const assets = ['main.min.css', 'main.min.js'];
assets.forEach(asset => {
const assetPath = path.join(__dirname, 'dist', asset);
if (fs.existsSync(assetPath)) {
existingAssets[asset] = fs.readFileSync(assetPath, 'utf8');
}
});
// Clean and recreate dist directory
if (fs.existsSync(distDirectory)) {
fs.rmSync(distDirectory, { recursive: true, force: true });
}
fs.mkdirSync(distDirectory, { recursive: true });
// Read the base template
const baseTemplatePath = path.join(__dirname, 'src', 'index.html');
let baseTemplate = fs.readFileSync(baseTemplatePath, 'utf8');
// Inject header and footer into base template
const headerPath = path.join(layoutDirectory, 'header', 'header.html');
const footerPath = path.join(layoutDirectory, 'footer', 'footer.html');
if (fs.existsSync(headerPath)) {
const headerContent = fs.readFileSync(headerPath, 'utf8');
baseTemplate = baseTemplate.replace(/<header class="header">[\s\S]*?<\/header>/, `<header class="header">${headerContent}</header>`);
}
if (fs.existsSync(footerPath)) {
const footerContent = fs.readFileSync(footerPath, 'utf8');
baseTemplate = baseTemplate.replace(/<footer class="footer">[\s\S]*?<\/footer>/, `<footer class="footer">${footerContent}</footer>`);
}
// Get all views
const views = fs.readdirSync(viewsDirectory).filter(item => {
const itemPath = path.join(viewsDirectory, item);
return fs.statSync(itemPath).isDirectory();
});
// Generate page for each view
views.forEach(viewName => {
const viewHtmlPath = path.join(viewsDirectory, viewName, `${viewName}.html`);
if (fs.existsSync(viewHtmlPath)) {
let viewContent = fs.readFileSync(viewHtmlPath, 'utf8');
// Inject components into view content
viewContent = injectComponents(viewContent);
// Create full page HTML
let pageHtml = baseTemplate.replace(
/<div id="view-container"><\/div>/,
`<div id="view-container">${viewContent}</div>`
);
// Update meta tags
const meta = viewMeta[viewName] || getDefaultMeta();
pageHtml = pageHtml.replace(
/<title>[^<]*<\/title>/,
`<title>${meta.title}</title>`
);
pageHtml = pageHtml.replace(
/<meta name="description" content="[^"]*">/,
`<meta name="description" content="${meta.description}">`
);
// Fix image paths in the generated HTML
pageHtml = pageHtml.replace(/\.\.\/\.\.\/images\//g, './images/');
// Add component CSS links to the head for all views
let cssLinks = '';
// Add content CSS for the home page
if (viewName === 'home') {
cssLinks += '<link rel="stylesheet" href="./components/content/content.css">';
}
// Always add layout CSS files (header, footer, nav)
cssLinks += '<link rel="stylesheet" href="./layout/header/header.css">';
cssLinks += '<link rel="stylesheet" href="./layout/footer/footer.css">';
cssLinks += '<link rel="stylesheet" href="./layout/nav/nav.css">';
// Add all CSS links to the head
pageHtml = pageHtml.replace('</head>', cssLinks + '</head>');
// Write the static page
const outputPath = path.join(distDirectory, `${viewName}.html`);
fs.writeFileSync(outputPath, pageHtml);
console.log(`Generated static page: ${outputPath}`);
}
});
// Generate index.html that redirects to home.html
const indexContent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=home.html">
<title>Redirecting...</title>
</head>
<body>
<p>Redirecting to <a href="home.html">home page</a>...</p>
</body>
</html>`;
fs.writeFileSync(path.join(distDirectory, 'index.html'), indexContent);
console.log('Generated index.html redirect');
return existingAssets;
}
function copyAssets(existingAssets = {}) {
// Copy images from src
const imagesSrc = path.join(__dirname, 'src', 'images');
const imagesDest = path.join(distDirectory, 'images');
if (fs.existsSync(imagesSrc)) {
if (!fs.existsSync(imagesDest)) {
fs.mkdirSync(imagesDest, { recursive: true });
}
copyDirectoryRecursive(imagesSrc, imagesDest);
console.log('Copied images directory');
}
// Copy component files (CSS, JS, HTML)
copyComponentFiles();
// For CSS and JS, use existing assets if available, otherwise create minimal versions
const assets = ['main.min.css', 'main.min.js'];
let assetsFound = 0;
assets.forEach(asset => {
const destPath = path.join(distDirectory, asset);
if (existingAssets[asset]) {
// Use the asset that was captured before cleaning
fs.writeFileSync(destPath, existingAssets[asset]);
console.log(`Used existing asset: ${asset}`);
assetsFound++;
} else {
// Create minimal fallback assets
if (asset === 'main.min.css') {
// Create a minimal CSS file
const minimalCss = `
/* Minimal styles for Dissent.js static build */
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
header, footer { padding: 1rem; background: #f5f5f5; }
main { padding: 2rem; }
h1, h2, h3 { color: #333; }
a { color: #007acc; text-decoration: none; }
a:hover { text-decoration: underline; }
`;
fs.writeFileSync(destPath, minimalCss.trim());
console.log(`Created minimal CSS: ${asset}`);
assetsFound++;
} else if (asset === 'main.min.js') {
// Create a minimal JS file or skip it
const minimalJs = `
// Minimal JavaScript for Dissent.js static build
console.log('Dissent.js static build loaded');
// Make sure paths to components work in static build
window.addEventListener('DOMContentLoaded', function() {
// Load content component if present
const contentElements = document.querySelectorAll('.content');
if (contentElements.length > 0) {
// Load the script
const contentScript = document.createElement('script');
contentScript.src = './components/content/content.js';
document.head.appendChild(contentScript);
}
// Always load layout scripts
const headerScript = document.createElement('script');
headerScript.src = './layout/header/header.js';
document.head.appendChild(headerScript);
const footerScript = document.createElement('script');
footerScript.src = './layout/footer/footer.js';
document.head.appendChild(footerScript);
const navScript = document.createElement('script');
navScript.src = './layout/nav/nav.js';
document.head.appendChild(navScript);
});
`;
fs.writeFileSync(destPath, minimalJs.trim());
console.log(`Created minimal JS: ${asset}`);
assetsFound++;
}
}
});
if (assetsFound < assets.length) {
console.log(`Note: Created ${assetsFound}/${assets.length} assets. For full functionality, run 'yarn build' first.`);
}
}
// Function to copy component files
function copyComponentFiles() {
// Copy component files
const componentsSrc = path.join(__dirname, 'src', 'components');
const componentsDest = path.join(distDirectory, 'components');
if (fs.existsSync(componentsSrc)) {
if (!fs.existsSync(componentsDest)) {
fs.mkdirSync(componentsDest, { recursive: true });
}
// Copy the content component files specifically
const contentSrc = path.join(componentsSrc, 'content');
if (fs.existsSync(contentSrc)) {
const contentDest = path.join(componentsDest, 'content');
if (!fs.existsSync(contentDest)) {
fs.mkdirSync(contentDest, { recursive: true });
}
// Copy content.html and fix image paths
const contentHtml = path.join(contentSrc, 'content.html');
if (fs.existsSync(contentHtml)) {
let contentHtmlData = fs.readFileSync(contentHtml, 'utf8');
// Fix image paths from ../../images/ to ./images/
contentHtmlData = contentHtmlData.replace(/\.\.\/\.\.\/images\//g, './images/');
fs.writeFileSync(path.join(contentDest, 'content.html'), contentHtmlData);
console.log('Copied and fixed image paths in content.html');
}
// Copy content.js
const contentJs = path.join(contentSrc, 'content.js');
if (fs.existsSync(contentJs)) {
fs.copyFileSync(contentJs, path.join(contentDest, 'content.js'));
}
// Copy content.css
const contentCss = path.join(contentSrc, 'content.css');
if (fs.existsSync(contentCss)) {
fs.copyFileSync(contentCss, path.join(contentDest, 'content.css'));
} else {
// If CSS doesn't exist, try to copy and rename the SCSS file
const contentScss = path.join(contentSrc, 'content.scss');
if (fs.existsSync(contentScss)) {
// Basic SCSS conversion - Just copy as CSS for now
fs.copyFileSync(contentScss, path.join(contentDest, 'content.css'));
}
}
console.log('Copied content component files');
}
}
// Copy layout files (header and footer)
copyLayoutFiles();
}
// Function to copy layout files (header and footer)
function copyLayoutFiles() {
const layoutSrc = path.join(__dirname, 'src', 'layout');
const layoutDest = path.join(distDirectory, 'layout');
if (fs.existsSync(layoutSrc)) {
if (!fs.existsSync(layoutDest)) {
fs.mkdirSync(layoutDest, { recursive: true });
}
// Copy header files
copyLayoutComponent('header', layoutSrc, layoutDest);
// Copy footer files
copyLayoutComponent('footer', layoutSrc, layoutDest);
// Copy nav files
copyLayoutComponent('nav', layoutSrc, layoutDest);
console.log('Copied layout component files');
}
}
// Helper function to copy a layout component
function copyLayoutComponent(componentName, layoutSrc, layoutDest) {
const componentSrc = path.join(layoutSrc, componentName);
if (fs.existsSync(componentSrc)) {
const componentDest = path.join(layoutDest, componentName);
if (!fs.existsSync(componentDest)) {
fs.mkdirSync(componentDest, { recursive: true });
}
// Copy HTML
const htmlFile = path.join(componentSrc, `${componentName}.html`);
if (fs.existsSync(htmlFile)) {
fs.copyFileSync(htmlFile, path.join(componentDest, `${componentName}.html`));
}
// Copy JS
const jsFile = path.join(componentSrc, `${componentName}.js`);
if (fs.existsSync(jsFile)) {
fs.copyFileSync(jsFile, path.join(componentDest, `${componentName}.js`));
}
// Copy CSS or SCSS
const cssFile = path.join(componentSrc, `${componentName}.css`);
if (fs.existsSync(cssFile)) {
fs.copyFileSync(cssFile, path.join(componentDest, `${componentName}.css`));
} else {
// If CSS doesn't exist, try to copy and rename the SCSS file
const scssFile = path.join(componentSrc, `${componentName}.scss`);
if (fs.existsSync(scssFile)) {
// Basic SCSS conversion - Just copy as CSS for now
fs.copyFileSync(scssFile, path.join(componentDest, `${componentName}.css`));
}
}
}
}
}
// Helper function to copy directory recursively
function copyDirectoryRecursive(source, destination) {
const entries = fs.readdirSync(source, { withFileTypes: true });
for (const entry of entries) {
const sourcePath = path.join(source, entry.name);
const destPath = path.join(destination, entry.name);
if (entry.isDirectory()) {
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath, { recursive: true });
}
copyDirectoryRecursive(sourcePath, destPath);
} else {
fs.copyFileSync(sourcePath, destPath);
}
}
}
function minifyHtmlFiles() {
// Dynamically find all HTML files in the dist directory
const files = fs.readdirSync(distDirectory);
const htmlFiles = files.filter(file => file.endsWith('.html'));
console.log(`Found ${htmlFiles.length} HTML files to minify: ${htmlFiles.join(', ')}`);
htmlFiles.forEach(fileName => {
const filePath = path.join(distDirectory, fileName);
const content = fs.readFileSync(filePath, 'utf8');
minify(content, {
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true,
})
.then(minified => {
fs.writeFileSync(filePath, minified);
console.log(`Minified: ${fileName}`);
})
.catch(err => {
console.error(`Error minifying ${fileName}:`, err);
});
});
}
// Run the static build
console.log('Building static version of Dissent.js...');
const existingAssets = generateStaticPages();
copyAssets(existingAssets);
minifyHtmlFiles();
console.log('Static build complete! SEO-friendly HTML files generated in dist/');