Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace import.meta.url with CJS shim #3996

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Dismissed Show dismissed Hide dismissed
: (document.currentScript && document.currentScript.src) ||
new URL('main.js', document.baseURI).href
26 changes: 9 additions & 17 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const esbuild = require('esbuild')
const { cp, stat, readFile, writeFile } = require('fs/promises')
const { exists, emptyDir } = require('fs-extra')
const { join } = require('path')

const outputDir = 'build'

Expand All @@ -13,19 +12,6 @@ function cleanPkgJson(json) {
return json
}

/**
* Remove useless fields from package.json, this is needed mostly for `pkg`
* otherwise it will try to bundle dependencies
*/
async function patchPkgJson(path) {
const pkgJsonPath = join(outputDir, path, 'package.json')
const pkgJson = require('./' + pkgJsonPath)
cleanPkgJson(pkgJson)
delete pkgJson.scripts
delete pkgJson.exports
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robertsLando Do you remember why this was necessary? This is breaking the build now, as Z-Wave JS internally makes heavy use of the exports field.
I don't see any difference in bundle size when I remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clean was mostly for pkg side so there shouldn’t be any side effect removing it

await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2))
}

// from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
const nativeNodeModulesPlugin = {
name: 'native-node-modules',
Expand Down Expand Up @@ -108,6 +94,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 Expand Up @@ -165,9 +160,6 @@ async function main() {
`${outputDir}/package.json`,
JSON.stringify(pkgJson, null, 2),
)

await patchPkgJson('node_modules/@zwave-js/config')
await patchPkgJson('node_modules/zwave-js')
}

main().catch((err) => {
Expand Down
Loading