aboutsummaryrefslogtreecommitdiff
path: root/src/feature_flags.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/feature_flags.zig')
-rw-r--r--src/feature_flags.zig43
1 files changed, 35 insertions, 8 deletions
diff --git a/src/feature_flags.zig b/src/feature_flags.zig
index a0db75f79..a01fea7dc 100644
--- a/src/feature_flags.zig
+++ b/src/feature_flags.zig
@@ -122,15 +122,42 @@ pub const react_server_components = true;
pub const help_catch_memory_issues = @import("bun").Environment.allow_assert;
-/// Disabled because we need to handle module scope for CJS better.
+/// This performs similar transforms as https://github.com/rollup/plugins/tree/master/packages/commonjs
///
-/// The current bugs are:
-/// - We need to handle name collisions in the top-level due to hoisted functions
-/// It breaks when multiple modules bundled together have functions with the
-/// same name at the top-level scope.
-/// - Cyclical requires need to be a de-optimization.
+/// Though, not exactly the same.
///
-/// Once fixed, it's a very meaningful bundle size improvement
-pub const commonjs_to_esm = false;
+/// There are two scenarios where this kicks in:
+///
+/// 1) You import a CommonJS module using ESM.
+///
+/// Semantically, CommonJS expects us to wrap everything in a closure. That
+/// bloats the code. We want to make the generated code as small as we can.
+///
+/// To avoid that, we attempt to unwrap the CommonJS module into ESM.
+///
+/// But, we can't always do that. When you have cyclical require() or directly
+/// mutate exported bindings, we can't unwrap it.
+///
+/// However, in the simple case, where you do something like
+///
+/// exports.foo = 123;
+/// exports.bar = 456;
+///
+/// We can unwrap it into
+///
+/// export const foo = 123;
+/// export const bar = 456;
+///
+/// 2) You import a CommonJS module using CommonJS.
+///
+/// This is a bit more complicated. We want to avoid the closure wrapper, but
+/// it's really difficult to track down all the places where you mutate the
+/// exports object. `require.cache` makes it even more complicated.
+/// So, we just wrap the entire module in a closure.
+///
+/// But what if we previously unwrapped it?
+///
+/// In that case, we wrap it again in the printer.
+pub const unwrap_commonjs_to_esm = true;
pub const boundary_based_chunking = true;