Skip to content
Closed

ai slop #21861

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/js_printer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4460,25 +4460,30 @@ fn NewPrinter(
p.printImportRecordPath(record);

// backwards compatibility: previously, we always stripped type
if (comptime is_bun_platform) if (record.loader) |loader| switch (loader) {
.jsx => p.printWhitespacer(ws(" with { type: \"jsx\" }")),
.js => p.printWhitespacer(ws(" with { type: \"js\" }")),
.ts => p.printWhitespacer(ws(" with { type: \"ts\" }")),
.tsx => p.printWhitespacer(ws(" with { type: \"tsx\" }")),
if (record.loader) |loader| switch (loader) {
// Always preserve web standard import attributes
.css => p.printWhitespacer(ws(" with { type: \"css\" }")),
.file => p.printWhitespacer(ws(" with { type: \"file\" }")),
.json => p.printWhitespacer(ws(" with { type: \"json\" }")),
.jsonc => p.printWhitespacer(ws(" with { type: \"jsonc\" }")),
.toml => p.printWhitespacer(ws(" with { type: \"toml\" }")),
.wasm => p.printWhitespacer(ws(" with { type: \"wasm\" }")),
.napi => p.printWhitespacer(ws(" with { type: \"napi\" }")),
.base64 => p.printWhitespacer(ws(" with { type: \"base64\" }")),
.dataurl => p.printWhitespacer(ws(" with { type: \"dataurl\" }")),
.text => p.printWhitespacer(ws(" with { type: \"text\" }")),
.bunsh => p.printWhitespacer(ws(" with { type: \"sh\" }")),
// sqlite_embedded only relevant when bundling
.sqlite, .sqlite_embedded => p.printWhitespacer(ws(" with { type: \"sqlite\" }")),
.html => p.printWhitespacer(ws(" with { type: \"html\" }")),
.wasm => p.printWhitespacer(ws(" with { type: \"webassembly\" }")),
// Only preserve Bun-specific loaders when on Bun platform
else => if (comptime is_bun_platform) switch (loader) {
.jsx => p.printWhitespacer(ws(" with { type: \"jsx\" }")),
.js => p.printWhitespacer(ws(" with { type: \"js\" }")),
.ts => p.printWhitespacer(ws(" with { type: \"ts\" }")),
.tsx => p.printWhitespacer(ws(" with { type: \"tsx\" }")),
.file => p.printWhitespacer(ws(" with { type: \"file\" }")),
.jsonc => p.printWhitespacer(ws(" with { type: \"jsonc\" }")),
.toml => p.printWhitespacer(ws(" with { type: \"toml\" }")),
.napi => p.printWhitespacer(ws(" with { type: \"napi\" }")),
.base64 => p.printWhitespacer(ws(" with { type: \"base64\" }")),
.dataurl => p.printWhitespacer(ws(" with { type: \"dataurl\" }")),
.text => p.printWhitespacer(ws(" with { type: \"text\" }")),
.bunsh => p.printWhitespacer(ws(" with { type: \"sh\" }")),
// sqlite_embedded only relevant when bundling
.sqlite, .sqlite_embedded => p.printWhitespacer(ws(" with { type: \"sqlite\" }")),
.html => p.printWhitespacer(ws(" with { type: \"html\" }")),
.css, .json, .wasm => unreachable, // handled above
},
};
p.printSemicolonAfterStatement();
},
Expand Down
1 change: 1 addition & 0 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ pub const Loader = enum(u8) {
.{ "jsonc", .jsonc },
.{ "toml", .toml },
.{ "wasm", .wasm },
.{ "webassembly", .wasm },
.{ "napi", .napi },
.{ "node", .napi },
.{ "dataurl", .dataurl },
Expand Down
84 changes: 84 additions & 0 deletions test/regression/issue/15310-css-import-transpiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, test } from "bun:test";

// Regression tests for issue #15310
// CSS import attributes should be preserved in transpiler output
describe("transpiler preserves web standard import attributes - issue #15310", () => {
test("CSS import attributes should be preserved", () => {
const transpiler = new Bun.Transpiler({ loader: "js" });

const input = `import reset from "./reset.css" with { type: "css" };
console.log(reset);
document.adoptedStyleSheets = [reset];`;

const result = transpiler.transformSync(input, "js");

// CSS import attributes should be preserved (web standard)
expect(result).toContain('with { type: "css" }');
expect(result).toContain('import reset from "./reset.css" with { type: "css" };');
});

test("JSON import attributes should be preserved", () => {
const transpiler = new Bun.Transpiler({ loader: "js" });

const input = `import data from "./data.json" with { type: "json" };
console.log(data);`;

const result = transpiler.transformSync(input, "js");

// JSON import attributes should be preserved (web standard)
expect(result).toContain('with { type: "json" }');
expect(result).toContain('import data from "./data.json" with { type: "json" };');
});

test("WebAssembly import attributes should be preserved", () => {
const transpiler = new Bun.Transpiler({ loader: "js" });

const input = `import module from "./module.wasm" with { type: "webassembly" };
console.log(module);`;

const result = transpiler.transformSync(input, "js");

// WebAssembly import attributes should be preserved (web standard)
expect(result).toContain('with { type: "webassembly" }');
expect(result).toContain('import module from "./module.wasm" with { type: "webassembly" };');
});

test("Bun-specific import attributes should not be preserved in transpiler", () => {
const transpiler = new Bun.Transpiler({ loader: "js" });

const input = `import data from "./data.toml" with { type: "toml" };
console.log(data);`;

const result = transpiler.transformSync(input, "js");

// TOML imports are Bun-specific and should not be preserved in transpiler mode
expect(result).not.toContain('with { type: "toml" }');
expect(result).toContain('import data from "./data.toml";');
});

test("Multiple standard import attributes work correctly", () => {
const transpiler = new Bun.Transpiler({ loader: "js" });

const input = `import styles from "./styles.css" with { type: "css" };
import config from "./config.json" with { type: "json" };
import wasmModule from "./module.wasm" with { type: "webassembly" };
import utils from "./utils.toml" with { type: "toml" };
document.adoptedStyleSheets = [styles];
console.log(config, wasmModule, utils);`;

const result = transpiler.transformSync(input, "js");

// Web standard attributes preserved
expect(result).toContain('with { type: "css" }');
expect(result).toContain('with { type: "json" }');
expect(result).toContain('with { type: "webassembly" }');
// Bun-specific attributes stripped
expect(result).not.toContain('with { type: "toml" }');

// All imports still present
expect(result).toContain('import styles from "./styles.css"');
expect(result).toContain('import config from "./config.json"');
expect(result).toContain('import wasmModule from "./module.wasm"');
expect(result).toContain('import utils from "./utils.toml"');
});
});