aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-27 22:30:10 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-27 22:30:10 -0700
commit3e5beb12790b3fe5b7eef1835c59f05fd12fcff9 (patch)
tree3c1119e06b7738a1db4abd27383b56425c45fed3
parent70b9bf743c21484b35918bb07ff2423f77207d2e (diff)
downloadbun-3e5beb12790b3fe5b7eef1835c59f05fd12fcff9.tar.gz
bun-3e5beb12790b3fe5b7eef1835c59f05fd12fcff9.tar.zst
bun-3e5beb12790b3fe5b7eef1835c59f05fd12fcff9.zip
Fix bug with // @bun annotation in main thread (#3855)
* Uncomment test * Fix bug with // @bun + async transpiler --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r--src/bun.js/module_loader.zig18
-rw-r--r--test/transpiler/async-transpiler-entry.js1
-rw-r--r--test/transpiler/async-transpiler-imported.js6
-rw-r--r--test/transpiler/runtime-transpiler.test.ts30
4 files changed, 53 insertions, 2 deletions
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index 4763ed1bc..69bed7d99 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -194,8 +194,7 @@ pub const RuntimeTranspilerStore = struct {
referrer: []const u8,
) *anyopaque {
debug("transpile({s})", .{path.text});
- var was_new = true;
- var job: *TranspilerJob = this.store.getAndSeeIfNew(&was_new);
+ var job: *TranspilerJob = this.store.get();
var owned_path = Fs.Path.init(bun.default_allocator.dupe(u8, path.text) catch unreachable);
var promise = JSC.JSInternalPromise.create(globalObject);
job.* = TranspilerJob{
@@ -469,6 +468,21 @@ pub const RuntimeTranspilerStore = struct {
}
}
+ if (parse_result.already_bundled) {
+ this.resolved_source = ResolvedSource{
+ .allocator = null,
+ .source_code = bun.String.createLatin1(parse_result.source.contents),
+ .specifier = String.create(specifier),
+ .source_url = ZigString.init(path.text),
+ // // TODO: change hash to a bitfield
+ // .hash = 1,
+
+ // having JSC own the memory causes crashes
+ .hash = 0,
+ };
+ return;
+ }
+
for (parse_result.ast.import_records.slice()) |*import_record_| {
var import_record: *bun.ImportRecord = import_record_;
diff --git a/test/transpiler/async-transpiler-entry.js b/test/transpiler/async-transpiler-entry.js
new file mode 100644
index 000000000..179a4e6a8
--- /dev/null
+++ b/test/transpiler/async-transpiler-entry.js
@@ -0,0 +1 @@
+export { default } from "./async-transpiler-imported.js";
diff --git a/test/transpiler/async-transpiler-imported.js b/test/transpiler/async-transpiler-imported.js
new file mode 100644
index 000000000..ed2f22490
--- /dev/null
+++ b/test/transpiler/async-transpiler-imported.js
@@ -0,0 +1,6 @@
+// @bun
+export default 42;
+
+if (import.meta.main) {
+ console.log("Hello world!");
+}
diff --git a/test/transpiler/runtime-transpiler.test.ts b/test/transpiler/runtime-transpiler.test.ts
new file mode 100644
index 000000000..b3f545719
--- /dev/null
+++ b/test/transpiler/runtime-transpiler.test.ts
@@ -0,0 +1,30 @@
+import { beforeEach, describe, expect, test } from "bun:test";
+import { bunEnv, bunExe } from "harness";
+
+describe("// @bun", () => {
+ beforeEach(() => {
+ delete require.cache[require.resolve("./async-transpiler-entry")];
+ delete require.cache[require.resolve("./async-transpiler-imported")];
+ });
+
+ test("async transpiler", async () => {
+ const { default: value } = await import("./async-transpiler-entry");
+ expect(value).toBe(42);
+ });
+
+ test("require()", async () => {
+ const { default: value } = require("./async-transpiler-entry");
+ expect(value).toBe(42);
+ });
+
+ test("synchronous", async () => {
+ const { stdout, exitCode } = Bun.spawnSync({
+ cmd: [bunExe(), require.resolve("./async-transpiler-imported")],
+ cwd: import.meta.dir,
+ env: bunEnv,
+ stderr: "inherit",
+ });
+ expect(stdout.toString()).toBe("Hello world!\n");
+ expect(exitCode).toBe(0);
+ });
+});