Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/js_parser/parse/parse_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Jest, ParseStatementOptions, RuntimeFeatures, RuntimeImports, ScanPassResult, StatementScope,
WrapMode,
};
use crate::scan::scan_side_effects::SideEffects;
use bun_ast as js_ast;
use bun_ast::DeclaredSymbol;
use bun_ast::{B, E, Expr, G, S, Stmt};
Expand Down Expand Up @@ -1252,7 +1253,45 @@
found
};
if let Some(found) = stmt_and_part {
let stmt = found.stmt;
let mut stmt = found.stmt;
// Without --minify-syntax the NODE_ENV if/else above survives
// as `if (<const>) {} else { X }` after DCE empties the dead
// arm; peel to the live arm's single statement. `to_boolean` is
// the same predicate `s_if` used to pick the dead arm.
Comment thread
robobun marked this conversation as resolved.
while let js_ast::StmtData::SIf(s_if) = &stmt.data {
let effects = SideEffects::to_boolean(p, &s_if.test.data);
if !effects.ok || effects.side_effects != SideEffects::NoSideEffects {

Check failure on line 1263 in src/js_parser/parse/parse_entry.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

no field `side_effects` on type `std::option::Option<scan::scan_side_effects::Known>`

Check failure on line 1263 in src/js_parser/parse/parse_entry.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

no field `ok` on type `std::option::Option<scan::scan_side_effects::Known>`
break;
}
let (live, dead) = if effects.value {

Check failure on line 1266 in src/js_parser/parse/parse_entry.rs

View check run for this annotation

Claude / Claude Code Review

Semantic merge conflict: SideEffects::to_boolean now returns Option<Known>

The merge with main (916f6cb0) pulled in a refactor where `SideEffects::to_boolean` now returns `Option<Known>` (scan_side_effects.rs:859-862), but this new peel loop still uses the old `.ok`/`.value`/`.side_effects` struct API, so the merged HEAD no longer compiles. Change to `let Some(effects) = SideEffects::to_boolean(p, &s_if.test.data) else { break };` then keep the `effects.side_effects` guard and branch on `effects.value`, matching every other caller in `visit_stmt.rs` / `visit_binary.rs`

Check failure on line 1266 in src/js_parser/parse/parse_entry.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

no field `value` on type `std::option::Option<scan::scan_side_effects::Known>`
Comment thread
claude[bot] marked this conversation as resolved.
Outdated
(Some(s_if.yes), s_if.no)
} else {
(s_if.no, Some(s_if.yes))
};
let dead_is_empty = match dead {
None => true,
Some(d) => match d.data {
js_ast::StmtData::SEmpty(_) => true,
js_ast::StmtData::SBlock(block) => block.stmts.len() == 0,
_ => false,
},
};
if !dead_is_empty {
break;
}
let Some(live_stmt) = live else { break };
match live_stmt.data {
js_ast::StmtData::SBlock(block) => {
let body = block.stmts.slice();
if body.len() != 1 {
break;
}
stmt = body[0];
}
js_ast::StmtData::SEmpty(_) => break,
_ => stmt = live_stmt,
}
}
let part = &mut parts[found.part_idx];
if p.symbols.as_slice()[p.module_ref.inner_index() as usize].use_count_estimate == 1
{
Expand Down
187 changes: 187 additions & 0 deletions test/bundler/bundler_cjs2esm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,193 @@ describe("bundler", () => {
stdout: "development",
},
});
// https://github.com/oven-sh/bun/issues/12726
itBundled("cjs2esm/ModuleExportsBasedOnNodeEnvProductionNoMinify", {
files: {
"/entry.js": /* js */ `
import { foo } from 'lib';
console.log(foo);
`,
"/node_modules/lib/index.js": /* js */ `
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./library.prod.js')
} else {
module.exports = require('./library.dev.js')
}
`,
"/node_modules/lib/library.prod.js": /* js */ `
module.exports.foo = 'production';
`,
"/node_modules/lib/library.dev.js": /* js */ `
module.exports.foo = 'FAILED';
`,
},
cjs2esm: true,
env: {
NODE_ENV: "production",
},
run: {
stdout: "production",
},
});
itBundled("cjs2esm/ModuleExportsBasedOnNodeEnvDevelopmentNoMinify", {
files: {
"/entry.js": /* js */ `
import { foo } from 'lib';
console.log(foo);
`,
"/node_modules/lib/index.js": /* js */ `
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./library.prod.js')
} else {
module.exports = require('./library.dev.js')
}
`,
"/node_modules/lib/library.prod.js": /* js */ `
module.exports.foo = 'FAILED';
`,
"/node_modules/lib/library.dev.js": /* js */ `
module.exports.foo = 'development';
`,
},
cjs2esm: true,
env: {
NODE_ENV: "development",
},
run: {
stdout: "development",
},
});
itBundled("cjs2esm/ModuleExportsBasedOnNodeEnvNoBracesNoMinify", {
files: {
"/entry.js": /* js */ `
import { foo } from 'lib';
console.log(foo);
`,
"/node_modules/lib/index.js": /* js */ `
if (process.env.NODE_ENV === 'production')
module.exports = require('./library.prod.js')
else
module.exports = require('./library.dev.js')
`,
"/node_modules/lib/library.prod.js": /* js */ `
module.exports.foo = 'FAILED';
`,
"/node_modules/lib/library.dev.js": /* js */ `
module.exports.foo = 'development';
`,
},
cjs2esm: true,
env: {
NODE_ENV: "development",
},
run: {
stdout: "development",
},
});
itBundled("cjs2esm/ModuleExportsBasedOnDefineNumberNoMinify", {
files: {
"/entry.js": /* js */ `
import { foo } from 'lib';
console.log(foo);
`,
"/node_modules/lib/index.js": /* js */ `
if (__DEV__) {
module.exports = require('./library.dev.js')
} else {
module.exports = require('./library.prod.js')
}
`,
"/node_modules/lib/library.prod.js": /* js */ `
module.exports.foo = 'production';
`,
"/node_modules/lib/library.dev.js": /* js */ `
module.exports.foo = 'FAILED';
`,
},
cjs2esm: true,
define: {
__DEV__: "0",
},
run: {
stdout: "production",
},
});
itBundled("cjs2esm/ModuleExportsBasedOnNodeEnvElseIfChainNoMinify", {
files: {
"/entry.js": /* js */ `
import { foo } from 'lib';
console.log(foo);
`,
"/node_modules/lib/index.js": /* js */ `
if (process.env.NODE_ENV === 'production') {
module.exports = require('./library.prod.js')
} else if (process.env.NODE_ENV === 'test') {
module.exports = require('./library.test.js')
} else {
module.exports = require('./library.dev.js')
}
`,
"/node_modules/lib/library.prod.js": /* js */ `
module.exports.foo = 'FAILED';
`,
"/node_modules/lib/library.test.js": /* js */ `
module.exports.foo = 'FAILED';
`,
"/node_modules/lib/library.dev.js": /* js */ `
module.exports.foo = 'development';
`,
},
cjs2esm: true,
env: {
NODE_ENV: "development",
},
run: {
stdout: "development",
},
});
itBundled("cjs2esm/ReactIndexNodeEnvRedirectNoMinify", {
files: {
"/entry.js": /* js */ `
const react = require("react");
console.log(react.version);
`,
"/node_modules/react/package.json": /* json */ `
{ "name": "react", "version": "18.3.1", "main": "./index.js" }
`,
"/node_modules/react/index.js": /* js */ `
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
`,
"/node_modules/react/cjs/react.production.min.js": /* js */ `
exports.version = 'FAILED';
`,
"/node_modules/react/cjs/react.development.js": /* js */ `
exports.version = '18.3.1-dev';
`,
},
target: "bun",
env: {
NODE_ENV: "development",
},
onAfterBundle(api) {
const code = api.readFile("out.js");
expect(code).not.toContain("react/index.js");
expect(code).not.toContain("require_react ");
expect(code).not.toContain("require_react(");
expect(code).not.toContain("react.production.min.js");
},
run: {
stdout: "18.3.1-dev",
},
});
itBundled("cjs2esm/ModuleExportsEqualsRuntimeCondition", {
files: {
"/entry.js": /* js */ `
Expand Down
4 changes: 2 additions & 2 deletions test/bundler/transpiler/jsx-dev/jsx-dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ if (!process.env.NO_BUILD) {
let shouldHaveJSX = process.env.CHILD_NODE_ENV === "production";

if (shouldHaveJSXDev) {
if (!code.includes("jsx_dev_runtime.jsxDEV")) {
if (!code.includes("jsx-dev-runtime.development") || !code.includes("jsxDEV(")) {
throw new Error("jsxDEV is not included");
}
}

if (shouldHaveJSX) {
if (!code.includes("jsx_runtime.jsx")) {
if (!code.includes("jsx-runtime.production") || code.includes("jsxDEV(")) {
throw new Error("Jsx is not included");
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/bundler/transpiler/jsx-production.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ if (!process.env.NO_BUILD) {
let shouldHaveJSX = process.env.CHILD_NODE_ENV === "production";

if (shouldHaveJSXDev) {
if (!code.includes("jsx_dev_runtime.jsxDEV")) {
if (!code.includes("jsx-dev-runtime.development") || !code.includes("jsxDEV(")) {
throw new Error("jsxDEV is not included");
}
}

if (shouldHaveJSX) {
if (!code.includes("jsx_runtime.jsx")) {
if (!code.includes("jsx-runtime.production") || code.includes("jsxDEV(")) {
throw new Error("Jsx is not included");
}
}
Expand Down
Loading