From 87e91037e455816a03323b6b344c312c6f93db39 Mon Sep 17 00:00:00 2001 From: bddjr Date: Wed, 27 Nov 2024 20:03:52 +0800 Subject: [PATCH] fix --- src/index.ts | 8 +++++++- test/public/robots.txt | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 test/public/robots.txt diff --git a/src/index.ts b/src/index.ts index 2b018bc..7caa4be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -81,6 +81,7 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin console.log(pc.cyan('\n\nvite-plugin-singlefile-compression ') + pc.green('building...')) const globalDel = new Set() + const globalDoNotDel = new Set() for (const htmlFileName of Object.keys(bundle)) { // key format: @@ -155,6 +156,10 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin oldSize += js.code.length // fix new URL newJSCode.push(`import.meta.url=location.origin+location.pathname.replace(/[^/]*$/,"${name}")`) + for (const i in assets) { + if (js.code.includes(i)) + globalDoNotDel.add(i) + } // add script newJSCode.push(js.code.replace(/;?\n?$/, '')) // gzip @@ -184,7 +189,8 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin // delete inlined assets for (const name of globalDel) { - delete bundle[name] + if (!globalDoNotDel.has(name)) + delete bundle[name] } console.log(pc.green('Finish.\n')) } diff --git a/test/public/robots.txt b/test/public/robots.txt new file mode 100644 index 0000000..77470cb --- /dev/null +++ b/test/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / \ No newline at end of file