vite-plugin-inline-multipage 1.0.14 → 1.0.16
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.d.ts +6 -0
- package/dist/index.js +20 -38
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { type Plugin } from 'vite';
|
|
2
2
|
export interface InlineEverythingOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Dir where the build files are located (default: 'build')
|
|
5
|
+
*/
|
|
3
6
|
buildDir?: string;
|
|
7
|
+
/**
|
|
8
|
+
* If the plugin should remove the _app directory after inlining (default: true)
|
|
9
|
+
*/
|
|
4
10
|
cleanUp?: boolean;
|
|
5
11
|
remap?: Record<string, string>;
|
|
6
12
|
}
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,7 @@ import path from 'path';
|
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
import * as cheerio from 'cheerio';
|
|
5
5
|
export default function inlineEverythingPlugin(options) {
|
|
6
|
-
const { buildDir = 'build', cleanUp = false, remap = {}
|
|
7
|
-
const remapKeys = new Set(Object.keys(remap));
|
|
6
|
+
const { buildDir = 'build', cleanUp = false, remap = {} } = options || {};
|
|
8
7
|
return {
|
|
9
8
|
name: 'inline-everything',
|
|
10
9
|
apply: 'build',
|
|
@@ -24,16 +23,15 @@ export default function inlineEverythingPlugin(options) {
|
|
|
24
23
|
});
|
|
25
24
|
for (const htmlFile of htmlFiles) {
|
|
26
25
|
let html = fs.readFileSync(htmlFile, 'utf8');
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const href = $(el).attr('href');
|
|
26
|
+
const load_html = cheerio.load(html);
|
|
27
|
+
load_html('link[rel="stylesheet"]').each((_, el) => {
|
|
28
|
+
const href = load_html(el).attr('href');
|
|
31
29
|
if (href) {
|
|
32
30
|
const filename = path.basename(href);
|
|
33
31
|
if (assetMap.has(filename)) {
|
|
34
32
|
try {
|
|
35
|
-
const
|
|
36
|
-
|
|
33
|
+
const cssContent = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
34
|
+
load_html(el).replaceWith(`<style>${cssContent}</style>`);
|
|
37
35
|
}
|
|
38
36
|
catch (e) {
|
|
39
37
|
console.warn(`Failed to inline CSS ${filename}:`, e.message);
|
|
@@ -41,15 +39,14 @@ export default function inlineEverythingPlugin(options) {
|
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const src = $(el).attr('src');
|
|
42
|
+
load_html('script[type="module"][src]').each((_, el) => {
|
|
43
|
+
const src = load_html(el).attr('src');
|
|
47
44
|
if (src) {
|
|
48
45
|
const filename = path.basename(src);
|
|
49
46
|
if (assetMap.has(filename)) {
|
|
50
47
|
try {
|
|
51
|
-
const
|
|
52
|
-
|
|
48
|
+
const jsContent = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
49
|
+
load_html(el).replaceWith(`<script type="module">${jsContent}</script>`);
|
|
53
50
|
}
|
|
54
51
|
catch (e) {
|
|
55
52
|
console.warn(`Failed to inline JS ${filename}:`, e.message);
|
|
@@ -57,33 +54,18 @@ export default function inlineEverythingPlugin(options) {
|
|
|
57
54
|
}
|
|
58
55
|
}
|
|
59
56
|
});
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
remapKey = path.relative(resolvedBuildDir, path.dirname(htmlFile)).replace(/\\/g, '/'); // normalize slashes
|
|
57
|
+
load_html('link[rel="modulepreload"]').remove();
|
|
58
|
+
load_html('link[rel="preload"]').remove();
|
|
59
|
+
load_html('script[data-sveltekit-hydrate]').remove();
|
|
60
|
+
const originalName = path.basename(htmlFile, '.html');
|
|
61
|
+
const targetName = remap[originalName];
|
|
62
|
+
if (targetName) {
|
|
63
|
+
const newPath = path.join(path.dirname(htmlFile), targetName);
|
|
64
|
+
fs.writeFileSync(newPath, load_html.html());
|
|
65
|
+
fs.rmSync(htmlFile);
|
|
70
66
|
}
|
|
71
67
|
else {
|
|
72
|
-
|
|
73
|
-
remapKey = path.basename(htmlFile, '.html');
|
|
74
|
-
}
|
|
75
|
-
if (remapKeys.has(remapKey)) {
|
|
76
|
-
const remappedName = remap[remapKey];
|
|
77
|
-
const newPath = path.join(path.dirname(htmlFile), remappedName);
|
|
78
|
-
fs.writeFileSync(newPath, $.html());
|
|
79
|
-
// Remove old file if different path
|
|
80
|
-
if (newPath !== htmlFile) {
|
|
81
|
-
fs.unlinkSync(htmlFile);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
// No remap - write original file
|
|
86
|
-
fs.writeFileSync(htmlFile, $.html());
|
|
68
|
+
fs.writeFileSync(htmlFile, load_html.html());
|
|
87
69
|
}
|
|
88
70
|
}
|
|
89
71
|
if (cleanUp) {
|
package/package.json
CHANGED