Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
848b29bb20 Add special error for bun builtins in bundler 2024-10-20 00:23:14 -07:00
3 changed files with 53 additions and 6 deletions

View File

@@ -628,6 +628,16 @@ pub const BundleV2 = struct {
.{ import_record.kind.errorLabel(), path_to_use },
import_record.kind,
) catch unreachable;
} else if ((target == .browser or target == .node) and options.ExternalModules.isBunBuiltin(path_to_use)) {
addError(
log,
source,
import_record.range,
this.graph.allocator,
"{s} build cannot {s} Bun module: \"{s}\". To use Bun builtins, set target to 'bun' or feature-detect the Bun global at runtime.",
.{ if (target == .browser) "Browser" else "Node", import_record.kind.errorLabel(), path_to_use },
import_record.kind,
) catch unreachable;
} else {
addError(
log,
@@ -2365,6 +2375,16 @@ pub const BundleV2 = struct {
.{ import_record.kind.errorLabel(), import_record.path.text },
import_record.kind,
) catch @panic("unexpected log error");
} else if ((ast.target == .browser or ast.target == .node) and options.ExternalModules.isBunBuiltin(import_record.path.text)) {
addError(
this.bundler.log,
source,
import_record.range,
this.graph.allocator,
"{s} build cannot {s} Bun builtin: \"{s}\". To use Bun builtins, set target to 'bun' or feature-detect the Bun global at runtime.",
.{ if (ast.target == .browser) "Browser" else "Node", import_record.kind.errorLabel(), import_record.path.text },
import_record.kind,
) catch @panic("unexpected log error");
} else {
addError(
this.bundler.log,

View File

@@ -92,6 +92,10 @@ pub const ExternalModules = struct {
return bun.JSC.HardcodedModule.Aliases.has(str, .node);
}
pub fn isBunBuiltin(str: string) bool {
return bun.JSC.HardcodedModule.Aliases.has(str, .bun);
}
const default_wildcard_patterns = &[_]WildcardPattern{
.{
.prefix = "/bun:",

View File

@@ -56,7 +56,7 @@ describe("bundler", () => {
stdout: "function\nfunction\nundefined",
},
onAfterBundle(api) {
api.expectFile('out.js').not.toInclude('import ');
api.expectFile("out.js").not.toInclude("import ");
},
});
itBundled("browser/NodeTTY", {
@@ -73,7 +73,7 @@ describe("bundler", () => {
stdout: "function\nfunction\nfalse",
},
onAfterBundle(api) {
api.expectFile('out.js').not.toInclude('import ');
api.expectFile("out.js").not.toInclude("import ");
},
});
// TODO: use nodePolyfillList to generate the code in here.
@@ -228,11 +228,8 @@ describe("bundler", () => {
const bunModules: Record<string, "no-op" | "polyfill" | "error"> = {
"bun": "error",
"bun:ffi": "error",
"bun:dns": "error",
"bun:test": "error",
"bun:sqlite": "error",
// "bun:wrap": "error",
"bun:internal": "error",
"bun:jsc": "error",
};
@@ -281,7 +278,33 @@ describe("bundler", () => {
bundleErrors: {
"/entry.js": Object.keys(bunModules)
.filter(x => bunModules[x] === "error")
.map(x => `Could not resolve: "${x}". Maybe you need to "bun install"?`),
.map(
x =>
`Browser build cannot import Bun builtin: "${x}". To use Bun builtins, set target to 'bun' or feature-detect the Bun global at runtime.`,
),
},
});
itBundled("browser/ImportBunErrorInNode", {
skipOnEsbuild: true,
files: {
"/entry.js": `
${Object.keys(bunModules)
.map((x, i) => `import * as bun_${i} from "${x}";`)
.join("\n")}
${Object.keys(bunModules)
.map((x, i) => `console.log("${x.padEnd(12, " ")}:", !!bun_${i});`)
.join("\n")}
`,
},
target: "node",
bundleErrors: {
"/entry.js": Object.keys(bunModules)
.filter(x => bunModules[x] === "error")
.map(
x =>
`Node build cannot import Bun builtin: "${x}". To use Bun builtins, set target to 'bun' or feature-detect the Bun global at runtime.`,
),
},
});