diff options
author | 2022-09-06 03:37:33 -0700 | |
---|---|---|
committer | 2022-09-06 03:37:58 -0700 | |
commit | 93cfc7edbb339678e8b97aa1cfbf424a51435909 (patch) | |
tree | 9c71f91d1b7b646cb9e32609d3870e6ddf81b7db /src/js_parser.zig | |
parent | 5b6a2d9efe65bdb67e62a03a804f95b0eb4a53ed (diff) | |
download | bun-93cfc7edbb339678e8b97aa1cfbf424a51435909.tar.gz bun-93cfc7edbb339678e8b97aa1cfbf424a51435909.tar.zst bun-93cfc7edbb339678e8b97aa1cfbf424a51435909.zip |
preserve statements when generating a separate module for bun plugin
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 164f915c4..eda109540 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -12488,13 +12488,40 @@ fn NewParser_( if (_stmts.len == 1 and p.options.features.hoist_bun_plugin and !p.bun_plugin.ref.isNull()) { const bun_plugin_usage_count_after: usize = p.symbols.items[p.bun_plugin.ref.innerIndex()].use_count_estimate; if (bun_plugin_usage_count_after > bun_plugin_usage_count_before) { + var previous_parts: []js_ast.Part = parts.items; + + for (previous_parts) |*previous_part, j| { + if (previous_part.stmts.len == 0) continue; + + const declared_symbols = previous_part.declared_symbols; + + for (declared_symbols) |decl| { + if (p.symbol_uses.contains(decl.ref)) { + // we move this part to our other file + for (previous_parts[0..j]) |*this_part| { + if (this_part.stmts.len == 0) continue; + const this_declared_symbols = this_part.declared_symbols; + for (this_declared_symbols) |this_decl| { + if (previous_part.symbol_uses.contains(this_decl.ref)) { + try p.bun_plugin.hoisted_stmts.appendSlice(p.allocator, this_part.stmts); + this_part.stmts = &.{}; + break; + } + } + } + + try p.bun_plugin.hoisted_stmts.appendSlice(p.allocator, previous_part.stmts); + break; + } + } + } + p.bun_plugin.hoisted_stmts.append(p.allocator, _stmts[0]) catch unreachable; + // Single-statement part which uses Bun.plugin() // It's effectively an unrelated file if (p.declared_symbols.items.len > 0 or p.symbol_uses.count() > 0) { p.clearSymbolUsagesFromDeadPart(.{ .stmts = undefined, .declared_symbols = p.declared_symbols.items, .symbol_uses = p.symbol_uses }); } - - p.bun_plugin.hoisted_stmts.append(p.allocator, _stmts[0]) catch unreachable; return; } } |