aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bundler/entry_points.zig8
-rw-r--r--test/cli/run/run-cjs.test.ts60
2 files changed, 64 insertions, 4 deletions
diff --git a/src/bundler/entry_points.zig b/src/bundler/entry_points.zig
index b8414a9a5..ff0b79834 100644
--- a/src/bundler/entry_points.zig
+++ b/src/bundler/entry_points.zig
@@ -190,9 +190,9 @@ pub const ServerEntryPoint = struct {
\\export * from '{s}{s}';
\\var entryNamespace = start;
\\var cjs = start?.default;
- \\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{
+ \\if (cjs && cjsSymbol in cjs) {{
\\ // if you module.exports = (class {{}}), don't call it
- \\ entryNamespace = ("prototype" in cjs) ? cjs : cjs();
+ \\ entryNamespace = import.meta.primordials.isCallable(cjs) ? cjs() : cjs;
\\}}
\\if (typeof entryNamespace?.then === 'function') {{
\\ entryNamespace = entryNamespace.then((entryNamespace) => {{
@@ -233,9 +233,9 @@ pub const ServerEntryPoint = struct {
\\export * from '{s}{s}';
\\var entryNamespace = start;
\\var cjs = start?.default;
- \\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{
+ \\if (cjs && cjsSymbol in cjs) {{
\\ // if you module.exports = (class {{}}), don't call it
- \\ entryNamespace = ("prototype" in cjs) ? cjs : cjs();
+ \\ entryNamespace = import.meta.primordials.isCallable(cjs) ? cjs() : cjs;
\\}}
\\if (typeof entryNamespace?.then === 'function') {{
\\ entryNamespace = entryNamespace.then((entryNamespace) => {{
diff --git a/test/cli/run/run-cjs.test.ts b/test/cli/run/run-cjs.test.ts
new file mode 100644
index 000000000..935cbe452
--- /dev/null
+++ b/test/cli/run/run-cjs.test.ts
@@ -0,0 +1,60 @@
+import { expect, test } from "bun:test";
+import { mkdirSync, realpathSync } from "fs";
+import { bunEnv, bunExe } from "harness";
+import { tmpdir } from "os";
+import { join } from "path";
+
+test("running a commonjs module works", async () => {
+ const dir = join(realpathSync(tmpdir()), "bun-run-test1");
+ mkdirSync(dir, { recursive: true });
+ await Bun.write(join(dir, "index1.js"), "module.exports = 1; console.log('hello world');");
+ let { stdout } = Bun.spawnSync({
+ cmd: [bunExe(), join(dir, "index1.js")],
+ cwd: dir,
+ env: bunEnv,
+ });
+ expect(stdout.toString("utf8")).toEqual("hello world\n");
+});
+
+test("running with Symbol.for(CommonJS)", async () => {
+ const dir = join(realpathSync(tmpdir()), "bun-run-test2");
+ mkdirSync(dir, { recursive: true });
+ await Bun.write(
+ join(dir, "index1.js"),
+ `// @bun
+const fn = () => console.log('hello world');
+fn[Symbol.for("CommonJS")] = true;
+export default fn;
+`,
+ );
+ let { stdout } = Bun.spawnSync({
+ cmd: [bunExe(), join(dir, "index1.js")],
+ cwd: dir,
+ env: bunEnv,
+ });
+ expect(stdout.toString("utf8")).toEqual("hello world\n");
+});
+
+
+test("not running with export default class", async () => {
+ const dir = join(realpathSync(tmpdir()), "bun-run-test2");
+ mkdirSync(dir, { recursive: true });
+ await Bun.write(
+ join(dir, "index1.js"),
+ `// @bun
+class Foo {
+ constructor() {
+ console.log('hello world');
+ }
+};
+Foo[Symbol.for("CommonJS")] = true;
+export default Foo
+`,
+ );
+ let { stdout } = Bun.spawnSync({
+ cmd: [bunExe(), join(dir, "index1.js")],
+ cwd: dir,
+ env: bunEnv,
+ });
+ expect(stdout.toString("utf8")).toEqual("");
+});