Closed meglio closed 1 year ago
This is because import.meta.url
is not supported in your context (CJS or IIFE), it is only supported in ESM. So esbuild transforms that to var import_meta = {}
.
Since your runtime is Node.js, you can do either:
--external:tiny-secp256k1
.--format=esm
, you may also need to set out extension to .mjs for Node.js to know the context.import.meta.url
to the real absolute path in your FS. You can take a look at how tsup did it.So, if I go with the esm format, will it literally bundle the source wasm file into the bundle and make it automagically work?
I tried --format=esm
. Building successful, but error when running:
file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:13
throw new Error('Dynamic require of "' + x + '" is not supported');
^
Error: Dynamic require of "buffer" is not supported
at file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:13:9
at node_modules/bitcoinjs-lib/src/types.js (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:6419:20)
at __require2 (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:16:51)
at node_modules/bitcoinjs-lib/src/script_signature.js (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:6531:17)
at __require2 (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:16:51)
at node_modules/bitcoinjs-lib/src/script.js (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:6602:27)
at __require2 (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:16:51)
at node_modules/bitcoinjs-lib/src/payments/embed.js (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:6809:19)
at __require2 (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:16:51)
at node_modules/bitcoinjs-lib/src/payments/index.js (file:///Users/.../.../.../esbuildtest/bin/jstools.mjs:12372:19)
Node.js v18.12.1
Dynamic require of "buffer" is not supported
That's another issue, that cjs code will be wrapped in a callback in esm bundle result in esbuild, and Node.js forbids any dynamic require on builtin modules. More details see #1921.
The best solution to this (not workarounds) IMO is to ensure all dependencies of your bundle are in ESM format. You can enable metafile and use the official analyzer to check it.
will it literally bundle the source wasm file into the bundle and make it automagically work?
To bundle a wasm file, first of all it has to be imported into the code with direct imports (import wasmBinary from './file.wasm'
). Then you could make use of the wasm plugin to do so.
Okay, since I don't control how the packages use .wasm files, nor whether they are available in the ESM format, turns out this option is not viable.
Next, I should try --external:tiny-secp256k1
. If I make it external, does it mean I'll have to npm i tiny-secp256k1
in the project's directory before I can call the bundled file? Or is there a better way? I'm trying to avoid installing packages with using npm, being my ultimate goal for using esbuild.
Since your main purpose is creating a bundle.js with everything bundled in, it is not that hard to go with the wasm plugin approach. Let me give you a tiny example: https://gist.github.com/hyrious/311a8d7af354e94ab64a70a4da757e49
I'm closing this issue because this is not a problem with esbuild.
I'm building for node environment and using
tiny-secp256k1
package.The build is successful, but when running, an error is thrown:
Here is that piece in the file with line
19452
:Am I doing it wrong, or is it a bug?