-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline-script.js
More file actions
42 lines (33 loc) · 1.5 KB
/
inline-script.js
File metadata and controls
42 lines (33 loc) · 1.5 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
const fs = require('fs');
const path = require('path');
const buildPath = path.resolve(__dirname, 'build');
const indexPath = path.join(buildPath, 'index.html');
console.log('Starting inlining process...');
console.log('Build path:', buildPath);
console.log('Index path:', indexPath);
let htmlContent = fs.readFileSync(indexPath, 'utf8');
console.log('Read index.html content.');
// Inline CSS
const cssLinkRegex = /<link href="\.\/static\/css\/(main\.[a-f0-9]+\.css)" rel="stylesheet">/;
const cssMatch = htmlContent.match(cssLinkRegex);
if (cssMatch) {
console.log('CSS link found.');
const cssFileName = cssMatch[1];
const cssFilePath = path.join(buildPath, 'static', 'css', cssFileName);
const cssContent = fs.readFileSync(cssFilePath, 'utf8');
htmlContent = htmlContent.replace(cssMatch[0], `<style>${cssContent}</style>`);
console.log('CSS inlined.');
}
// Inline JavaScript
const jsScriptRegex = /<script defer="defer" src="\.\/static\/js\/(main\.[a-f0-9]+\.js)"><\/script>/;
const jsMatch = htmlContent.match(jsScriptRegex);
if (jsMatch) {
console.log('JavaScript script found.');
const jsFileName = jsMatch[1];
const jsFilePath = path.join(buildPath, 'static', 'js', jsFileName);
const jsContent = fs.readFileSync(jsFilePath, 'utf8').replace(/\/\/# sourceMappingURL=.*/g, '');
htmlContent = htmlContent.replace(jsMatch[0], `<script>${jsContent}</script>`);
console.log('JavaScript inlined.');
}
fs.writeFileSync(indexPath, htmlContent, 'utf8');
console.log('Assets inlined successfully!');