Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark Bun built-ins as external #3398

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 14 additions & 9 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,18 +661,23 @@ func ResolveFailureErrorTextSuggestionNotes(
if strings.HasPrefix(pkg, "node:") {
pkg = pkg[5:]
}

var how string
switch logger.API {
case logger.CLIAPI:
how = "--platform=node"
case logger.JSAPI:
how = "platform: 'node'"
case logger.GoAPI:
how = "Platform: api.PlatformNode"
}

if resolver.BuiltInNodeModules[pkg] {
var how string
switch logger.API {
case logger.CLIAPI:
how = "--platform=node"
case logger.JSAPI:
how = "platform: 'node'"
case logger.GoAPI:
how = "Platform: api.PlatformNode"
}
hint = fmt.Sprintf("The package %q wasn't found on the file system but is built into node. "+
"Are you trying to bundle for node? You can use %q to do that, which will remove this error.", path, how)
} else if strings.HasPrefix(pkg, "bun:") {
hint = fmt.Sprintf("The package %q wasn't found on the file system but is built into Bun. "+
"Bun users should target Node.js with %q which will remove this error.", path, how)
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,31 @@ func TestAutoExternalNode(t *testing.T) {
})
}

func TestAutoExternalBun(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
// These URLs should be external automatically
import {file} from "bun";
import { Database } from "bun:sqlite";

await file("./test.txt");
const db = new Database(":memory:");

// This should be external and should be tree-shaken because it's side-effect free
import "bun:ffi";
import "bun";
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
Platform: config.PlatformNode,
},
})
}

func TestExternalWithWildcard(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
9 changes: 9 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ import "https://example.com/code.js";
import "//example.com/code.js";
import "data:application/javascript;base64,ZXhwb3J0IGRlZmF1bHQgMTIz";

================================================================================
TestAutoExternalBun
---------- /out/entry.js ----------
// entry.js
import { file } from "bun";
import { Database } from "bun:sqlite";
await file("./test.txt");
var db = new Database(":memory:");

================================================================================
TestAutoExternalNode
---------- /out/entry.js ----------
Expand Down
27 changes: 27 additions & 0 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,18 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor
if r.debugLogs != nil {
r.debugLogs.addNote("Marking this path as implicitly external due to it being a node built-in")
}
r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PrimarySideEffectsData: &SideEffectsData{}, // Mark this with "sideEffects: false"
}, debugMeta
}

if r.options.Platform == config.PlatformNode && (strings.HasPrefix(importPath, "bun:") || importPath == "bun") {
if r.debugLogs != nil {
r.debugLogs.addNote("Marking this path as implicitly external due to it being a Bun built-in")
}
r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
Expand Down Expand Up @@ -495,6 +506,22 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor
IsExternal: true,
PrimarySideEffectsData: sideEffects,
}, debugMeta

}

if r.options.Platform == config.PlatformNode && (strings.HasPrefix(importPath, "bun:") || importPath == "bun") {
if r.debugLogs != nil {
r.debugLogs.addNote("Marking this path as implicitly external due to it being a Bun built-in")
}

// NodeColonPrefixImport check isn't necessary because the "bun:" prefix is always required

r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PrimarySideEffectsData: &SideEffectsData{},
}, debugMeta
}

if parsed, ok := ParseDataURL(importPath); ok {
Expand Down