vite-plugin-singlefile-compression 1.0.3 → 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 +21 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -54,13 +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
- for (const htmlFileName of Object.keys(bundle)) {
58
- // key format:
59
- // index.html
60
- // assets/index-ZZZZZZZZ.js
61
- // skip other file
62
- if (!htmlFileName.endsWith('.html'))
63
- 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) {
64
68
  // init
65
69
  const htmlChunk = bundle[htmlFileName];
66
70
  let newHtml = htmlChunk.source;
@@ -75,6 +79,11 @@ async function generateBundle(bundle, htmlMinifierOptions) {
75
79
  const cssSource = css.source;
76
80
  if (cssSource) {
77
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
+ }
78
87
  // add script for load css
79
88
  newJSCode.push('document.head.appendChild(document.createElement("style")).innerHTML='
80
89
  + JSON.stringify(cssSource.replace(/\s+$/, '')));
@@ -114,10 +123,10 @@ async function generateBundle(bundle, htmlMinifierOptions) {
114
123
  oldSize += js.code.length;
115
124
  // fix new URL
116
125
  newJSCode.push(`import.meta.url=location.origin+location.pathname.replace(/[^/]*$/,"${name}")`);
117
- for (const name in assets) {
118
- // name: logo-XXXXXXXX.svg
119
- if (js.code.includes(name))
120
- globalDoNotDel.add("assets/" + name);
126
+ // do not delete not inlined asset
127
+ for (const name of bundleAssetsNames) {
128
+ if (js.code.includes(name.slice('assets/'.length)))
129
+ globalDoNotDel.add(name);
121
130
  }
122
131
  // add script
123
132
  newJSCode.push(js.code.replace(/;?\n?$/, ''));
@@ -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.3",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "files": [