diff options
author | 2023-04-17 00:30:40 -0700 | |
---|---|---|
committer | 2023-04-17 00:30:40 -0700 | |
commit | 5a8cfd839041af3492f2637bfd6cc8dd9b2d1fd4 (patch) | |
tree | 976e86e4460bc4d13a653bf5c8da6590caf1dece | |
parent | a2d5e7c570aca3c2e14a991617282da93bfa30f9 (diff) | |
download | bun-5a8cfd839041af3492f2637bfd6cc8dd9b2d1fd4.tar.gz bun-5a8cfd839041af3492f2637bfd6cc8dd9b2d1fd4.tar.zst bun-5a8cfd839041af3492f2637bfd6cc8dd9b2d1fd4.zip |
Deoptimize CJS -> ESM transform on computed property access of `exports` object
Example:
```js
for (var k in constants) {
exports[k] = constants[k];
}
```
-rw-r--r-- | src/js_parser.zig | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index bf78573ed..05966b745 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -3270,7 +3270,9 @@ pub const Parser = struct { var wrapper_expr: ?Expr = null; - if (p.esm_export_keyword.len > 0 or p.top_level_await_keyword.len > 0) { + if (p.commonjs_named_exports_deoptimized and p.commonjs_named_exports.count() > 0) { + exports_kind = .cjs; + } else if (p.esm_export_keyword.len > 0 or p.top_level_await_keyword.len > 0) { exports_kind = .esm; } else if (uses_exports_ref or uses_module_ref or p.has_top_level_return) { exports_kind = .cjs; @@ -15777,6 +15779,13 @@ fn NewParser_( target.data.e_array.items.ptr[0].canBeInlinedFromPropertyAccess()) { return target.data.e_array.items.ptr[0]; + } else if (FeatureFlags.unwrap_commonjs_to_esm and + // If you're accessing the exports object in a way that is not statically analyzable + // We must deoptimize + target.data == .e_identifier and + target.data.e_identifier.ref.eql(p.exports_ref)) + { + p.deoptimizeCommonJSNamedExports(); } // Create an error for assigning to an import namespace when bundling. Even // though this is a run-time error, we make it a compile-time error when |