-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-inline-assets.js
More file actions
70 lines (57 loc) · 2.44 KB
/
vite-plugin-inline-assets.js
File metadata and controls
70 lines (57 loc) · 2.44 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
import fs from 'fs';
import path from 'path';
export function inlineAssetsPlugin() {
return {
name: 'inline-assets',
generateBundle(options, bundle) {
// Find the HTML file
const htmlFile = Object.keys(bundle).find(fileName => fileName.endsWith('.html'));
if (!htmlFile) return;
const htmlAsset = bundle[htmlFile];
let htmlContent = htmlAsset.source;
// Inline CSS files
const cssFiles = Object.keys(bundle).filter(fileName => fileName.endsWith('.css'));
for (const cssFile of cssFiles) {
const cssAsset = bundle[cssFile];
const cssContent = cssAsset.source;
// Replace CSS link with inline style
const cssLinkRegex = new RegExp(`<link[^>]*href="[^"]*${cssFile.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"[^>]*>`, 'g');
htmlContent = htmlContent.replace(cssLinkRegex, `<style>${cssContent}</style>`);
// Remove the CSS file from bundle since it's now inlined
delete bundle[cssFile];
}
// Convert images to base64 and inline them
const imageFiles = Object.keys(bundle).filter(fileName =>
fileName.match(/\.(png|jpg|jpeg|gif|svg|webp)$/i)
);
for (const imageFile of imageFiles) {
const imageAsset = bundle[imageFile];
const imageBuffer = imageAsset.source;
const imageBase64 = imageBuffer.toString('base64');
const mimeType = getMimeType(imageFile);
const dataUrl = `data:${mimeType};base64,${imageBase64}`;
// Replace image references with data URLs
const imageRegex = new RegExp(`href="[^"]*${imageFile.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`, 'g');
htmlContent = htmlContent.replace(imageRegex, `href="${dataUrl}"`);
const srcRegex = new RegExp(`src="[^"]*${imageFile.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`, 'g');
htmlContent = htmlContent.replace(srcRegex, `src="${dataUrl}"`);
// Remove the image file from bundle since it's now inlined
delete bundle[imageFile];
}
// Update the HTML content
htmlAsset.source = htmlContent;
}
};
}
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';
}