[bun build] Automatically set target to bun when a hashbang is provided

This commit is contained in:
Jarred Sumner
2023-05-15 06:30:55 -07:00
parent 0a879be328
commit 8eaf30c022
2 changed files with 15 additions and 1 deletions

View File

@@ -2592,7 +2592,7 @@ pub const ParseTask = struct {
.contents_is_recycled = false,
};
const target = use_directive.target(task.known_target orelse bundler.options.target);
const target = targetFromHashbang(entry.contents) orelse use_directive.target(task.known_target orelse bundler.options.target);
var opts = js_parser.Parser.Options.init(task.jsx, loader);
opts.legacy_transform_require_to_import = false;
@@ -11320,3 +11320,16 @@ fn getRedirectId(id: u32) ?u32 {
return id;
}
fn targetFromHashbang(buffer: []const u8) ?options.Target {
if (buffer.len > "#!/usr/bin/env bun".len) {
if (strings.hasPrefixComptime(buffer, "#!/usr/bin/env bun")) {
switch (buffer["#!/usr/bin/env bun".len]) {
'\n', ' ' => return options.Target.bun,
else => {},
}
}
}
return null;
}

View File

@@ -400,6 +400,7 @@ pub const BuildCommand = struct {
},
},
.encoding = .buffer,
.mode = if (f.is_executable) 0o755 else 0o644,
.dirfd = @intCast(bun.FileDescriptor, root_dir.dir.fd),
.file = .{
.path = JSC.Node.PathLike{