-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline-assets.js
More file actions
74 lines (59 loc) · 2.24 KB
/
inline-assets.js
File metadata and controls
74 lines (59 loc) · 2.24 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
import fs from 'fs';
import path from 'path';
// Function to inline assets after build
function inlineAssetsAfterBuild() {
const distDir = './dist';
const indexPath = path.join(distDir, 'index.html');
if (!fs.existsSync(indexPath)) {
console.log('index.html not found, skipping asset inlining');
return;
}
let htmlContent = fs.readFileSync(indexPath, 'utf8');
// Find and inline CSS files
const cssRegex = /<link[^>]*rel="stylesheet"[^>]*href="([^"]*)"[^>]*>/g;
let match;
while ((match = cssRegex.exec(htmlContent)) !== null) {
const cssPath = match[1];
const fullCssPath = path.join(distDir, cssPath);
if (fs.existsSync(fullCssPath)) {
console.log(`Inlining CSS: ${cssPath}`);
const cssContent = fs.readFileSync(fullCssPath, 'utf8');
htmlContent = htmlContent.replace(match[0], `<style>${cssContent}</style>`);
// Remove the CSS file since it's now inlined
fs.unlinkSync(fullCssPath);
}
}
// Find and inline images as base64
const imageRegex = /<link[^>]*href="([^"]*\.(png|jpg|jpeg|gif|svg|webp))"[^>]*>/gi;
while ((match = imageRegex.exec(htmlContent)) !== null) {
const imagePath = match[1];
const fullImagePath = path.join(distDir, imagePath);
if (fs.existsSync(fullImagePath)) {
console.log(`Inlining image: ${imagePath}`);
const imageBuffer = fs.readFileSync(fullImagePath);
const imageBase64 = imageBuffer.toString('base64');
const mimeType = getMimeType(imagePath);
const dataUrl = `data:${mimeType};base64,${imageBase64}`;
htmlContent = htmlContent.replace(match[0], match[0].replace(imagePath, dataUrl));
// Remove the image file since it's now inlined
fs.unlinkSync(fullImagePath);
}
}
// Write the updated HTML
fs.writeFileSync(indexPath, htmlContent);
console.log('Assets inlined successfully!');
}
function getMimeType(filename) {
const ext = path.extname(filename).toLowerCase();
const mimeTypes = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.webp': 'image/webp'
};
return mimeTypes[ext] || 'application/octet-stream';
}
// Run the inlining process
inlineAssetsAfterBuild();