diff options
author | 2023-04-16 20:15:47 -0700 | |
---|---|---|
committer | 2023-04-16 20:15:47 -0700 | |
commit | c916a55eadc6cc730084ae6851a57fcb8d9b85e6 (patch) | |
tree | cbfce074a11f53f51c39030a4b7e1bd4d70232f2 /src | |
parent | 31319c01771da83aba58716289c44bdc9db6391e (diff) | |
download | bun-c916a55eadc6cc730084ae6851a57fcb8d9b85e6.tar.gz bun-c916a55eadc6cc730084ae6851a57fcb8d9b85e6.tar.zst bun-c916a55eadc6cc730084ae6851a57fcb8d9b85e6.zip |
Lazily wrap
Diffstat (limited to '')
-rw-r--r-- | src/bundler/bundle_v2.zig | 30 | ||||
-rw-r--r-- | src/js_ast.zig | 4 | ||||
-rw-r--r-- | src/js_parser.zig | 2 |
3 files changed, 24 insertions, 12 deletions
diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index 532bbaad1..5faa3c893 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -2904,7 +2904,8 @@ const LinkerContext = struct { this.cycle_detector.clearRetainingCapacity(); var wrapper_part_indices = this.graph.meta.items(.wrapper_part_index); var imports_to_bind = this.graph.meta.items(.imports_to_bind); - + var to_mark_as_esm_with_dynamic_fallback = std.AutoArrayHashMap(u32, void).init(this.allocator); + defer to_mark_as_esm_with_dynamic_fallback.deinit(); for (reachable) |source_index_| { const source_index = source_index_.get(); const id = source_index; @@ -2920,6 +2921,7 @@ const LinkerContext = struct { named_imports_, &imports_to_bind[id], source_index, + &to_mark_as_esm_with_dynamic_fallback, ); if (this.log.errors > 0) { @@ -2957,6 +2959,12 @@ const LinkerContext = struct { source_index, ); } + + // When we hit an unknown import on a file that started as CommonJS + // We make it an ESM file with dynamic fallback. + for (to_mark_as_esm_with_dynamic_fallback.keys()) |id| { + this.graph.ast.items(.exports_kind)[id] = .esm_with_dynamic_fallback; + } } // Step 5: Create namespace exports for every file. This is always necessary @@ -6968,6 +6976,7 @@ const LinkerContext = struct { c: *LinkerContext, init_tracker: *ImportTracker, re_exports: *std.ArrayList(js_ast.Dependency), + to_mark_as_esm_with_dynamic_fallback: *std.AutoArrayHashMap(u32, void), ) MatchImport { var tracker = init_tracker; var ambiguous_results = std.ArrayList(MatchImport).init(c.allocator); @@ -7056,21 +7065,17 @@ const LinkerContext = struct { .dynamic_fallback_interop_default => { const named_import: js_ast.NamedImport = named_imports[prev_source_index].get(prev_import_ref).?; + to_mark_as_esm_with_dynamic_fallback.put(other_id, {}) catch unreachable; if (named_import.namespace_ref != null and named_import.namespace_ref.?.isValid()) { - // TODO: why do we need to do this? - // Why is the namespace_ref incorrect? It implies we need to link - // it somewhere - const ref_to_use = c.graph.ast.items(.exports_ref)[other_id]; - // If the file was rewritten to ESM from CJS, the "default" export should alias to the namespace export. if (strings.eqlComptime(named_import.alias orelse "", "default")) { result.kind = .normal; - result.ref = ref_to_use; + result.ref = c.graph.ast.items(.exports_ref)[other_id]; result.name_loc = named_import.alias_loc orelse Logger.Loc.Empty; } else { result.kind = .normal_and_namespace; - result.namespace_ref = ref_to_use; + result.namespace_ref = c.graph.ast.items(.exports_ref)[other_id]; result.alias = named_import.alias.?; result.name_loc = named_import.alias_loc orelse Logger.Loc.Empty; } @@ -7167,7 +7172,7 @@ const LinkerContext = struct { var old_cycle_detector = c.cycle_detector; c.cycle_detector = c.swap_cycle_detector; - var ambig = c.matchImportWithExport(&ambiguous_tracker.data, re_exports); + var ambig = c.matchImportWithExport(&ambiguous_tracker.data, re_exports, to_mark_as_esm_with_dynamic_fallback); c.cycle_detector.clearRetainingCapacity(); c.swap_cycle_detector = c.cycle_detector; c.cycle_detector = old_cycle_detector; @@ -7485,13 +7490,14 @@ const LinkerContext = struct { } // Is this a file with dynamic exports? - if (other_kind == .esm_with_dynamic_fallback) { + const is_commonjs_to_esm = other_kind == .esm_with_dynamic_fallback_from_cjs; + if (other_kind == .esm_with_dynamic_fallback or is_commonjs_to_esm) { return .{ .value = .{ .source_index = Index.source(other_source_index), .import_ref = c.graph.ast.items(.exports_ref)[other_id], }, - .status = if (c.graph.ast.items(.commonjs_named_exports)[other_source_index].count() > 0) + .status = if (is_commonjs_to_esm) .dynamic_fallback_interop_default else .dynamic_fallback, @@ -7522,6 +7528,7 @@ const LinkerContext = struct { named_imports_ptr: *JSAst.NamedImports, imports_to_bind: *RefImportData, source_index: Index.Int, + to_mark_as_esm_with_dynamic_fallback: *std.AutoArrayHashMap(u32, void), ) void { var named_imports = named_imports_ptr.cloneWithAllocator(c.allocator) catch unreachable; defer named_imports_ptr.* = named_imports; @@ -7557,6 +7564,7 @@ const LinkerContext = struct { var result = c.matchImportWithExport( &import_tracker.data, &re_exports, + to_mark_as_esm_with_dynamic_fallback, ); switch (result.kind) { diff --git a/src/js_ast.zig b/src/js_ast.zig index 6f3298102..9173ce6eb 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -5525,6 +5525,10 @@ pub const ExportsKind = enum { // directly. Other imports go through property accesses on "exports". esm_with_dynamic_fallback, + // Like "esm_with_dynamic_fallback", but the module was originally a CommonJS + // module. + esm_with_dynamic_fallback_from_cjs, + pub fn isDynamic(self: ExportsKind) bool { return self == .esm_with_dynamic_fallback or self == .cjs; } diff --git a/src/js_parser.zig b/src/js_parser.zig index 45e585d27..23e17a58d 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -3290,7 +3290,7 @@ pub const Parser = struct { } if (exports_kind == .esm and p.commonjs_named_exports.count() > 0 and !p.unwrap_all_requires) { - exports_kind = .esm_with_dynamic_fallback; + exports_kind = .esm_with_dynamic_fallback_from_cjs; } // Auto inject jest globals into the test file |