aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/js_parser.zig6
-rw-r--r--src/linker.zig62
-rw-r--r--src/runtime.footer.bun.js5
3 files changed, 36 insertions, 37 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 34e4b3a9f..9f422d9b8 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -5015,6 +5015,11 @@ fn NewParser_(
pub fn resolveCommonJSSymbols(p: *P) void {
if (p.runtime_imports.__require) |*require| {
p.resolveGeneratedSymbol(require);
+ } else if (p.symbols.items[p.require_ref.innerIndex()].use_count_estimate == 0 and
+ p.symbols.items[p.require_ref.innerIndex()].link.isNull())
+ {
+ // ensure our unused require() never collides with require()
+ p.symbols.items[p.require_ref.innerIndex()].original_name = "__require";
}
}
@@ -17736,6 +17741,7 @@ fn NewParser_(
.primary = p.require_ref,
.ref = declareSymbolMaybeGenerated(p, .other, logger.Loc.Empty, StaticSymbolName.List.__require.internal, true) catch unreachable,
};
+ p.runtime_imports.put(name, p.runtime_imports.__require.?);
break :brk p.runtime_imports.__require.?.ref;
}
const generated_symbol = p.declareGeneratedSymbol(.other, name) catch unreachable;
diff --git a/src/linker.zig b/src/linker.zig
index 63ed3ca0a..e193cd53a 100644
--- a/src/linker.zig
+++ b/src/linker.zig
@@ -210,7 +210,6 @@ pub const Linker = struct {
comptime allow_import_from_bundle: bool,
comptime is_bun: bool,
) !void {
- const supports_dynamic_require = comptime is_bun;
const source_dir = file_path.sourceDir();
var externals = std.ArrayList(u32).init(linker.allocator);
var needs_bundle = false;
@@ -497,11 +496,8 @@ pub const Linker = struct {
if (loader != .napi and resolved_import.shouldAssumeCommonJS(import_record.kind)) {
import_record.do_commonjs_transform_in_printer = true;
import_record.module_id = @truncate(u32, std.hash.Wyhash.hash(0, path.pretty));
-
- if (comptime !supports_dynamic_require) {
- result.ast.needs_runtime = true;
- needs_require = true;
- }
+ result.ast.needs_runtime = true;
+ needs_require = true;
}
} else |err| {
switch (err) {
@@ -594,36 +590,34 @@ pub const Linker = struct {
import_records = new_import_records;
}
- if (comptime !supports_dynamic_require) {
- // We _assume_ you're importing ESM.
- // But, that assumption can be wrong without parsing code of the imports.
- // That's where in here, we inject
- // > import {require} from 'bun:wrap';
- // Since they definitely aren't using require, we don't have to worry about the symbol being renamed.
- if (needs_require and !result.ast.uses_require_ref) {
- result.ast.uses_require_ref = true;
- require_part_import_clauses[0] = js_ast.ClauseItem{
- .alias = require_alias,
- .original_name = "",
- .alias_loc = logger.Loc.Empty,
- .name = js_ast.LocRef{
- .loc = logger.Loc.Empty,
- .ref = result.ast.require_ref,
- },
- };
-
- require_part_import_statement = js_ast.S.Import{
- .namespace_ref = Ref.None,
- .items = std.mem.span(&require_part_import_clauses),
- .import_record_index = result.ast.runtime_import_record_id.?,
- };
- require_part_stmts[0] = js_ast.Stmt{
- .data = .{ .s_import = &require_part_import_statement },
+ // We _assume_ you're importing ESM.
+ // But, that assumption can be wrong without parsing code of the imports.
+ // That's where in here, we inject
+ // > import {require} from 'bun:wrap';
+ // Since they definitely aren't using require, we don't have to worry about the symbol being renamed.
+ if (needs_require and !result.ast.uses_require_ref) {
+ result.ast.uses_require_ref = true;
+ require_part_import_clauses[0] = js_ast.ClauseItem{
+ .alias = require_alias,
+ .original_name = "",
+ .alias_loc = logger.Loc.Empty,
+ .name = js_ast.LocRef{
.loc = logger.Loc.Empty,
- };
+ .ref = result.ast.require_ref,
+ },
+ };
- result.ast.prepend_part = js_ast.Part{ .stmts = std.mem.span(&require_part_stmts) };
- }
+ require_part_import_statement = js_ast.S.Import{
+ .namespace_ref = Ref.None,
+ .items = std.mem.span(&require_part_import_clauses),
+ .import_record_index = result.ast.runtime_import_record_id.?,
+ };
+ require_part_stmts[0] = js_ast.Stmt{
+ .data = .{ .s_import = &require_part_import_statement },
+ .loc = logger.Loc.Empty,
+ };
+
+ result.ast.prepend_part = js_ast.Part{ .stmts = std.mem.span(&require_part_stmts) };
}
}
diff --git a/src/runtime.footer.bun.js b/src/runtime.footer.bun.js
index 38d5596cb..72a2703e9 100644
--- a/src/runtime.footer.bun.js
+++ b/src/runtime.footer.bun.js
@@ -3,7 +3,6 @@ export var __markAsModule = BUN_RUNTIME.__markAsModule;
export var $$lzy = BUN_RUNTIME.$$lzy;
export var __toModule = BUN_RUNTIME.__toModule;
export var __commonJS = BUN_RUNTIME.__commonJS;
-export var __require = BUN_RUNTIME.__require;
export var __name = BUN_RUNTIME.__name;
export var __export = BUN_RUNTIME.__export;
export var __reExport = BUN_RUNTIME.__reExport;
@@ -16,12 +15,12 @@ export var $$bun_runtime_json_parse = JSON.parse;
export var __internalIsCommonJSNamespace =
BUN_RUNTIME.__internalIsCommonJSNamespace;
-globalThis.require ||= function (moduleId) {
+export var __require = (globalThis.require ||= function (moduleId) {
if (typeof moduleId === "string") {
return import.meta.require(moduleId);
}
return BUN_RUNTIME.__require(moduleId);
-};
+});
globalThis.__internalIsCommonJSNamespace ||=
BUN_RUNTIME.__internalIsCommonJSNamespace;