vite-plugin-singlefile-compression 2.0.5 → 2.0.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.
package/README.md CHANGED
@@ -49,54 +49,60 @@ More info see [src/options.ts](src/options.ts)
49
49
 
50
50
  ```ts
51
51
  export interface Options {
52
- /**
53
- * Rename index.html
54
- */
55
- rename?: string;
56
-
57
- /**
58
- * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
59
- * @default defaultHtmlMinifierTerserOptions
60
- */
61
- htmlMinifierTerser?: htmlMinifierOptions | boolean;
62
-
63
- /**
64
- * Try inline html used assets, if inlined or not used in JS.
65
- * @default true
66
- */
67
- tryInlineHtmlAssets?: boolean;
68
-
69
- /**
70
- * Remove inlined asset files.
71
- * @default true
72
- */
73
- removeInlinedAssetFiles?: boolean;
74
-
75
- /**
76
- * Try inline html icon, if icon is in public dir.
77
- * @default true
78
- */
79
- tryInlineHtmlPublicIcon?: boolean;
80
-
81
- /**
82
- * Remove inlined html icon files.
83
- * @default true
84
- */
85
- removeInlinedPublicIconFiles?: boolean;
86
-
87
- /**
88
- * Use Base128 to encode gzipped script.
89
- * If false, use Base64.
90
- * https://www.npmjs.com/package/base128-ascii
91
- * @default true
92
- */
93
- useBase128?: boolean;
94
-
95
- /**
96
- * Compress format.
97
- * @default "deflate-raw"
98
- */
99
- compressFormat?: compressFormat;
52
+ /**
53
+ * Rename index.html
54
+ */
55
+ rename?: string
56
+
57
+ /**
58
+ * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
59
+ * @default defaultHtmlMinifierTerserOptions
60
+ */
61
+ htmlMinifierTerser?: htmlMinifierOptions | boolean
62
+
63
+ /**
64
+ * Try inline html used assets, if inlined or not used in JS.
65
+ * @default true
66
+ */
67
+ tryInlineHtmlAssets?: boolean
68
+
69
+ /**
70
+ * Remove inlined asset files.
71
+ * @default true
72
+ */
73
+ removeInlinedAssetFiles?: boolean
74
+
75
+ /**
76
+ * Try inline html icon, if icon is in public dir.
77
+ * @default true
78
+ */
79
+ tryInlineHtmlPublicIcon?: boolean
80
+
81
+ /**
82
+ * Remove inlined html icon files.
83
+ * @default true
84
+ */
85
+ removeInlinedPublicIconFiles?: boolean
86
+
87
+ /**
88
+ * Use Base128 to encode gzipped script.
89
+ * If false, use Base64.
90
+ * https://www.npmjs.com/package/base128-ascii
91
+ * @default true
92
+ */
93
+ useBase128?: boolean
94
+
95
+ /**
96
+ * Compress format.
97
+ * https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream/DecompressionStream
98
+ * @default "deflate-raw"
99
+ */
100
+ compressFormat?: compressFormat
101
+
102
+ /**
103
+ * Custom compressor.
104
+ */
105
+ compressor?: compressor
100
106
  }
101
107
  ```
102
108
 
@@ -105,19 +111,19 @@ export interface Options {
105
111
  https://bddjr.github.io/vite-plugin-singlefile-compression/
106
112
 
107
113
  ```
108
- vite v6.2.6 building for production...
109
- 45 modules transformed.
114
+ vite v7.3.1 building client environment for production...
115
+ 46 modules transformed.
110
116
  rendering chunks (1)...
111
117
 
112
- vite-plugin-singlefile-compression 2.0.5 building...
118
+ vite-plugin-singlefile-compression 2.0.6 building...
113
119
 
114
120
  file:///D:/code/js/vite-plugin-singlefile-compression/test/dist/index.html
115
- 101.63 KiB -> 46.42 KiB
121
+ 107.124 kB -> 48.342 kB
116
122
 
117
123
  Finish.
118
124
 
119
- dist/index.html 47.53 kB
120
- ✓ built in 770ms
125
+ dist/index.html 48.34 kB
126
+ ✓ built in 783ms
121
127
  ```
122
128
 
123
129
  ![](effect.jpg)
@@ -131,5 +137,5 @@ npm i
131
137
  cd test
132
138
  npm i
133
139
  cd ..
134
- npm run build
140
+ node --run build
135
141
  ```
@@ -1,3 +1,12 @@
1
1
  import zlib from 'zlib';
2
- export type compressFormat = "deflate-raw" | "deflate" | "gzip";
3
- export declare function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean): string;
2
+ declare const compressors: {
3
+ "deflate-raw"(buf: zlib.InputType): Buffer;
4
+ deflate(buf: zlib.InputType): Buffer;
5
+ gzip(buf: zlib.InputType): Buffer;
6
+ brotli: typeof zlib.brotliCompressSync;
7
+ zstd: typeof zlib.zstdCompressSync;
8
+ };
9
+ export type compressor = ((buf: zlib.InputType) => Buffer);
10
+ export type compressFormat = keyof typeof compressors;
11
+ export declare function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean, compressor: compressor): string;
12
+ export {};
package/dist/compress.js CHANGED
@@ -1,24 +1,33 @@
1
1
  import base128 from "base128-ascii";
2
2
  import zlib from 'zlib';
3
- export function compress(format, buf, useBase128) {
4
- const options = {
5
- level: zlib.constants.Z_BEST_COMPRESSION,
6
- };
7
- let outBuf;
8
- switch (format) {
9
- case "deflate":
10
- outBuf = zlib.deflateSync(buf, options);
11
- break;
12
- case "deflate-raw":
13
- outBuf = zlib.deflateRawSync(buf, options);
14
- break;
15
- case "gzip":
16
- outBuf = zlib.gzipSync(buf, options);
17
- break;
18
- default:
19
- throw `unknown compress format ${format}`;
3
+ const zlibDefaultOptions = {
4
+ level: zlib.constants.Z_BEST_COMPRESSION,
5
+ };
6
+ const compressors = {
7
+ "deflate-raw"(buf) {
8
+ return zlib.deflateRawSync(buf, zlibDefaultOptions);
9
+ },
10
+ deflate(buf) {
11
+ return zlib.deflateSync(buf, zlibDefaultOptions);
12
+ },
13
+ gzip(buf) {
14
+ return zlib.gzipSync(buf, zlibDefaultOptions);
15
+ },
16
+ brotli: zlib.brotliCompressSync,
17
+ zstd: zlib.zstdCompressSync,
18
+ };
19
+ function switchCompressor(format) {
20
+ const f = compressors[format] || zlib[format + 'CompressSync'];
21
+ if (!f) {
22
+ if (Object.prototype.hasOwnProperty.call(compressors, format))
23
+ throw Error(`Could not get compressor: Please upgrade node.js or set your compressor function.`);
24
+ throw Error(`Could not get compressor: Unknown compress format '${format}', please set your compressor function.`);
20
25
  }
26
+ return f;
27
+ }
28
+ export function compress(format, buf, useBase128, compressor) {
29
+ const outBuf = compressor ? compressor(buf) : switchCompressor(format)(buf);
21
30
  return useBase128
22
- ? base128.encode(Uint8Array.from(outBuf)).toJSTemplateLiterals()
31
+ ? base128.encode(outBuf).toJSTemplateLiterals()
23
32
  : outBuf.toString('base64');
24
33
  }
@@ -1,6 +1,6 @@
1
- import { compressFormat } from './compress.js';
1
+ import { compressFormat, compressor } from './compress.js';
2
2
  export declare const template: {
3
- base(script: string, format: compressFormat, useBase128: boolean): string;
3
+ base(script: string, format: compressFormat, useBase128: boolean, compressor: compressor): string;
4
4
  assets(assetsJSON: string): string;
5
5
  css(cssSource: string): string;
6
6
  icon(dataURL: string): string;
@@ -5,9 +5,9 @@ function r(name) {
5
5
  return fs.readFileSync(fileURLToPath(import.meta.resolve(`./template/${name}.js`))).toString();
6
6
  }
7
7
  function rt(name, template) {
8
- const s = r(name).split(template, 2);
8
+ const s = r(name).split(template);
9
9
  if (s.length !== 2)
10
- throw "s.length!==2";
10
+ throw Error("s.length!==2");
11
11
  return s;
12
12
  }
13
13
  const files = {
@@ -19,8 +19,8 @@ const files = {
19
19
  importmeta: rt('importmeta', '"<path>"'),
20
20
  };
21
21
  export const template = {
22
- base(script, format, useBase128) {
23
- script = compress(format, script, useBase128);
22
+ base(script, format, useBase128, compressor) {
23
+ script = compress(format, script, useBase128, compressor);
24
24
  if (useBase128) {
25
25
  return files.base128
26
26
  .replace("<format>", format)
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { pathToFileURL } from "url";
7
7
  import { version } from './getVersion.js';
8
8
  import { template } from './getTemplate.js';
9
9
  import { bufferToDataURL } from "./dataurl.js";
10
- import { KiB } from "./KiB.js";
10
+ import { kB } from "./kB.js";
11
11
  import { getInnerOptions } from "./options.js";
12
12
  import { cutPrefix } from "./cutPrefix.js";
13
13
  export function singleFileCompression(opt) {
@@ -22,16 +22,17 @@ export function singleFileCompression(opt) {
22
22
  }
23
23
  export default singleFileCompression;
24
24
  function setConfig(config) {
25
- config.base ??= './';
26
- config.build ??= {};
27
- config.build.cssCodeSplit ??= false;
28
- config.build.assetsInlineLimit ??= () => true;
29
- config.build.chunkSizeWarningLimit ??= Number.MAX_SAFE_INTEGER;
30
- config.build.modulePreload ??= { polyfill: false };
31
- config.build.reportCompressedSize ??= false;
32
- config.build.rollupOptions ??= {};
33
- for (const output of [config.build.rollupOptions.output ??= {}].flat(1)) {
34
- output.inlineDynamicImports ??= true;
25
+ var _a, _b, _c, _d, _e, _f, _g;
26
+ config.base ?? (config.base = './');
27
+ config.build ?? (config.build = {});
28
+ (_a = config.build).cssCodeSplit ?? (_a.cssCodeSplit = false);
29
+ (_b = config.build).assetsInlineLimit ?? (_b.assetsInlineLimit = () => true);
30
+ (_c = config.build).chunkSizeWarningLimit ?? (_c.chunkSizeWarningLimit = Number.MAX_SAFE_INTEGER);
31
+ (_d = config.build).modulePreload ?? (_d.modulePreload = { polyfill: false });
32
+ (_e = config.build).reportCompressedSize ?? (_e.reportCompressedSize = false);
33
+ (_f = config.build).rollupOptions ?? (_f.rollupOptions = {});
34
+ for (const output of [(_g = config.build.rollupOptions).output ?? (_g.output = {})].flat(1)) {
35
+ output.inlineDynamicImports ?? (output.inlineDynamicImports = true);
35
36
  }
36
37
  }
37
38
  async function generateBundle(bundle, config, options) {
@@ -107,14 +108,14 @@ async function generateBundle(bundle, config, options) {
107
108
  const name = cutPrefix(element.src, assetsDirWithBase);
108
109
  if (name.endsWith('.js'))
109
110
  continue;
110
- if (!Object.hasOwn(assetsDataURL, name)) {
111
+ if (!Object.prototype.hasOwnProperty.call(assetsDataURL, name)) {
111
112
  const bundleName = assetsDir + name;
112
113
  const a = bundle[bundleName];
113
114
  if (!a)
114
115
  continue;
115
116
  thisDel.add(bundleName);
116
117
  oldSize += a.source.length;
117
- if (!Object.hasOwn(globalAssetsDataURL, name))
118
+ if (!Object.prototype.hasOwnProperty.call(globalAssetsDataURL, name))
118
119
  globalAssetsDataURL[name] = bufferToDataURL(name, Buffer.from(
119
120
  //@ts-ignore
120
121
  a.source));
@@ -142,7 +143,7 @@ async function generateBundle(bundle, config, options) {
142
143
  if (needInline) {
143
144
  // inline
144
145
  try {
145
- if (!Object.hasOwn(globalPublicFilesCache, iconName)) {
146
+ if (!Object.prototype.hasOwnProperty.call(globalPublicFilesCache, iconName)) {
146
147
  // dist/favicon.ico
147
148
  let Path = path.join(config.build.outDir, iconName);
148
149
  if (fs.existsSync(Path)) {
@@ -211,9 +212,9 @@ async function generateBundle(bundle, config, options) {
211
212
  else {
212
213
  inlineHtmlAssets();
213
214
  }
214
- htmlChunk.source = htmlChunk.source.split(fakeScript, 2).join(template.base(newJSCode.join(';'), options.compressFormat, options.useBase128));
215
+ htmlChunk.source = htmlChunk.source.split(fakeScript, 2).join(template.base(newJSCode.join(';'), options.compressFormat, options.useBase128, options.compressor));
215
216
  // log
216
- console.log(" " + pc.gray(KiB(oldSize) + " -> ") + pc.cyanBright(KiB(htmlChunk.source.length)) + '\n');
217
+ console.log(" " + pc.gray(kB(oldSize) + " -> ") + pc.cyanBright(kB(htmlChunk.source.length)) + '\n');
217
218
  // delete assets
218
219
  for (const name of thisDel) {
219
220
  globalDelete.add(name);
package/dist/kB.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function kB(size: number): string;
package/dist/kB.js ADDED
@@ -0,0 +1,3 @@
1
+ export function kB(size) {
2
+ return `${size / 1000} kB`;
3
+ }
package/dist/options.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Options as htmlMinifierOptions } from 'html-minifier-terser';
2
- import { compressFormat } from './compress.js';
2
+ import { compressFormat, compressor } from './compress.js';
3
3
  export interface Options {
4
4
  /**
5
5
  * Rename index.html
@@ -39,9 +39,14 @@ export interface Options {
39
39
  useBase128?: boolean;
40
40
  /**
41
41
  * Compress format.
42
+ * https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream/DecompressionStream
42
43
  * @default "deflate-raw"
43
44
  */
44
45
  compressFormat?: compressFormat;
46
+ /**
47
+ * Custom compressor.
48
+ */
49
+ compressor?: compressor;
45
50
  }
46
51
  export declare const defaultHtmlMinifierTerserOptions: htmlMinifierOptions;
47
52
  export interface innerOptions {
@@ -53,5 +58,6 @@ export interface innerOptions {
53
58
  removeInlinedPublicIconFiles: boolean;
54
59
  useBase128: boolean;
55
60
  compressFormat: compressFormat;
61
+ compressor?: compressor;
56
62
  }
57
63
  export declare function getInnerOptions(opt?: Options): innerOptions;
package/dist/options.js CHANGED
@@ -7,7 +7,7 @@ export const defaultHtmlMinifierTerserOptions = {
7
7
  minifyJS: false,
8
8
  };
9
9
  export function getInnerOptions(opt) {
10
- opt ||= {};
10
+ opt || (opt = {});
11
11
  return {
12
12
  rename: opt.rename && opt.rename.replace(/(\.(html?)?)?$/, '.html'),
13
13
  htmlMinifierTerser: opt.htmlMinifierTerser == null || opt.htmlMinifierTerser === true
@@ -18,8 +18,7 @@ export function getInnerOptions(opt) {
18
18
  tryInlineHtmlPublicIcon: opt.tryInlineHtmlPublicIcon ?? true,
19
19
  removeInlinedPublicIconFiles: opt.removeInlinedPublicIconFiles ?? true,
20
20
  useBase128: opt.useBase128 ?? true,
21
- compressFormat: ["deflate-raw", "deflate", "gzip"].includes(opt.compressFormat)
22
- ? opt.compressFormat
23
- : "deflate-raw",
21
+ compressFormat: opt.compressFormat || "deflate-raw",
22
+ compressor: typeof opt.compressor == 'function' ? opt.compressor : undefined,
24
23
  };
25
24
  }
@@ -1 +1 @@
1
- function l(e){let t=new Uint8Array(Math.floor(e.length/8*7)),a=0,c=0,r,o=()=>(r=e.charCodeAt(a++))>>7?r=0:r;for(;a<e.length;)t[c++]=o()<<1|o()>>6,t[c++]=r<<2|o()>>5,t[c++]=r<<3|o()>>4,t[c++]=r<<4|o()>>3,t[c++]=r<<5|o()>>2,t[c++]=r<<6|o()>>1,t[c++]=r<<7|o();return t}new Response(new ReadableStream({start(e){e.enqueue(l("<script>")),e.close()}}).pipeThrough(new DecompressionStream("<format>")),{headers:{"Content-Type":"text/javascript"}}).blob().then(e=>import(e=URL.createObjectURL(e)).finally(()=>URL.revokeObjectURL(e)))
1
+ var a="<script>",i=a.length,r=new Uint8Array(Math.floor(i/8*7)),c=0,o=0,e,t=()=>(e=a.charCodeAt(c++))>127?e=0:e;for(;c<i;)r[o++]=t()<<1|t()>>6,r[o++]=e<<2|t()>>5,r[o++]=e<<3|t()>>4,r[o++]=e<<4|t()>>3,r[o++]=e<<5|t()>>2,r[o++]=e<<6|t()>>1,r[o++]=e<<7|t();new Response(new Blob([r]).stream().pipeThrough(new DecompressionStream("<format>")),{headers:{"Content-Type":"text/javascript"}}).blob().then(n=>import(n=URL.createObjectURL(n)).finally(h=>URL.revokeObjectURL(n)))
@@ -1 +1 @@
1
- fetch("data:;base64,<script>").then(e=>new Response(e.body.pipeThrough(new DecompressionStream("<format>")),{headers:{"Content-Type":"text/javascript"}}).blob()).then(e=>import(e=URL.createObjectURL(e)).finally(()=>URL.revokeObjectURL(e)))
1
+ fetch("data:;base64,<script>").then(e=>new Response(e.body.pipeThrough(new DecompressionStream("<format>")),{headers:{"Content-Type":"text/javascript"}}).blob()).then(e=>import(e=URL.createObjectURL(e)).finally(t=>URL.revokeObjectURL(e)))
package/package.json CHANGED
@@ -1,23 +1,27 @@
1
1
  {
2
2
  "name": "vite-plugin-singlefile-compression",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "author": "bddjr",
5
5
  "license": "MIT",
6
6
  "description": "",
7
- "main": "dist/index.js",
8
- "types": "dist/index.d.ts",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "browser": "null.js",
9
10
  "files": [
10
11
  "dist"
11
12
  ],
12
13
  "type": "module",
13
14
  "scripts": {
14
- "build": "rimraf dist && tsc && node build.js && cd test && npm run build",
15
- "prepublishOnly": "npm run build"
15
+ "build": "rimraf dist && tsc && node build.js && cd test && node --run build",
16
+ "prepublishOnly": "node --run build"
16
17
  },
17
18
  "repository": {
18
19
  "type": "git",
19
20
  "url": "git+https://github.com/bddjr/vite-plugin-singlefile-compression.git"
20
21
  },
22
+ "publishConfig": {
23
+ "registry": "https://registry.npmjs.org"
24
+ },
21
25
  "keywords": [
22
26
  "vite-plugin",
23
27
  "vite",
@@ -54,4 +58,4 @@
54
58
  "esbuild": ">=0.25.0",
55
59
  "typescript": ">=5.7.2"
56
60
  }
57
- }
61
+ }
package/dist/KiB.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function KiB(size: number): string;
package/dist/KiB.js DELETED
@@ -1,3 +0,0 @@
1
- export function KiB(size) {
2
- return `${Math.ceil(size / 10.24) / 100} KiB`;
3
- }