oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.1k stars 2.76k forks source link

bun build generates undefined variable when bundling @swc/wasm-typescript #14939

Open guest271314 opened 4 days ago

guest271314 commented 4 days ago

What version of Bun is running?

1.1.34-canary.40+693320879

What platform is your computer?

Linux 5.15.0-43-generic x86_64 x86_64

What steps can reproduce the bug?

bun install @swc/wasm-typescript
bun build node_modules/@swc/wasm-typescript/wasm.js --target=node --format=esm --outfile=swc-typescript.js

What is the expected behavior?

The bundled script to work as expected.

What do you see instead?

test.js

import { transform } from './swc-typescript.js';

const tsSourceCode = `
export function add(a: number, b: number) {
    return a + b;
}
`;

const code = await transform(tsSourceCode, {

})

console.log(code);
bun run test.js
1 | import { createRequire } from "node:module";
2 | var __require = /* @__PURE__ */ createRequire(import.meta.url);
3 | 
4 | // node_modules/@swc/wasm-typescript/wasm.js
5 | var imports = {};
6 | imports["__wbindgen_placeholder__"] = exports_wasm;
                                          ^
ReferenceError: Can't find variable: exports_wasm
      at /home/user/bin/swc-typescript.js:6:39

Bun v1.1.34-canary.40+693320879 (Linux x64 baseline)
node run test.js
node:internal/modules/cjs/loader:1250
  throw err;
  ^

Error: Cannot find module '/home/user/bin/run'
    at Function._resolveFilename (node:internal/modules/cjs/loader:1247:15)
    at Function._load (node:internal/modules/cjs/loader:1072:27)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:216:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v24.0.0-nightly20241031996708042b
deno -A test.js
error: Uncaught (in promise) ReferenceError: exports_wasm is not defined
imports["__wbindgen_placeholder__"] = exports_wasm;
                                      ^
    at file:///home/userbin/swc-typescript.js:6:39

Additional information

The issue in code

@swc/wasm-typescript/wasm.js

2 let imports = {};
3 imports['__wbindgen_placeholder__'] = module.exports;

swc-typescript.js

4 // node_modules/@swc/wasm-typescript/wasm.js
5 var imports = {};
6 imports["__wbindgen_placeholder__"] = exports_wasm;

exports_wasm is undefined.

guest271314 commented 4 days ago

Pardon, using node without the run, which triggered CommonJS loader

node --no-warnings test.js
file:///home/user/bin/swc-typescript.js:6
imports["__wbindgen_placeholder__"] = exports_wasm;
                                      ^

ReferenceError: exports_wasm is not defined
    at file:///home/user/bin/swc-typescript.js:6:39
    at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Node.js v24.0.0-nightly20241031996708042b
pfgithub commented 4 days ago

Smaller reproduction:

// a.ts
let result = module.exports;
module.exports.getExports = () => result;

bun build a.ts

// a.ts
var result = exports_a;
var $getExports = () => result;
export {
  $getExports as getExports
};