fix: strip import options when bundling with applied loader

This commit is contained in:
RiskyMH
2025-07-30 13:01:37 +10:00
parent e86f0c65f1
commit 642fb9d181
3 changed files with 29 additions and 5 deletions

View File

@@ -1835,9 +1835,15 @@ fn NewPrinter(
p.printStringLiteralUTF8(path.pretty, false);
}
// Only print import options if the loader hasn't already been applied
// When bundling with a loader applied, the transformation has already happened
// so we don't need the import options anymore
if (!import_options.isMissing()) {
p.printWhitespacer(ws(", "));
p.printExpr(import_options, .comma, .{});
const loader_was_applied = p.options.bundling and record.loader != null;
if (!loader_was_applied) {
p.printWhitespacer(ws(", "));
p.printExpr(import_options, .comma, .{});
}
}
p.print(")");

View File

@@ -93,6 +93,25 @@ export const data = "test";`,
},
run: { stdout: "executed" },
});
itBundled("bun/loader-text-splitting-strips-options", {
target,
splitting: true,
outdir: "/out",
files: {
"/entry.ts": /* js */ `
const mod = await import('./data.js', { with: { type: 'text' } });
console.write(mod.default);
`,
"/data.js": `export default "test data";`,
},
run: { stdout: `export default "test data";` },
onAfterBundle(api) {
const entryFile = api.readFile("/out/entry.js");
expect(entryFile).not.toContain("with:");
expect(entryFile).not.toContain("{ type:");
},
});
});
}

View File

@@ -580,13 +580,12 @@ describe("import.meta.glob", () => {
runProc.exited,
]);
expect(exitCode).toBe(0);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
expect(stderr).toBe("");
expect(stdout).toContain("COUNT: 3");
expect(stdout).toContain("SCRIPT_TEXT:");
expect(stdout).toContain('var script_default = \'console.log("This should be text, not executed!");');
expect(stdout).toContain('console.log("This should be text, not executed!");');
expect(stdout).toContain('export default "js-module";');
const lines = stdout.split("\n");
const shouldNotExecuteLine = lines.findIndex(line => line === "This should be text, not executed!");
expect(shouldNotExecuteLine).toBe(-1);