vite-plugin-inline-multipage 1.0.9 → 1.0.11
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 +1 -6
- package/dist/index.js +41 -34
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { type Plugin } from 'vite';
|
|
2
2
|
export interface InlineEverythingOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Dir where the build files are located (default: 'build')
|
|
5
|
-
*/
|
|
6
3
|
buildDir?: string;
|
|
7
|
-
/**
|
|
8
|
-
* If the plugin should remove the _app directory after inlining (default: true)
|
|
9
|
-
*/
|
|
10
4
|
cleanUp?: boolean;
|
|
5
|
+
remap?: Record<string, string>;
|
|
11
6
|
}
|
|
12
7
|
export default function inlineEverythingPlugin(options?: InlineEverythingOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +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 } = options || {};
|
|
6
|
+
const { buildDir = 'build', cleanUp = false, remap = {}, } = options || {};
|
|
7
7
|
return {
|
|
8
8
|
name: 'inline-everything',
|
|
9
9
|
apply: 'build',
|
|
@@ -11,53 +11,60 @@ export default function inlineEverythingPlugin(options) {
|
|
|
11
11
|
const resolvedBuildDir = path.resolve(process.cwd(), buildDir);
|
|
12
12
|
const htmlFiles = await glob('**/*.html', {
|
|
13
13
|
cwd: resolvedBuildDir,
|
|
14
|
-
absolute: true
|
|
14
|
+
absolute: true,
|
|
15
15
|
});
|
|
16
16
|
const assetFiles = await glob('**/_app/immutable/**/*.{css,js}', {
|
|
17
17
|
cwd: resolvedBuildDir,
|
|
18
|
-
absolute: true
|
|
18
|
+
absolute: true,
|
|
19
19
|
});
|
|
20
20
|
const assetMap = new Map();
|
|
21
|
-
assetFiles.forEach(file => {
|
|
21
|
+
assetFiles.forEach((file) => {
|
|
22
22
|
assetMap.set(path.basename(file), file);
|
|
23
23
|
});
|
|
24
24
|
for (const htmlFile of htmlFiles) {
|
|
25
25
|
let html = fs.readFileSync(htmlFile, 'utf8');
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const href =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
console.warn(`Failed to inline CSS ${filename}:`, e.message);
|
|
38
|
-
}
|
|
26
|
+
const $ = cheerio.load(html);
|
|
27
|
+
$('link[rel="stylesheet"]').each((_, el) => {
|
|
28
|
+
const href = $(el).attr('href');
|
|
29
|
+
const filename = href && path.basename(href);
|
|
30
|
+
if (filename && assetMap.has(filename)) {
|
|
31
|
+
try {
|
|
32
|
+
const css = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
33
|
+
$(el).replaceWith(`<style>${css}</style>`);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.warn(`Failed to inline CSS ${filename}:`, e.message);
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
});
|
|
42
|
-
|
|
43
|
-
const src =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
console.warn(`Failed to inline JS ${filename}:`, e.message);
|
|
53
|
-
}
|
|
40
|
+
$('script[type="module"][src]').each((_, el) => {
|
|
41
|
+
const src = $(el).attr('src');
|
|
42
|
+
const filename = src && path.basename(src);
|
|
43
|
+
if (filename && assetMap.has(filename)) {
|
|
44
|
+
try {
|
|
45
|
+
const js = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
46
|
+
$(el).replaceWith(`<script type="module">${js}</script>`);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
console.warn(`Failed to inline JS ${filename}:`, e.message);
|
|
54
50
|
}
|
|
55
51
|
}
|
|
56
52
|
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
$('link[rel="modulepreload"]').remove();
|
|
54
|
+
$('link[rel="preload"]').remove();
|
|
55
|
+
$('script[data-sveltekit-hydrate]').remove();
|
|
56
|
+
const outputPath = (() => {
|
|
57
|
+
const original = path.basename(htmlFile);
|
|
58
|
+
const remapKey = original.replace(/\.html$/, '');
|
|
59
|
+
if (remap[remapKey]) {
|
|
60
|
+
return path.join(path.dirname(htmlFile), remap[remapKey]);
|
|
61
|
+
}
|
|
62
|
+
return htmlFile;
|
|
63
|
+
})();
|
|
64
|
+
fs.writeFileSync(outputPath, $.html());
|
|
65
|
+
if (outputPath !== htmlFile) {
|
|
66
|
+
fs.unlinkSync(htmlFile);
|
|
67
|
+
}
|
|
61
68
|
}
|
|
62
69
|
if (cleanUp) {
|
|
63
70
|
try {
|
|
@@ -67,6 +74,6 @@ export default function inlineEverythingPlugin(options) {
|
|
|
67
74
|
console.warn('Could not clean up _app directory:', e.message);
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
|
-
}
|
|
77
|
+
},
|
|
71
78
|
};
|
|
72
79
|
}
|
package/package.json
CHANGED