vite-plugin-inline-multipage 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +36 -30
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import fs from 'fs';
|
|
|
4
4
|
import * as cheerio from 'cheerio';
|
|
5
5
|
export default function inlineEverythingPlugin(options) {
|
|
6
6
|
const { buildDir = 'build', cleanUp = false, remap = {}, } = options || {};
|
|
7
|
+
const remapKeys = new Set(Object.keys(remap));
|
|
7
8
|
return {
|
|
8
9
|
name: 'inline-everything',
|
|
9
10
|
apply: 'build',
|
|
@@ -11,14 +12,14 @@ export default function inlineEverythingPlugin(options) {
|
|
|
11
12
|
const resolvedBuildDir = path.resolve(process.cwd(), buildDir);
|
|
12
13
|
const htmlFiles = await glob('**/*.html', {
|
|
13
14
|
cwd: resolvedBuildDir,
|
|
14
|
-
absolute: true
|
|
15
|
+
absolute: true
|
|
15
16
|
});
|
|
16
17
|
const assetFiles = await glob('**/_app/immutable/**/*.{css,js}', {
|
|
17
18
|
cwd: resolvedBuildDir,
|
|
18
|
-
absolute: true
|
|
19
|
+
absolute: true
|
|
19
20
|
});
|
|
20
21
|
const assetMap = new Map();
|
|
21
|
-
assetFiles.forEach(
|
|
22
|
+
assetFiles.forEach(file => {
|
|
22
23
|
assetMap.set(path.basename(file), file);
|
|
23
24
|
});
|
|
24
25
|
for (const htmlFile of htmlFiles) {
|
|
@@ -26,44 +27,49 @@ export default function inlineEverythingPlugin(options) {
|
|
|
26
27
|
const $ = cheerio.load(html);
|
|
27
28
|
$('link[rel="stylesheet"]').each((_, el) => {
|
|
28
29
|
const href = $(el).attr('href');
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
if (href) {
|
|
31
|
+
const filename = path.basename(href);
|
|
32
|
+
if (assetMap.has(filename)) {
|
|
33
|
+
try {
|
|
34
|
+
const css = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
35
|
+
$(el).replaceWith(`<style>${css}</style>`);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
console.warn(`Failed to inline CSS ${filename}:`, e.message);
|
|
39
|
+
}
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
42
|
});
|
|
40
43
|
$('script[type="module"][src]').each((_, el) => {
|
|
41
44
|
const src = $(el).attr('src');
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
if (src) {
|
|
46
|
+
const filename = path.basename(src);
|
|
47
|
+
if (assetMap.has(filename)) {
|
|
48
|
+
try {
|
|
49
|
+
const js = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
50
|
+
$(el).replaceWith(`<script type="module">${js}</script>`);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.warn(`Failed to inline JS ${filename}:`, e.message);
|
|
54
|
+
}
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
});
|
|
53
58
|
$('link[rel="modulepreload"]').remove();
|
|
54
59
|
$('link[rel="preload"]').remove();
|
|
55
60
|
$('script[data-sveltekit-hydrate]').remove();
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
const originalName = path.basename(htmlFile, '.html');
|
|
62
|
+
const isRouteFile = remapKeys.has(originalName);
|
|
63
|
+
if (isRouteFile) {
|
|
64
|
+
const remappedName = remap[originalName];
|
|
65
|
+
const remappedPath = path.join(path.dirname(htmlFile), remappedName);
|
|
66
|
+
fs.writeFileSync(remappedPath, $.html());
|
|
67
|
+
if (remappedPath !== htmlFile) {
|
|
68
|
+
fs.unlinkSync(htmlFile);
|
|
61
69
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (outputPath !== htmlFile) {
|
|
66
|
-
fs.unlinkSync(htmlFile);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
fs.writeFileSync(htmlFile, $.html());
|
|
67
73
|
}
|
|
68
74
|
}
|
|
69
75
|
if (cleanUp) {
|
|
@@ -74,6 +80,6 @@ export default function inlineEverythingPlugin(options) {
|
|
|
74
80
|
console.warn('Could not clean up _app directory:', e.message);
|
|
75
81
|
}
|
|
76
82
|
}
|
|
77
|
-
}
|
|
83
|
+
}
|
|
78
84
|
};
|
|
79
85
|
}
|
package/package.json
CHANGED