Skip to content

Commit

Permalink
Fixes #5178
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jan 7, 2024
1 parent 0dc6108 commit 08ff944
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ pub const VirtualMachine = struct {
source_to_use,
normalized_specifier,
if (is_esm) .stmt else .require,
.read_only,
if (jsc_vm.standalone_module_graph == null) .read_only else .disable,
)) {
.success => |r| r,
.failure => |e| e,
Expand Down Expand Up @@ -2091,7 +2091,7 @@ pub const VirtualMachine = struct {
this.bundler.fs.top_level_dir,
normalizeSource(preload),
.stmt,
.read_only,
if (this.standalone_module_graph == null) .read_only else .disable,
)) {
.success => |r| r,
.failure => |e| {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/build_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ pub const BuildCommand = struct {

// We never want to hit the filesystem for these files
// This "compiled" protocol is specially handled by the module resolver.
this_bundler.options.public_path = "compiled://root/";
this_bundler.resolver.opts.public_path = "compiled://root/";
this_bundler.options.public_path = "/$bunfs/root/";
this_bundler.resolver.opts.public_path = "/$bunfs/root/";

if (outfile.len == 0) {
outfile = std.fs.path.basename(this_bundler.options.entry_points[0]);
Expand Down
40 changes: 26 additions & 14 deletions src/resolver/resolver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ pub const Resolver = struct {

pub fn resolveAndAutoInstall(
r: *ThisResolver,
source_dir: string,
source_dir_: string,
import_path: string,
kind: ast.ImportKind,
global_cache: GlobalCache,
Expand Down Expand Up @@ -864,22 +864,34 @@ pub const Resolver = struct {
};
}

if (r.standalone_module_graph) |graph| {
if (strings.hasPrefixComptime(import_path, "compiled://")) {
if (graph.files.contains(import_path)) {
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
// When using `bun build --compile`, module resolution is never relative
// to our special /$bunfs/ directory.
//
// It's always relative to the current working directory of the project
// root.
const source_dir = brk: {
if (r.standalone_module_graph) |graph| {
if (strings.isBunStandaloneFilePath(import_path)) {
if (graph.files.contains(import_path)) {
return .{
.success = Result{
.import_kind = kind,
.path_pair = PathPair{
.primary = Path.init(import_path),
},
.is_standalone_module = true,
.module_type = .esm,
},
.is_standalone_module = true,
.module_type = .esm,
},
};
};
}

return .{ .not_found = {} };
} else if (strings.isBunStandaloneFilePath(source_dir_)) {
break :brk Fs.FileSystem.instance.top_level_dir;
}
}
}
break :brk source_dir_;
};

if (DataURL.parse(import_path) catch {
return .{ .failure = error.InvalidDataURL };
Expand Down
2 changes: 1 addition & 1 deletion src/standalone_bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const StandaloneModuleGraph = struct {
}

pub fn find(this: *const StandaloneModuleGraph, name: []const u8) ?*File {
if (!bun.strings.hasPrefixComptime(name, "compiled://root/")) {
if (!bun.strings.isBunStandaloneFilePath(name)) {
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/string_immutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ pub fn hasPrefixComptime(self: string, comptime alt: anytype) bool {
return self.len >= alt.len and eqlComptimeCheckLenWithType(u8, self[0..alt.len], alt, false);
}

pub fn isBunStandaloneFilePath(self: string) bool {
return hasPrefixComptime(self, "/$bunfs/");
}

pub fn hasPrefixComptimeUTF16(self: []const u16, comptime alt: []const u8) bool {
return self.len >= alt.len and eqlComptimeCheckLenWithType(u16, self[0..alt.len], comptime toUTF16Literal(alt), false);
}
Expand Down
28 changes: 27 additions & 1 deletion test/bundler/bundler_compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe("bundler", () => {
},
run: { stdout: "Hello, world!" },
});
itBundled("compile/pathToFileURLWorks", {
compile: true,
files: {
"/entry.ts": /* js */ `
import {pathToFileURL, fileURLToPath} from 'bun';
console.log(pathToFileURL(import.meta.path).href + " " + fileURLToPath(import.meta.url));
if (fileURLToPath(import.meta.url) !== import.meta.path) throw "fail";
if (pathToFileURL(import.meta.path).href !== import.meta.url) throw "fail";
`,
},
run: { stdout: `file:///$bunfs/root/out /$bunfs/root/out`, setCwd: true },
});
itBundled("compile/VariousBunAPIs", {
compile: true,
files: {
Expand Down Expand Up @@ -144,11 +156,25 @@ describe("bundler", () => {
files: {
"/entry.tsx": /* tsx */ `
const req = (x) => require(x);
req('express');
console.log(req('express'));
`,
},
run: {
error: 'Cannot find package "express"',
setCwd: true,
},
compile: true,
});
itBundled("compile/CanRequireLocalPackages", {
files: {
"/entry.tsx": /* tsx */ `
const req = (x) => require(x);
console.log(req('react/package.json').version);
`,
},
run: {
stdout: require("react/package.json").version,
setCwd: false,
},
compile: true,
});
Expand Down

0 comments on commit 08ff944

Please sign in to comment.