vite-plugin-singlefile-compression 1.1.4 → 1.2.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 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
- ### README Language
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
- * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
65
- * @default defaultHtmlMinifierTerserOptions
66
- */
67
- htmlMinifierTerser?: htmlMinifierOptions | boolean;
68
-
69
- /**
70
- * Try inline html used assets, if inlined or not used in JS.
71
- * @default true
72
- */
73
- tryInlineHtmlAssets?: boolean;
74
-
75
- /**
76
- * Remove inlined asset files.
77
- * @default true
78
- */
79
- removeInlinedAssetFiles?: boolean;
80
-
81
- /**
82
- * Try inline html icon, if icon is in public dir.
83
- * @default true
84
- */
85
- tryInlineHtmlPublicIcon?: boolean;
86
-
87
- /**
88
- * Remove inlined html icon files.
89
- * @default true
90
- */
91
- removeInlinedPublicIconFiles?: boolean;
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.1.3 building...
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 -> 52.1 KiB
107
+ 101.02 KiB -> 46.81 KiB
108
108
 
109
109
  Finish.
110
110
 
111
- dist/index.html 53.35 kB
112
- ✓ built in 734ms
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';
@@ -31,7 +32,10 @@ export function singleFileCompression(opt) {
31
32
  : opt.tryInlineHtmlPublicIcon,
32
33
  removeInlinedPublicIconFiles: opt.removeInlinedPublicIconFiles == null
33
34
  ? true
34
- : opt.removeInlinedPublicIconFiles
35
+ : opt.removeInlinedPublicIconFiles,
36
+ useBase128: opt.useBase128 == null
37
+ ? true
38
+ : opt.useBase128,
35
39
  };
36
40
  let conf;
37
41
  return {
@@ -44,6 +48,7 @@ export function singleFileCompression(opt) {
44
48
  }
45
49
  export default singleFileCompression;
46
50
  const template = fs.readFileSync(path.join(import.meta.dirname, "template.js")).toString().split('{<script>}', 2);
51
+ const templateBase128 = fs.readFileSync(path.join(import.meta.dirname, "template-base128.js")).toString().split('"<script>"', 2);
47
52
  const templateAssets = fs.readFileSync(path.join(import.meta.dirname, "template-assets.js")).toString().split('{"":""}', 2);
48
53
  const { version } = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, "../package.json")).toString());
49
54
  function bufferToDataURL(name, b) {
@@ -56,6 +61,11 @@ function gzipToBase64(buf) {
56
61
  level: zlib.constants.Z_BEST_COMPRESSION,
57
62
  }).toString('base64');
58
63
  }
64
+ function gzipToBase128(buf) {
65
+ return base128.encodeToTemplateLiterals(Uint8Array.from(zlib.gzipSync(buf, {
66
+ level: zlib.constants.Z_BEST_COMPRESSION,
67
+ })));
68
+ }
59
69
  function KiB(size) {
60
70
  return `${Math.ceil(size / 10.24) / 100} KiB`;
61
71
  }
@@ -222,16 +232,18 @@ async function generateBundle(bundle, config, options) {
222
232
  }
223
233
  // add script
224
234
  newJSCode.push(js.code.replace(/;?\n?$/, ''));
225
- // gzip
226
- return '<script type="module">'
227
- + template.join(gzipToBase64(newJSCode.toString()))
228
- + '</script>';
235
+ // fill fake script
236
+ return '<script type="module">self.__vitePluginSinglefileCompression=1</script>';
229
237
  });
230
238
  if (!ok)
231
239
  continue;
232
240
  // minify html
233
241
  if (options.htmlMinifierTerser)
234
242
  newHtml = await htmlMinify(newHtml, options.htmlMinifierTerser);
243
+ // fill script
244
+ newHtml = newHtml.split('self.__vitePluginSinglefileCompression=1', 2).join(options.useBase128
245
+ ? templateBase128.join(gzipToBase128(newJSCode.toString()))
246
+ : template.join(gzipToBase64(newJSCode.toString())));
235
247
  // finish
236
248
  htmlChunk.source = newHtml;
237
249
  console.log("\n"
@@ -0,0 +1 @@
1
+ function l(o){let n=new TextEncoder().encode(o),t=new Uint8Array(Math.ceil((n.length-1)/8)*7);for(let r=1,i=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[i++]=Number(e>>48n),t[i++]=Number(e>>40n&255n),t[i++]=Number(e>>32n&255n),t[i++]=Number(e>>24n&255n),t[i++]=Number(e>>16n&255n),t[i++]=Number(e>>8n&255n),t[i++]=Number(e&255n)}let c=n[0]-48;return c?t.slice(0,c-7):t}new Response(new ReadableStream({start(o){o.enqueue(l("<script>")),o.close()}}).pipeThrough(new DecompressionStream("gzip")),{headers:{"Content-Type":"text/javascript"}}).blob().then(o=>import(o=URL.createObjectURL(o)).finally(()=>URL.revokeObjectURL(o)))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-singlefile-compression",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
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": "^0.0.1",
45
46
  "esbuild": "^0.24.0",
46
47
  "html-minifier-terser": "^7.2.0",
47
48
  "mime": "^4.0.4",