Skip to content

Commit

Permalink
fix: replace import.meta.url with CJS shim (#3996)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Nov 12, 2024
1 parent f3c4ac5 commit c156075
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions esbuild-import-meta-url-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const __import_meta_url =
typeof document === 'undefined'
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
: (document.currentScript && document.currentScript.src) ||
new URL('main.js', document.baseURI).href
10 changes: 9 additions & 1 deletion esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function patchPkgJson(path) {
const pkgJson = require('./' + pkgJsonPath)
cleanPkgJson(pkgJson)
delete pkgJson.scripts
delete pkgJson.exports
await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2))
}

Expand Down Expand Up @@ -108,6 +107,15 @@ async function main() {
'direct-eval': 'silent',
},
external: externals,

// Prevent esbuild from adding a "2" to the names of CC classes for some reason.
keepNames: true,

// Fix import.meta.url in CJS output
define: {
'import.meta.url': '__import_meta_url',
},
inject: ['esbuild-import-meta-url-shim.js'],
}

await esbuild.build(config)
Expand Down

0 comments on commit c156075

Please sign in to comment.