Skip to content

Commit

Permalink
fix: this.document and this.location.href do not exist in a Web Worker (
Browse files Browse the repository at this point in the history
#55)

* this -> self

this.location does not work in a web worker, but self.location does work both
in a web worker and in a normal page.

* document doesn't exist either

* use self which works in both places

* polyfilling self.document always works
  • Loading branch information
ryanxcharles authored Aug 8, 2024
1 parent ae1190e commit 8006241
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default function topLevelAwait(options?: Options): Plugin {
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}`,
`self.document = { currentScript: { src: self.location.href } };\n${newEntry.code}`,
{
minify,
target: buildTarget as string | string[]
Expand Down

0 comments on commit 8006241

Please sign in to comment.