vite-plugin-singlefile-compression 1.2.4 → 1.2.6

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.
Files changed (3) hide show
  1. package/README.md +51 -56
  2. package/dist/index.js +17 -9
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -4,7 +4,7 @@ Compress all assets and embeds them into `dist/index.html`, making it convenient
4
4
 
5
5
  The recipient can open it directly in the browser without manually unzipping the file.
6
6
 
7
- Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-singlefile)
7
+ Using [DecompressionStream](https://developer.mozilla.org/docs/Web/API/DecompressionStream) + [base128-ascii](https://www.npmjs.com/package/base128-ascii).
8
8
 
9
9
  ## Setup
10
10
 
@@ -12,13 +12,12 @@ Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-
12
12
  npm i vite-plugin-singlefile-compression
13
13
  ```
14
14
 
15
- Then modify [vite.config.ts](test/vite.config.ts#L14)
15
+ Then modify `vite.config.ts`, see [test/vite.config.ts](test/vite.config.ts)
16
16
 
17
17
  ```ts
18
18
  // Import singleFileCompression
19
19
  import singleFileCompression from 'vite-plugin-singlefile-compression'
20
20
 
21
- // https://vite.dev/config/
22
21
  export default defineConfig({
23
22
  plugins: [
24
23
  vue(),
@@ -26,26 +25,20 @@ export default defineConfig({
26
25
  // Add singleFileCompression
27
26
  singleFileCompression(),
28
27
  ],
29
- esbuild: {
30
- // Remove license comments
31
- legalComments: "none"
32
- },
28
+
33
29
  build: {
34
- terserOptions: {
35
- format: {
36
- // Remove license comments
37
- comments: false
38
- }
39
- },
30
+ // Not use old syntax, make file smaller.
40
31
  target: 'esnext',
32
+ // Disable reporting compressed chunk sizes, slightly improve build speed.
41
33
  reportCompressedSize: false
42
34
  },
43
35
  ```
44
36
 
45
- Then modify [src/router/index.ts](test/src/router/index.ts#L5) , change `createWebHistory` to `createWebHashHistory`
37
+ Then modify [src/router/index.ts](test/src/router/index.ts#L5)
46
38
 
47
39
  ```ts
48
40
  const router = createRouter({
41
+ // Use Hash History
49
42
  history: createWebHashHistory(),
50
43
  ```
51
44
 
@@ -53,43 +46,43 @@ const router = createRouter({
53
46
 
54
47
  ```ts
55
48
  export interface Options {
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
- * https://www.npmjs.com/package/base128-ascii
90
- * @default true
91
- */
92
- useBase128?: boolean
49
+ /**
50
+ * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
51
+ * @default defaultHtmlMinifierTerserOptions
52
+ */
53
+ htmlMinifierTerser?: htmlMinifierOptions | boolean;
54
+
55
+ /**
56
+ * Try inline html used assets, if inlined or not used in JS.
57
+ * @default true
58
+ */
59
+ tryInlineHtmlAssets?: boolean;
60
+
61
+ /**
62
+ * Remove inlined asset files.
63
+ * @default true
64
+ */
65
+ removeInlinedAssetFiles?: boolean;
66
+
67
+ /**
68
+ * Try inline html icon, if icon is in public dir.
69
+ * @default true
70
+ */
71
+ tryInlineHtmlPublicIcon?: boolean;
72
+
73
+ /**
74
+ * Remove inlined html icon files.
75
+ * @default true
76
+ */
77
+ removeInlinedPublicIconFiles?: boolean;
78
+
79
+ /**
80
+ * Use Base128 to encode gzipped script.
81
+ * If false, use Base64.
82
+ * https://www.npmjs.com/package/base128-ascii
83
+ * @default true
84
+ */
85
+ useBase128?: boolean;
93
86
  }
94
87
  ```
95
88
 
@@ -98,19 +91,19 @@ export interface Options {
98
91
  https://bddjr.github.io/vite-plugin-singlefile-compression/
99
92
 
100
93
  ```
101
- vite v6.0.6 building for production...
94
+ vite v6.0.7 building for production...
102
95
  ✓ 45 modules transformed.
103
96
  rendering chunks (1)...
104
97
 
105
- vite-plugin-singlefile-compression 1.2.4 building...
98
+ vite-plugin-singlefile-compression 1.2.6 building...
106
99
 
107
100
  file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
108
- 101.02 KiB -> 46.52 KiB
101
+ 101.56 KiB -> 46.84 KiB
109
102
 
110
103
  Finish.
111
104
 
112
- dist/index.html 47.64 kB
113
- ✓ built in 690ms
105
+ dist/index.html 47.96 kB
106
+ ✓ built in 686ms
114
107
  ```
115
108
 
116
109
  ## Clone
@@ -128,3 +121,5 @@ npm run build
128
121
  ## License
129
122
 
130
123
  [MIT](LICENSE.txt)
124
+
125
+ Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-singlefile).
package/dist/index.js CHANGED
@@ -62,16 +62,22 @@ function KiB(size) {
62
62
  }
63
63
  function setConfig(config) {
64
64
  config.base = './';
65
- if (!config.build)
66
- config.build = {};
65
+ config.build ??= {};
67
66
  config.build.assetsInlineLimit = () => true;
68
67
  config.build.chunkSizeWarningLimit = Infinity;
69
68
  config.build.cssCodeSplit = false;
70
69
  config.build.assetsDir = 'assets';
71
70
  config.build.modulePreload = { polyfill: false };
72
- if (!config.build.rollupOptions)
73
- config.build.rollupOptions = {};
74
- config.build.rollupOptions.output = { inlineDynamicImports: true };
71
+ config.build.rollupOptions ??= {};
72
+ config.build.rollupOptions.output ??= {};
73
+ if (Array.isArray(config.build.rollupOptions.output)) {
74
+ for (const output of config.build.rollupOptions.output) {
75
+ output.inlineDynamicImports = true;
76
+ }
77
+ }
78
+ else {
79
+ config.build.rollupOptions.output.inlineDynamicImports = true;
80
+ }
75
81
  }
76
82
  async function generateBundle(bundle, config, options) {
77
83
  console.log(pc.cyan('\n\nvite-plugin-singlefile-compression ' + version) + pc.green(' building...'));
@@ -79,7 +85,7 @@ async function generateBundle(bundle, config, options) {
79
85
  return console.error("error: config.base has been changed!");
80
86
  if (config.build.assetsDir !== 'assets')
81
87
  return console.error("error: config.build.assetsDir has been changed!");
82
- const distURL = (u => u.endsWith('/') ? u : u + '/')(pathToFileURL(path.resolve(config.build.outDir)).href);
88
+ const distURL = pathToFileURL(path.resolve(config.build.outDir)).href + '/';
83
89
  const globalDelete = new Set();
84
90
  const globalDoNotDelete = new Set();
85
91
  const globalRemoveDistFileNames = new Set();
@@ -103,7 +109,9 @@ async function generateBundle(bundle, config, options) {
103
109
  const thisDel = new Set();
104
110
  // Fix async import
105
111
  const newJSCode = ["self.__VITE_PRELOAD__=void 0"];
106
- newJSCode.toString = () => newJSCode.join(';');
112
+ newJSCode.toString = function () {
113
+ return this.join(';');
114
+ };
107
115
  // remove html comments
108
116
  newHtml = newHtml.replaceAll(/<!--[\d\D]*?-->/g, '');
109
117
  // get css tag
@@ -232,9 +240,9 @@ async function generateBundle(bundle, config, options) {
232
240
  if (options.htmlMinifierTerser)
233
241
  newHtml = await htmlMinify(newHtml, options.htmlMinifierTerser);
234
242
  // fill script
235
- newHtml = newHtml.split('self.__vitePluginSinglefileCompression=1', 2).join(options.useBase128
243
+ newHtml = newHtml.split('self.__vitePluginSinglefileCompression=1', 2).join('\n//vite-plugin-singlefile-compression\n' + (options.useBase128
236
244
  ? templateBase128.join(gzipToBase128(newJSCode.toString()))
237
- : template.join(gzipToBase64(newJSCode.toString())));
245
+ : template.join(gzipToBase64(newJSCode.toString()))));
238
246
  // finish
239
247
  htmlChunk.source = newHtml;
240
248
  console.log("\n"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-singlefile-compression",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "files": [
@@ -42,7 +42,7 @@
42
42
  "dependencies": {
43
43
  "@types/html-minifier-terser": "^7.0.2",
44
44
  "@types/node": "^22.9.3",
45
- "base128-ascii": "^2.0.1",
45
+ "base128-ascii": "^2.0.3",
46
46
  "esbuild": "^0.24.0",
47
47
  "html-minifier-terser": "^7.2.0",
48
48
  "mime": "^4.0.4",
@@ -50,6 +50,6 @@
50
50
  "picocolors": "^1.1.1",
51
51
  "rimraf": "^6.0.1",
52
52
  "typescript": "^5.7.2",
53
- "vite": "^6.0.6"
53
+ "vite": "^6.0.7"
54
54
  }
55
55
  }