Skip to content

Commit

Permalink
do not delete not inlined asset
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Nov 27, 2024
1 parent 4785d94 commit 797bef1
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
const globalDel = new Set<string>()
const globalDoNotDel = new Set<string>()

const bundleNames = Object.keys(bundle)

for (const htmlFileName of bundleNames) {
// key format:
// index.html
// assets/index-ZZZZZZZZ.js

// skip other file
if (!htmlFileName.endsWith('.html')) continue
/** fotmat: ["assets/index-XXXXXXXX.js"] */
const bundleAssetsNames = [] as string[]
/** format: ["index.html"] */
const bundleHTMLNames = [] as string[]

for (const name of Object.keys(bundle)) {
if (name.startsWith('assets/'))
bundleAssetsNames.push(name)
else if (name.endsWith('.html'))
bundleHTMLNames.push(name)
}

for (const htmlFileName of bundleHTMLNames) {
// init
const htmlChunk = bundle[htmlFileName] as OutputAsset
let newHtml = htmlChunk.source as string
Expand All @@ -110,6 +113,11 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin
const cssSource = css.source as string
if (cssSource) {
oldSize += cssSource.length
// do not delete not inlined asset
for (const name of bundleAssetsNames) {
if (cssSource.includes(name.slice('assets/'.length)))
globalDoNotDel.add(name)
}
// add script for load css
newJSCode.push(
'document.head.appendChild(document.createElement("style")).innerHTML='
Expand Down Expand Up @@ -158,8 +166,9 @@ 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 name of bundleNames) {
if (name.startsWith('assets/') && js.code.includes(name.slice('assets/'.length)))
// do not delete not inlined asset
for (const name of bundleAssetsNames) {
if (js.code.includes(name.slice('assets/'.length)))
globalDoNotDel.add(name)
}
// add script
Expand Down Expand Up @@ -191,6 +200,7 @@ async function generateBundle(bundle: OutputBundle, htmlMinifierOptions: htmlMin

// delete inlined assets
for (const name of globalDel) {
// do not delete not inlined asset
if (!globalDoNotDel.has(name))
delete bundle[name]
}
Expand Down

0 comments on commit 797bef1

Please sign in to comment.