سامانه صدور فاکتور و حسابداری
build(pages): compile and syntax-check 14 ordered extensions into hashed same-origin JS bundle
davoodmehraban89 committed
Oct 13, 2026 at 01:03 UTC
04f1720a2ac8ea45dda9ac43f298e0e25fbe7ba3
1 file changed
+24
scripts/build-pages.mjs
+24
@@ -72,6 +72,30 @@ if(/script-src[^;]*'unsafe-inline'/i.test(html))throw new Error('Built page stil
72
const externalScripts=[...html.matchAll(/<script[^>]+src="https:\/\/[^\"]+"[^>]*>/gi)].map(m=>m[0]);
73
for(const tag of externalScripts)if(!/integrity=/i.test(tag))throw new Error('Built page contains external script without SRI: '+tag);
74
75
+// Build one self-hosted production request from the 14 dependency-ordered IIFEs.
76
+// Both the list and all individual sources remain canonical in git.
77
+const bootstrapPath=path.join(out,'js','bootstrap.js');
78
+const originalBootstrap=fs.readFileSync(bootstrapPath,'utf8');
79
+const manifestMatch=originalBootstrap.match(/const FINORA_EXTENSION_SCRIPTS=\[([\s\S]*?)\];/);
80
+if(!manifestMatch)throw new Error('Missing canonical module list for bundle build');
81
+const extensionFiles=JSON.parse('['+manifestMatch[1]+']');
82
+if(extensionFiles.length!==14||new Set(extensionFiles.map(s=>s.split('?')[0])).size!==14)
83
+ throw new Error('Unexpected module list or duplicate extension');
84
+const bundleParts=extensionFiles.map(src=>{
85
+ if(!/^js\/[a-z0-9-]+\.js\?v=[a-z0-9-]+$/i.test(src))throw new Error('Invalid extension source');
86
+ const sourcePath=path.join(out,src.split('?')[0]);
87
+ if(!fs.existsSync(sourcePath))throw new Error('Missing extension: '+src);
88
+ return '\n;\n'+fs.readFileSync(sourcePath,'utf8')+'\n;\n';
89
+});
90
+const bundle='// Bundled local Finora extensions, original dependency order.\n'+bundleParts.join('');
91
+new vm.Script(bundle,{filename:'finora-extension-bundle.js'});
92
+const bundleHash=createHash('sha256').update(bundle).digest('hex').slice(0,16);
93
+fs.writeFileSync(path.join(out,'js','finora-extension-bundle.js'),bundle);
94
+const marker='const FINORA_EXTENSION_BUNDLE_URL=null;';
95
+if(!originalBootstrap.includes(marker))throw new Error('Missing bundle loader feature flag');
96
+fs.writeFileSync(bootstrapPath,originalBootstrap.replace(marker,
97
+ 'const FINORA_EXTENSION_BUNDLE_URL="js/finora-extension-bundle.js?v='+bundleHash+'";'));
98
+
99
fs.writeFileSync(path.join(out,'index.html'),html);
100
fs.writeFileSync(path.join(out,'js','generated-event-handlers.js'),generated);
101
fs.writeFileSync(path.join(out,'js','generated-dynamic-handlers.js'),dynamic);