From eabef40afaf8ab2f84a12da2a73e4172850092db Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 19:49:33 +0100 Subject: [PATCH 1/3] fix: replace import.meta.url with CJS shim --- esbuild-import-meta-url-shim.js | 5 +++++ esbuild.js | 26 +++++++++----------------- 2 files changed, 14 insertions(+), 17 deletions(-) create mode 100644 esbuild-import-meta-url-shim.js diff --git a/esbuild-import-meta-url-shim.js b/esbuild-import-meta-url-shim.js new file mode 100644 index 00000000000..586bf649fc0 --- /dev/null +++ b/esbuild-import-meta-url-shim.js @@ -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 diff --git a/esbuild.js b/esbuild.js index 38945398c91..b246fde0b14 100644 --- a/esbuild.js +++ b/esbuild.js @@ -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' @@ -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 - await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2)) -} - // from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 const nativeNodeModulesPlugin = { name: 'native-node-modules', @@ -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) @@ -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) => { From b4fb9361e060a745310990c2777784d3af852ef8 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 19:52:39 +0100 Subject: [PATCH 2/3] do not remove everything --- esbuild.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/esbuild.js b/esbuild.js index b246fde0b14..abf71f8b74b 100644 --- a/esbuild.js +++ b/esbuild.js @@ -1,6 +1,7 @@ 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' @@ -12,6 +13,18 @@ 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.exports + await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2)) +} + // from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 const nativeNodeModulesPlugin = { name: 'native-node-modules', @@ -160,6 +173,9 @@ 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) => { From c64afe39ddb7de7e65d90b984dcbd86358165249 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 12 Nov 2024 19:53:40 +0100 Subject: [PATCH 3/3] fix: wrong line removed --- esbuild.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esbuild.js b/esbuild.js index abf71f8b74b..c8f88b772e6 100644 --- a/esbuild.js +++ b/esbuild.js @@ -21,7 +21,7 @@ async function patchPkgJson(path) { const pkgJsonPath = join(outputDir, path, 'package.json') const pkgJson = require('./' + pkgJsonPath) cleanPkgJson(pkgJson) - delete pkgJson.exports + delete pkgJson.scripts await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2)) }