51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
/**
|
||
|
|
* Build-time SVG Symbol Injector
|
||
|
|
* Injects external SVG symbols into HTML files during build
|
||
|
|
* Usage: node inject-symbols.js input.html output.html symbols.svg
|
||
|
|
*/
|
||
|
|
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
function injectSymbols(htmlFile, outputFile, symbolFile) {
|
||
|
|
try {
|
||
|
|
// Read files
|
||
|
|
const htmlContent = fs.readFileSync(htmlFile, 'utf8');
|
||
|
|
const symbolContent = fs.readFileSync(symbolFile, 'utf8');
|
||
|
|
|
||
|
|
// Extract symbols from SVG (remove outer svg tags)
|
||
|
|
const symbolsOnly = symbolContent
|
||
|
|
.replace(/<\?xml[^>]*\?>/, '')
|
||
|
|
.replace(/<svg[^>]*>/, '')
|
||
|
|
.replace(/<\/svg>$/, '')
|
||
|
|
.trim();
|
||
|
|
|
||
|
|
// Inject symbols before closing body tag
|
||
|
|
const injectedHtml = htmlContent.replace(
|
||
|
|
'</body>',
|
||
|
|
` ${symbolsOnly}\n</body>`
|
||
|
|
);
|
||
|
|
|
||
|
|
// Write output
|
||
|
|
fs.writeFileSync(outputFile, injectedHtml);
|
||
|
|
console.log(`✅ Symbols injected: ${htmlFile} → ${outputFile}`);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Error injecting symbols:', error.message);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Command line usage
|
||
|
|
if (require.main === module) {
|
||
|
|
const [,, htmlFile, outputFile, symbolFile] = process.argv;
|
||
|
|
|
||
|
|
if (!htmlFile || !outputFile || !symbolFile) {
|
||
|
|
console.log('Usage: node inject-symbols.js <input.html> <output.html> <symbols.svg>');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
injectSymbols(htmlFile, outputFile, symbolFile);
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = injectSymbols;
|