Repository navigation
Expand file tree
/
Copy pathbuild-assets.js
More file actions
executable file
·67 lines (55 loc) · 2.19 KB
/
Copy pathbuild-assets.js
File metadata and controls
executable file
·67 lines (55 loc) · 2.19 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
#!/usr/bin/env node
/**
* Build script to inline static assets into JavaScript
* This reads HTML, CSS, and JS files and creates a module that exports them as strings
*/
const fs = require('fs');
const path = require('path');
// Read static files
const indexHtml = fs.readFileSync(path.join(__dirname, 'src/frontend/index.html'), 'utf8');
const stylesCss = fs.readFileSync(path.join(__dirname, 'src/frontend/styles.css'), 'utf8');
const appJs = fs.readFileSync(path.join(__dirname, 'src/frontend/app.js'), 'utf8');
const constantsJs = fs.readFileSync(path.join(__dirname, 'src/frontend/constants.js'), 'utf8');
// Escape backticks and backslashes in the content
function escapeTemplate(str) {
return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$');
}
// Generate the assets module
const assetsModule = `// Auto-generated file - DO NOT EDIT
// Generated by build-assets.js
const indexHtml = \`${escapeTemplate(indexHtml)}\`;
const stylesCss = \`${escapeTemplate(stylesCss)}\`;
const appJs = \`${escapeTemplate(appJs)}\`;
const constantsJs = \`${escapeTemplate(constantsJs)}\`;
/**
* Serve static assets directly from inlined content
*/
export async function serveAsset(pathname) {
// Map of paths to assets and their content types
const assets = {
'/': { content: indexHtml, type: 'text/html; charset=utf-8' },
'/index.html': { content: indexHtml, type: 'text/html; charset=utf-8' },
'/styles.css': { content: stylesCss, type: 'text/css; charset=utf-8' },
'/app.js': { content: appJs, type: 'application/javascript; charset=utf-8' },
'/constants.js': { content: constantsJs, type: 'application/javascript; charset=utf-8' },
};
// Get the asset
const asset = assets[pathname];
if (asset) {
return new Response(asset.content, {
status: 200,
headers: {
'Content-Type': asset.type,
'Cache-Control': 'public, max-age=3600',
},
});
}
// Return null if asset not found
return null;
}
`;
// Write the generated module
const outputPath = path.join(__dirname, 'src/worker/assets.js');
fs.writeFileSync(outputPath, assetsModule, 'utf8');
console.log('✅ Static assets built successfully!');
console.log(` Generated: ${outputPath}`);