vite-plugin-singlefile-compression 1.1.4 → 1.2.1
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 +1 -1
- package/README.md +41 -41
- package/dist/index.d.ts +6 -0
- package/dist/index.js +21 -18
- package/dist/template-base128.js +1 -0
- package/package.json +2 -1
package/LICENSE.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
|
-
Copyright © 2024 bddjr
|
|
2
|
+
Copyright © 2024-2025 bddjr
|
|
3
3
|
|
|
4
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
5
|
|
package/README.md
CHANGED
|
@@ -6,14 +6,7 @@ The recipient can open it directly in the browser without manually unzipping the
|
|
|
6
6
|
|
|
7
7
|
Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-singlefile)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
> English
|
|
12
|
-
> [简体中文](README-zh-CN.md)
|
|
13
|
-
|
|
14
|
-
## Install
|
|
15
|
-
|
|
16
|
-
Using `npm` to install
|
|
9
|
+
## Setup
|
|
17
10
|
|
|
18
11
|
```
|
|
19
12
|
npm i vite-plugin-singlefile-compression
|
|
@@ -60,35 +53,42 @@ const router = createRouter({
|
|
|
60
53
|
|
|
61
54
|
```ts
|
|
62
55
|
export interface Options {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
56
|
+
/**
|
|
57
|
+
* https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
|
|
58
|
+
* @default defaultHtmlMinifierTerserOptions
|
|
59
|
+
*/
|
|
60
|
+
htmlMinifierTerser?: htmlMinifierOptions | boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Try inline html used assets, if inlined or not used in JS.
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
tryInlineHtmlAssets?: boolean;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Remove inlined asset files.
|
|
70
|
+
* @default true
|
|
71
|
+
*/
|
|
72
|
+
removeInlinedAssetFiles?: boolean;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Try inline html icon, if icon is in public dir.
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
tryInlineHtmlPublicIcon?: boolean;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Remove inlined html icon files.
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
removeInlinedPublicIconFiles?: boolean;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Use Base128 to encode gzipped script.
|
|
88
|
+
* If false, use Base64.
|
|
89
|
+
* @default true
|
|
90
|
+
*/
|
|
91
|
+
useBase128?: boolean
|
|
92
92
|
}
|
|
93
93
|
```
|
|
94
94
|
|
|
@@ -101,15 +101,15 @@ vite v6.0.6 building for production...
|
|
|
101
101
|
✓ 45 modules transformed.
|
|
102
102
|
rendering chunks (1)...
|
|
103
103
|
|
|
104
|
-
vite-plugin-singlefile-compression 1.
|
|
104
|
+
vite-plugin-singlefile-compression 1.2.0 building...
|
|
105
105
|
|
|
106
106
|
file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
|
|
107
|
-
101.02 KiB ->
|
|
107
|
+
101.02 KiB -> 46.81 KiB
|
|
108
108
|
|
|
109
109
|
Finish.
|
|
110
110
|
|
|
111
|
-
dist/index.html
|
|
112
|
-
✓ built in
|
|
111
|
+
dist/index.html 47.93 kB
|
|
112
|
+
✓ built in 687ms
|
|
113
113
|
```
|
|
114
114
|
|
|
115
115
|
## Clone
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface Options {
|
|
|
26
26
|
* @default true
|
|
27
27
|
*/
|
|
28
28
|
removeInlinedPublicIconFiles?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Use Base128 to encode gzipped script.
|
|
31
|
+
* If false, use Base64.
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
useBase128?: boolean;
|
|
29
35
|
}
|
|
30
36
|
export declare const defaultHtmlMinifierTerserOptions: htmlMinifierOptions;
|
|
31
37
|
export declare function singleFileCompression(opt?: Options): PluginOption;
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import mime from 'mime';
|
|
|
2
2
|
import pc from "picocolors";
|
|
3
3
|
import svgToTinyDataUri from "mini-svg-data-uri";
|
|
4
4
|
import { minify as htmlMinify } from 'html-minifier-terser';
|
|
5
|
+
import base128 from "base128-ascii";
|
|
5
6
|
import zlib from 'zlib';
|
|
6
7
|
import path from 'path';
|
|
7
8
|
import fs from 'fs';
|
|
@@ -20,18 +21,11 @@ export function singleFileCompression(opt) {
|
|
|
20
21
|
htmlMinifierTerser: opt.htmlMinifierTerser == null || opt.htmlMinifierTerser === true
|
|
21
22
|
? defaultHtmlMinifierTerserOptions
|
|
22
23
|
: opt.htmlMinifierTerser,
|
|
23
|
-
tryInlineHtmlAssets: opt.tryInlineHtmlAssets
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
: opt.removeInlinedAssetFiles,
|
|
29
|
-
tryInlineHtmlPublicIcon: opt.tryInlineHtmlPublicIcon == null
|
|
30
|
-
? true
|
|
31
|
-
: opt.tryInlineHtmlPublicIcon,
|
|
32
|
-
removeInlinedPublicIconFiles: opt.removeInlinedPublicIconFiles == null
|
|
33
|
-
? true
|
|
34
|
-
: opt.removeInlinedPublicIconFiles
|
|
24
|
+
tryInlineHtmlAssets: opt.tryInlineHtmlAssets ?? true,
|
|
25
|
+
removeInlinedAssetFiles: opt.removeInlinedAssetFiles ?? true,
|
|
26
|
+
tryInlineHtmlPublicIcon: opt.tryInlineHtmlPublicIcon ?? true,
|
|
27
|
+
removeInlinedPublicIconFiles: opt.removeInlinedPublicIconFiles ?? true,
|
|
28
|
+
useBase128: opt.useBase128 ?? true,
|
|
35
29
|
};
|
|
36
30
|
let conf;
|
|
37
31
|
return {
|
|
@@ -44,6 +38,7 @@ export function singleFileCompression(opt) {
|
|
|
44
38
|
}
|
|
45
39
|
export default singleFileCompression;
|
|
46
40
|
const template = fs.readFileSync(path.join(import.meta.dirname, "template.js")).toString().split('{<script>}', 2);
|
|
41
|
+
const templateBase128 = fs.readFileSync(path.join(import.meta.dirname, "template-base128.js")).toString().split('"<script>"', 2);
|
|
47
42
|
const templateAssets = fs.readFileSync(path.join(import.meta.dirname, "template-assets.js")).toString().split('{"":""}', 2);
|
|
48
43
|
const { version } = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, "../package.json")).toString());
|
|
49
44
|
function bufferToDataURL(name, b) {
|
|
@@ -51,10 +46,16 @@ function bufferToDataURL(name, b) {
|
|
|
51
46
|
? svgToTinyDataUri(b.toString())
|
|
52
47
|
: `data:${mime.getType(name)};base64,${b.toString('base64')}`;
|
|
53
48
|
}
|
|
54
|
-
function
|
|
49
|
+
function gzip(buf) {
|
|
55
50
|
return zlib.gzipSync(buf, {
|
|
56
51
|
level: zlib.constants.Z_BEST_COMPRESSION,
|
|
57
|
-
})
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function gzipToBase64(buf) {
|
|
55
|
+
return gzip(buf).toString('base64');
|
|
56
|
+
}
|
|
57
|
+
function gzipToBase128(buf) {
|
|
58
|
+
return base128.encode(Uint8Array.from(gzip(buf))).toJSTemplateLiterals();
|
|
58
59
|
}
|
|
59
60
|
function KiB(size) {
|
|
60
61
|
return `${Math.ceil(size / 10.24) / 100} KiB`;
|
|
@@ -222,16 +223,18 @@ async function generateBundle(bundle, config, options) {
|
|
|
222
223
|
}
|
|
223
224
|
// add script
|
|
224
225
|
newJSCode.push(js.code.replace(/;?\n?$/, ''));
|
|
225
|
-
//
|
|
226
|
-
return '<script type="module">'
|
|
227
|
-
+ template.join(gzipToBase64(newJSCode.toString()))
|
|
228
|
-
+ '</script>';
|
|
226
|
+
// fill fake script
|
|
227
|
+
return '<script type="module">self.__vitePluginSinglefileCompression=1</script>';
|
|
229
228
|
});
|
|
230
229
|
if (!ok)
|
|
231
230
|
continue;
|
|
232
231
|
// minify html
|
|
233
232
|
if (options.htmlMinifierTerser)
|
|
234
233
|
newHtml = await htmlMinify(newHtml, options.htmlMinifierTerser);
|
|
234
|
+
// fill script
|
|
235
|
+
newHtml = newHtml.split('self.__vitePluginSinglefileCompression=1', 2).join(options.useBase128
|
|
236
|
+
? templateBase128.join(gzipToBase128(newJSCode.toString()))
|
|
237
|
+
: template.join(gzipToBase64(newJSCode.toString())));
|
|
235
238
|
// finish
|
|
236
239
|
htmlChunk.source = newHtml;
|
|
237
240
|
console.log("\n"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function l(i){let n=new TextEncoder().encode(i),t=new Uint8Array(Math.ceil((n.length-1)/8)*7);for(let r=1,o=0;r<n.length;){let e=BigInt(n[r++]||0)<<49n;e|=BigInt(n[r++]||0)<<42n,e|=BigInt(n[r++]||0)<<35n,e|=BigInt(n[r++]||0)<<28n,e|=BigInt(n[r++]||0)<<21n,e|=BigInt(n[r++]||0)<<14n,e|=BigInt(n[r++]||0)<<7n,e|=BigInt(n[r++]||0),t[o++]=Number(e>>48n),t[o++]=Number(e>>40n&255n),t[o++]=Number(e>>32n&255n),t[o++]=Number(e>>24n&255n),t[o++]=Number(e>>16n&255n),t[o++]=Number(e>>8n&255n),t[o++]=Number(e&255n)}let u=n[0]-48;return u?t.slice(0,u-7):t}new Response(new ReadableStream({start(i){i.enqueue(l("<script>")),i.close()}}).pipeThrough(new DecompressionStream("gzip")),{headers:{"Content-Type":"text/javascript"}}).blob().then(i=>import(i=URL.createObjectURL(i)).finally(()=>URL.revokeObjectURL(i)))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-singlefile-compression",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@types/html-minifier-terser": "^7.0.2",
|
|
44
44
|
"@types/node": "^22.9.3",
|
|
45
|
+
"base128-ascii": "^1.0.0",
|
|
45
46
|
"esbuild": "^0.24.0",
|
|
46
47
|
"html-minifier-terser": "^7.2.0",
|
|
47
48
|
"mime": "^4.0.4",
|