vite-plugin-singlefile-compression 1.0.4 → 1.0.5

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 (2) hide show
  1. package/dist/index.js +20 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -54,14 +54,17 @@ async function generateBundle(bundle, htmlMinifierOptions) {
54
54
  console.log(pc.cyan('\n\nvite-plugin-singlefile-compression ') + pc.green('building...'));
55
55
  const globalDel = new Set();
56
56
  const globalDoNotDel = new Set();
57
- const bundleNames = Object.keys(bundle);
58
- for (const htmlFileName of bundleNames) {
59
- // key format:
60
- // index.html
61
- // assets/index-ZZZZZZZZ.js
62
- // skip other file
63
- if (!htmlFileName.endsWith('.html'))
64
- continue;
57
+ /** fotmat: ["assets/index-XXXXXXXX.js"] */
58
+ const bundleAssetsNames = [];
59
+ /** format: ["index.html"] */
60
+ const bundleHTMLNames = [];
61
+ for (const name of Object.keys(bundle)) {
62
+ if (name.startsWith('assets/'))
63
+ bundleAssetsNames.push(name);
64
+ else if (name.endsWith('.html'))
65
+ bundleHTMLNames.push(name);
66
+ }
67
+ for (const htmlFileName of bundleHTMLNames) {
65
68
  // init
66
69
  const htmlChunk = bundle[htmlFileName];
67
70
  let newHtml = htmlChunk.source;
@@ -76,6 +79,11 @@ async function generateBundle(bundle, htmlMinifierOptions) {
76
79
  const cssSource = css.source;
77
80
  if (cssSource) {
78
81
  oldSize += cssSource.length;
82
+ // do not delete not inlined asset
83
+ for (const name of bundleAssetsNames) {
84
+ if (cssSource.includes(name.slice('assets/'.length)))
85
+ globalDoNotDel.add(name);
86
+ }
79
87
  // add script for load css
80
88
  newJSCode.push('document.head.appendChild(document.createElement("style")).innerHTML='
81
89
  + JSON.stringify(cssSource.replace(/\s+$/, '')));
@@ -115,8 +123,9 @@ async function generateBundle(bundle, htmlMinifierOptions) {
115
123
  oldSize += js.code.length;
116
124
  // fix new URL
117
125
  newJSCode.push(`import.meta.url=location.origin+location.pathname.replace(/[^/]*$/,"${name}")`);
118
- for (const name of bundleNames) {
119
- if (name.startsWith('assets/') && js.code.includes(name.slice('assets/'.length)))
126
+ // do not delete not inlined asset
127
+ for (const name of bundleAssetsNames) {
128
+ if (js.code.includes(name.slice('assets/'.length)))
120
129
  globalDoNotDel.add(name);
121
130
  }
122
131
  // add script
@@ -142,6 +151,7 @@ async function generateBundle(bundle, htmlMinifierOptions) {
142
151
  }
143
152
  // delete inlined assets
144
153
  for (const name of globalDel) {
154
+ // do not delete not inlined asset
145
155
  if (!globalDoNotDel.has(name))
146
156
  delete bundle[name];
147
157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-singlefile-compression",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "files": [