vite-plugin-inline-multipage 1.0.10 → 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.d.ts +0 -6
- package/dist/index.js +23 -19
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
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;
|
|
11
5
|
remap?: Record<string, string>;
|
|
12
6
|
}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,8 @@ 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 = {} } = options || {};
|
|
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',
|
|
@@ -23,15 +24,15 @@ export default function inlineEverythingPlugin(options) {
|
|
|
23
24
|
});
|
|
24
25
|
for (const htmlFile of htmlFiles) {
|
|
25
26
|
let html = fs.readFileSync(htmlFile, 'utf8');
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const href =
|
|
27
|
+
const $ = cheerio.load(html);
|
|
28
|
+
$('link[rel="stylesheet"]').each((_, el) => {
|
|
29
|
+
const href = $(el).attr('href');
|
|
29
30
|
if (href) {
|
|
30
31
|
const filename = path.basename(href);
|
|
31
32
|
if (assetMap.has(filename)) {
|
|
32
33
|
try {
|
|
33
|
-
const
|
|
34
|
-
|
|
34
|
+
const css = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
35
|
+
$(el).replaceWith(`<style>${css}</style>`);
|
|
35
36
|
}
|
|
36
37
|
catch (e) {
|
|
37
38
|
console.warn(`Failed to inline CSS ${filename}:`, e.message);
|
|
@@ -39,14 +40,14 @@ export default function inlineEverythingPlugin(options) {
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
|
-
|
|
43
|
-
const src =
|
|
43
|
+
$('script[type="module"][src]').each((_, el) => {
|
|
44
|
+
const src = $(el).attr('src');
|
|
44
45
|
if (src) {
|
|
45
46
|
const filename = path.basename(src);
|
|
46
47
|
if (assetMap.has(filename)) {
|
|
47
48
|
try {
|
|
48
|
-
const
|
|
49
|
-
|
|
49
|
+
const js = fs.readFileSync(assetMap.get(filename), 'utf8');
|
|
50
|
+
$(el).replaceWith(`<script type="module">${js}</script>`);
|
|
50
51
|
}
|
|
51
52
|
catch (e) {
|
|
52
53
|
console.warn(`Failed to inline JS ${filename}:`, e.message);
|
|
@@ -54,18 +55,21 @@ export default function inlineEverythingPlugin(options) {
|
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
$('link[rel="modulepreload"]').remove();
|
|
59
|
+
$('link[rel="preload"]').remove();
|
|
60
|
+
$('script[data-sveltekit-hydrate]').remove();
|
|
60
61
|
const originalName = path.basename(htmlFile, '.html');
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
fs.
|
|
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);
|
|
69
|
+
}
|
|
66
70
|
}
|
|
67
71
|
else {
|
|
68
|
-
fs.writeFileSync(htmlFile,
|
|
72
|
+
fs.writeFileSync(htmlFile, $.html());
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
if (cleanUp) {
|
package/package.json
CHANGED