@bddjr/vite-plugin-singlefile 3.0.0
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/LICENSE.txt +8 -0
- package/README.md +109 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +169 -0
- package/package.json +63 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2026 bddjr
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# vite plugin singlefile compression
|
|
2
|
+
|
|
3
|
+
Embed all assets into `dist/index.html`
|
|
4
|
+
|
|
5
|
+
Preview: https://bddjr.github.io/vite-plugin-singlefile/
|
|
6
|
+
|
|
7
|
+
> [!TIP]
|
|
8
|
+
> You may need: [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression)
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
npm i @bddjr/vite-plugin-singlefile@latest -D
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Then modify `vite.config.ts`, like [test/vite.config.ts](test/vite.config.ts)
|
|
17
|
+
|
|
18
|
+
```diff
|
|
19
|
+
+ import singleFile from '@bddjr/vite-plugin-singlefile'
|
|
20
|
+
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
plugins: [
|
|
23
|
+
vue(),
|
|
24
|
+
vueDevTools(),
|
|
25
|
+
+ singleFile(),
|
|
26
|
+
],
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Then use hash history, like [test/src/router/index.ts](test/src/router/index.ts)
|
|
30
|
+
|
|
31
|
+
```diff
|
|
32
|
+
const router = createRouter({
|
|
33
|
+
- history: createWebHistory(),
|
|
34
|
+
+ history: createWebHashHistory(),
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
Example:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
singleFileCompression({
|
|
43
|
+
rename: 'example.html'
|
|
44
|
+
}),
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### rename
|
|
48
|
+
|
|
49
|
+
Rename index.html
|
|
50
|
+
|
|
51
|
+
type: `string`
|
|
52
|
+
|
|
53
|
+
### tryInlineHtmlAssets
|
|
54
|
+
|
|
55
|
+
Try inline html used assets, if inlined or not used in JS.
|
|
56
|
+
|
|
57
|
+
default: `true`
|
|
58
|
+
|
|
59
|
+
type: `boolean`
|
|
60
|
+
|
|
61
|
+
### removeInlinedAssetFiles
|
|
62
|
+
|
|
63
|
+
Remove inlined asset files.
|
|
64
|
+
|
|
65
|
+
default: `true`
|
|
66
|
+
|
|
67
|
+
type: `boolean`
|
|
68
|
+
|
|
69
|
+
### tryInlineHtmlPublicIcon
|
|
70
|
+
|
|
71
|
+
Try inline html icon, if icon is in public dir.
|
|
72
|
+
|
|
73
|
+
default: `true`
|
|
74
|
+
|
|
75
|
+
type: `boolean`
|
|
76
|
+
|
|
77
|
+
### removeInlinedPublicIconFiles
|
|
78
|
+
|
|
79
|
+
Remove inlined html icon files.
|
|
80
|
+
|
|
81
|
+
default: `true`
|
|
82
|
+
|
|
83
|
+
type: `boolean`
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Effect
|
|
87
|
+
|
|
88
|
+
Preview: https://bddjr.github.io/vite-plugin-singlefile/
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
vite v8.0.3 building client environment for production...
|
|
92
|
+
✓ 43 modules transformed.
|
|
93
|
+
computing gzip size...
|
|
94
|
+
dist/index.html 123.51 kB │ gzip: 43.22 kB
|
|
95
|
+
|
|
96
|
+
✓ built in 379ms
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Clone
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
git clone https://github.com/bddjr/vite-plugin-singlefile
|
|
103
|
+
cd vite-plugin-singlefile
|
|
104
|
+
npm i
|
|
105
|
+
cd test
|
|
106
|
+
npm i
|
|
107
|
+
cd ..
|
|
108
|
+
npm run build
|
|
109
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/options.d.ts
|
|
4
|
+
interface Options {
|
|
5
|
+
/**
|
|
6
|
+
* Rename index.html
|
|
7
|
+
*/
|
|
8
|
+
rename?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Try inline html used assets, if inlined or not used in JS.
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
tryInlineHtmlAssets?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Remove inlined asset files.
|
|
16
|
+
* @default true
|
|
17
|
+
*/
|
|
18
|
+
removeInlinedAssetFiles?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Try inline html icon, if icon is in public dir.
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
tryInlineHtmlPublicIcon?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Remove inlined html icon files.
|
|
26
|
+
* @default true
|
|
27
|
+
*/
|
|
28
|
+
removeInlinedPublicIconFiles?: boolean;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/index.d.ts
|
|
32
|
+
declare function singleFile(opt?: Options): PluginOption;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { Options, singleFile as default, singleFile };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { JSDOM } from "jsdom";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import svgToTinyDataUri from "mini-svg-data-uri";
|
|
5
|
+
import mime from "mime";
|
|
6
|
+
//#region src/dataurl.ts
|
|
7
|
+
function bufferToDataURL(name, b) {
|
|
8
|
+
return /\.svg$/i.test(name) ? svgToTinyDataUri(b.toString()) : `data:${mime.getType(name)};base64,${b.toString("base64")}`;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/options.ts
|
|
12
|
+
function getInnerOptions(opt) {
|
|
13
|
+
opt ||= {};
|
|
14
|
+
return {
|
|
15
|
+
rename: opt.rename == null ? void 0 : String(opt.rename),
|
|
16
|
+
tryInlineHtmlAssets: opt.tryInlineHtmlAssets ?? true,
|
|
17
|
+
removeInlinedAssetFiles: opt.removeInlinedAssetFiles ?? true,
|
|
18
|
+
tryInlineHtmlPublicIcon: opt.tryInlineHtmlPublicIcon ?? true,
|
|
19
|
+
removeInlinedPublicIconFiles: opt.removeInlinedPublicIconFiles ?? true
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/cutPrefix.ts
|
|
24
|
+
function cutPrefix(str, prefix) {
|
|
25
|
+
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/index.ts
|
|
29
|
+
function singleFile(opt) {
|
|
30
|
+
let conf;
|
|
31
|
+
const innerOptions = getInnerOptions(opt);
|
|
32
|
+
return {
|
|
33
|
+
name: "@bddjr/vite-plugin-singlefile",
|
|
34
|
+
enforce: "post",
|
|
35
|
+
config(...args) {
|
|
36
|
+
return setConfig.call(this, innerOptions, ...args);
|
|
37
|
+
},
|
|
38
|
+
configResolved(c) {
|
|
39
|
+
conf = c;
|
|
40
|
+
},
|
|
41
|
+
generateBundle(outputOptions, bundle, isWrite) {
|
|
42
|
+
return generateBundle.call(this, bundle, conf, innerOptions);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function setConfig(opt, config, env) {
|
|
47
|
+
config.base ??= "./";
|
|
48
|
+
const build = config.build ??= {};
|
|
49
|
+
build.cssCodeSplit ??= false;
|
|
50
|
+
build.assetsInlineLimit ??= () => true;
|
|
51
|
+
build.modulePreload ?? build.polyfillModulePreload ?? (build.modulePreload = { polyfill: false });
|
|
52
|
+
if (this.meta.rolldownVersion) {
|
|
53
|
+
const rolldownOptions = build.rolldownOptions ?? build.rollupOptions ?? (build.rolldownOptions = {});
|
|
54
|
+
for (const output of [rolldownOptions.output ??= {}].flat(1)) output.codeSplitting ?? output.inlineDynamicImports ?? (output.codeSplitting = false);
|
|
55
|
+
} else {
|
|
56
|
+
const rollupOptions = build.rollupOptions ??= {};
|
|
57
|
+
for (const output of [rollupOptions.output ??= {}].flat(1)) output.inlineDynamicImports ??= true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function generateBundle(bundle, config, options) {
|
|
61
|
+
if (options.rename && options.rename != "index.html" && Object.prototype.hasOwnProperty.call(bundle, "index.html") && !Object.prototype.hasOwnProperty.call(bundle, options.rename)) {
|
|
62
|
+
bundle[options.rename] = bundle["index.html"];
|
|
63
|
+
bundle[options.rename].fileName = options.rename;
|
|
64
|
+
delete bundle["index.html"];
|
|
65
|
+
}
|
|
66
|
+
const assetsDir = path.posix.join(config.build.assetsDir, "/"), assetsDirWithBase = config.base + assetsDir, assetsHrefSelector = `[href^="${assetsDirWithBase}"]`, assetsSrcSelector = `[src^="${assetsDirWithBase}"]`, globalDelete = /* @__PURE__ */ new Set(), globalDoNotDelete = /* @__PURE__ */ new Set(), globalRemoveDistFileNames = /* @__PURE__ */ new Set(), globalAssetsDataURL = {}, globalPublicFilesCache = {}, bundleAssetsNames = [], bundleHTMLNames = [];
|
|
67
|
+
for (const name in bundle) if (name.startsWith(assetsDir)) bundleAssetsNames.push(name);
|
|
68
|
+
else if (/\.html$/i.test(name)) bundleHTMLNames.push(name);
|
|
69
|
+
for (const htmlFileName of bundleHTMLNames) {
|
|
70
|
+
const htmlChunk = bundle[htmlFileName], oldHTML = htmlChunk.source, dom = new JSDOM(oldHTML), document = dom.window.document, scriptElement = document.querySelector(`script[type=module]${assetsSrcSelector}`);
|
|
71
|
+
if (!scriptElement) continue;
|
|
72
|
+
const scriptName = scriptElement ? cutPrefix(scriptElement.src, config.base) : "", thisDel = /* @__PURE__ */ new Set();
|
|
73
|
+
scriptElement.remove();
|
|
74
|
+
scriptElement.removeAttribute("src");
|
|
75
|
+
scriptElement.removeAttribute("crossorigin");
|
|
76
|
+
document.body.appendChild(scriptElement);
|
|
77
|
+
let allCSS = "";
|
|
78
|
+
const linkStylesheet = document.querySelectorAll(`link[rel=stylesheet]${assetsHrefSelector}`);
|
|
79
|
+
for (const element of linkStylesheet) {
|
|
80
|
+
const name = cutPrefix(element.href, config.base);
|
|
81
|
+
thisDel.add(name);
|
|
82
|
+
const cssSource = bundle[name].source;
|
|
83
|
+
if (cssSource) {
|
|
84
|
+
for (const name of bundleAssetsNames) if (cssSource.includes(name.slice(assetsDir.length))) globalDoNotDelete.add(name);
|
|
85
|
+
allCSS += cssSource.replace(/\s*(\/\*[^*]*\*\/)?\s*$/, "");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (allCSS) {
|
|
89
|
+
const e = document.createElement("style");
|
|
90
|
+
e.innerHTML = allCSS;
|
|
91
|
+
linkStylesheet[0].before(e);
|
|
92
|
+
for (const e of linkStylesheet) e.remove();
|
|
93
|
+
}
|
|
94
|
+
const assetsDataURL = {};
|
|
95
|
+
if (options.tryInlineHtmlAssets) for (const element of document.querySelectorAll(assetsSrcSelector)) {
|
|
96
|
+
const name = cutPrefix(element.src, assetsDirWithBase);
|
|
97
|
+
if (/\.js$/i.test(name)) continue;
|
|
98
|
+
if (!Object.prototype.hasOwnProperty.call(assetsDataURL, name)) {
|
|
99
|
+
const bundleName = assetsDir + name;
|
|
100
|
+
const a = bundle[bundleName];
|
|
101
|
+
if (!a) continue;
|
|
102
|
+
thisDel.add(bundleName);
|
|
103
|
+
let dataURL;
|
|
104
|
+
if (Object.prototype.hasOwnProperty.call(globalAssetsDataURL, name)) dataURL = globalAssetsDataURL[name];
|
|
105
|
+
else globalAssetsDataURL[name] = dataURL = bufferToDataURL(name, Buffer.from(a.source));
|
|
106
|
+
element.src = dataURL;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (options.tryInlineHtmlPublicIcon) {
|
|
110
|
+
let needInline = true;
|
|
111
|
+
let iconName = "favicon.ico";
|
|
112
|
+
const element = document.querySelector(`link[rel=icon][href^="${config.base}"], link[rel="shortcut icon"][href^="${config.base}"]`);
|
|
113
|
+
if (element) {
|
|
114
|
+
iconName = cutPrefix(element.href, config.base);
|
|
115
|
+
if (bundleAssetsNames.includes(iconName)) needInline = false;
|
|
116
|
+
else {
|
|
117
|
+
element.rel = "icon";
|
|
118
|
+
element.href = "data:";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (needInline) try {
|
|
122
|
+
if (!Object.prototype.hasOwnProperty.call(globalPublicFilesCache, iconName)) {
|
|
123
|
+
let Path = path.join(config.build.outDir, iconName);
|
|
124
|
+
if (fs.existsSync(Path)) globalRemoveDistFileNames.add(iconName);
|
|
125
|
+
else Path = path.join(config.publicDir, iconName);
|
|
126
|
+
const b = fs.readFileSync(Path);
|
|
127
|
+
globalPublicFilesCache[iconName] = {
|
|
128
|
+
dataURL: bufferToDataURL(iconName, b),
|
|
129
|
+
size: b.length
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const { dataURL, size } = globalPublicFilesCache[iconName];
|
|
133
|
+
if (element) element.href = dataURL;
|
|
134
|
+
else {
|
|
135
|
+
const e = document.head.appendChild(document.createElement("link"));
|
|
136
|
+
e.rel = "icon";
|
|
137
|
+
e.href = dataURL;
|
|
138
|
+
}
|
|
139
|
+
} catch (e) {
|
|
140
|
+
if (element) console.error(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
thisDel.add(scriptName);
|
|
144
|
+
let { code } = bundle[scriptName];
|
|
145
|
+
code = code.replace(/;?\n?$/, "");
|
|
146
|
+
for (const name of bundleAssetsNames) {
|
|
147
|
+
const assetName = name.slice(assetsDir.length);
|
|
148
|
+
if (code.includes(assetName)) {
|
|
149
|
+
globalDoNotDelete.add(name);
|
|
150
|
+
delete assetsDataURL[assetName];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
let outputScript = code.replaceAll("<\/script", "<\\/script");
|
|
154
|
+
if (/\b__VITE_PRELOAD__\b/.test(code)) outputScript = "var __VITE_PRELOAD__;" + outputScript;
|
|
155
|
+
scriptElement.innerHTML = outputScript;
|
|
156
|
+
htmlChunk.source = dom.serialize();
|
|
157
|
+
for (const name of thisDel) globalDelete.add(name);
|
|
158
|
+
}
|
|
159
|
+
if (options.removeInlinedAssetFiles) {
|
|
160
|
+
for (const name of globalDelete) if (!globalDoNotDelete.has(name)) delete bundle[name];
|
|
161
|
+
}
|
|
162
|
+
if (options.removeInlinedPublicIconFiles) for (const name of globalRemoveDistFileNames) try {
|
|
163
|
+
fs.rmSync(path.join(config.build.outDir, name), { force: true });
|
|
164
|
+
} catch (e) {
|
|
165
|
+
console.error(e);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
export { singleFile as default, singleFile };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bddjr/vite-plugin-singlefile",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"author": "bddjr",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "Embed all assets into dist/index.html",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rimraf dist && tsc && node build.js && cd test && node --run build",
|
|
15
|
+
"prepublishOnly": "node --run build"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/bddjr/vite-plugin-singlefile.git"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"registry": "https://registry.npmjs.org"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"vite-plugin",
|
|
27
|
+
"vite",
|
|
28
|
+
"plugin",
|
|
29
|
+
"SFA",
|
|
30
|
+
"single-file",
|
|
31
|
+
"singlefile",
|
|
32
|
+
"single",
|
|
33
|
+
"gzip",
|
|
34
|
+
"inline",
|
|
35
|
+
"frontend",
|
|
36
|
+
"front-end",
|
|
37
|
+
"js",
|
|
38
|
+
"javascript",
|
|
39
|
+
"css",
|
|
40
|
+
"class",
|
|
41
|
+
"rolldown",
|
|
42
|
+
"rolldown-vite",
|
|
43
|
+
"rollup",
|
|
44
|
+
"vite-plugin-singlefile"
|
|
45
|
+
],
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@types/node": "*",
|
|
48
|
+
"jsdom": ">=28.1.0",
|
|
49
|
+
"mime": ">=4.1.0",
|
|
50
|
+
"mini-svg-data-uri": ">=1.4.4",
|
|
51
|
+
"vite": ">=3.0.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@bddjr/rimraf": "^0.1.0",
|
|
55
|
+
"@types/jsdom": ">=28.0.1",
|
|
56
|
+
"@types/node": "^22.19.15",
|
|
57
|
+
"rolldown": ">=1.0.0-rc.12",
|
|
58
|
+
"rolldown-plugin-dts": "^0.23.2",
|
|
59
|
+
"rollup": ">=4.60.0",
|
|
60
|
+
"typescript": ">=6.0.2",
|
|
61
|
+
"vite": ">=8.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|