Repository navigation
fix(core): externalize wasm-pack packages in the config bundler - #28
Merged
Merged
Conversation
esbuild bundles the user's commoners.config.ts into a `.mjs` ESM
snapshot. If that config statically imports a wasm-pack `--target
nodejs` output (CJS using `__dirname` + `require('fs')`), esbuild
wraps the CJS module but the `__dirname` reference has no value in
ESM scope → "__dirname is not defined in ES module scope" with no
stack at launch.
Wasm-pack Node outputs are not structurally bundlable into ESM
without rewriting their sync-load path. The cleanest fix is to mark
them external so Node's real resolver loads them at runtime, where
their nested package.json (`"type": "commonjs"` if the consumer set
it) is honored.
Widens the config-bundle externals from a hardcoded list to also
match `*-wasm` and `wasm-*` globs. esbuild's `external` field
supports prefix/suffix wildcards but not regex — `*-wasm` matches
wasm-pack packages; `wasm-*` mirrors for the other
naming convention.
Caller pattern (verified in a downstream app):
package.json:
"type": "module",
"exports": {
".": {
"node": "./node/pkg.js",
"browser": "./web/pkg.js",
"default": "./web/pkg.js"
}
}
node/package.json:
{ "type": "commonjs" }
(cherry picked from commit 44fbea4)
…SM build Config files are bundled into one ES module before Node loads them. wasm-pack's --target nodejs output is CommonJS that finds its .wasm through __dirname, so bundling it in fails with "__dirname is not defined in ES module scope". The bundler already keeps *-wasm and wasm-* packages external; nothing tested it. The new test builds a project whose config imports a CommonJS package that reads a file next to itself through __dirname, then loads it with loadConfigFromFile from dist/main.mjs in a separate Node process. With the *-wasm external removed, it fails with the __dirname error. tests/esm-dist.test.ts is new on this branch; it holds only this test. The child script catches and prints the load error, since an unhandled rejection there can exit 0 with no output. (cherry picked from commit f9fcbb0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Config files that import a wasm-pack
--target nodejspackage fail to load from the ESM build withReferenceError: __dirname is not defined in ES module scope. The config is bundled into a single ES module, and wasm-pack's Node output is CommonJS that locates its.wasmthrough__dirname.Fix: the config bundler now keeps
*-wasmandwasm-*packages external, so Node loads them fromnode_modulesas CommonJS at runtime.Test:
tests/esm-dist.test.tsbuilds a project whose config imports a CommonJS package that reads a file next to itself through__dirname, then loads it withloadConfigFromFilefromdist/main.mjsin a separate Node process. It passes with the fix and fails with the__dirnameerror when the externals are removed.