Skip to content

Commit

Permalink
feat: polyfill document.currentScript.src for IIFE workers
Browse files Browse the repository at this point in the history
  • Loading branch information
Menci committed Dec 10, 2023
1 parent f584bce commit 160f5f1
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,17 @@ export default function topLevelAwait(options?: Options): Plugin {
entryFileNames: path.posix.join(assetsDir, "[name].js")
});

// Minify with ESBuild
if (minify) {
newEntry.code = (
await esbuild.transform(newEntry.code, {
minify: true,
// Postprocess and minify (if requested) with ESBuild
newEntry.code = (
await esbuild.transform(
// Polyfill `document.currentScript.src` since it's used for `import.meta.url`.
`this.document = { currentScript: { src: this.location.href } };\n${newEntry.code}`,
{
minify,
target: buildTarget as string | string[]
})
).code;
}
}
)
).code;

// Remove extra chunks and replace ESM entry with IIFE entry
for (const chunkName of chunkNames) {
Expand Down

0 comments on commit 160f5f1

Please sign in to comment.