diff options
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 78 |
1 files changed, 68 insertions, 10 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 7c227ce75..5a9bca91a 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -5798,8 +5798,7 @@ fn NewParser_( if (p.options.features.inlining) { if (p.const_values.get(ref)) |replacement| { - // TODO: - // p.ignoreUsage(ref); + p.ignoreUsage(ref); return replacement; } } @@ -20427,7 +20426,8 @@ fn NewParser_( var end: usize = 0; for (decls) |decl| { if (decl.binding.data == .b_identifier) { - if (p.const_values.contains(decl.binding.data.b_identifier.ref)) { + const symbol = p.symbols.items[decl.binding.data.b_identifier.ref.innerIndex()]; + if (p.const_values.contains(decl.binding.data.b_identifier.ref) and symbol.use_count_estimate == 0) { continue; } } @@ -21180,17 +21180,75 @@ fn NewParser_( }, logger.Loc.Empty, ); + const cjsGlobal = p.newSymbol(.unbound, "$_BunCommonJSModule_$") catch unreachable; var call_args = allocator.alloc(Expr, 6) catch unreachable; + const this_module = p.newExpr( + E.Dot{ + .name = "module", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ); // - // (function(module, exports, require, __dirname, __filename) {}).call(exports, module, exports, require, __dirname, __filename) + // (function(module, exports, require, __dirname, __filename) {}).call(this.exports, this.module, this.exports, this.require, __dirname, __filename) call_args[0..6].* = .{ - p.newExpr(E.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), - p.newExpr(E.Identifier{ .ref = p.module_ref }, logger.Loc.Empty), - p.newExpr(E.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), - p.newExpr(E.Identifier{ .ref = p.require_ref }, logger.Loc.Empty), - p.newExpr(E.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty), - p.newExpr(E.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty), + p.newExpr( + E.Dot{ + .name = "exports", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), + this_module, + p.newExpr( + E.Dot{ + .name = "exports", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), + p.newExpr( + E.Binary{ + .left = p.newExpr( + E.Dot{ + .name = "require", + .target = this_module, + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), + .op = .bin_assign, + .right = p.newExpr( + E.Dot{ + .name = "require", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), + }, + logger.Loc.Empty, + ), + p.newExpr( + E.Dot{ + .name = "__dirname", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), + p.newExpr( + E.Dot{ + .name = "__filename", + .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .name_loc = logger.Loc.Empty, + }, + logger.Loc.Empty, + ), }; const call = p.newExpr( |