Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react] Make development option default to the configured env #16927

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion packages/babel-preset-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export default declarePreset((api, opts: Options) => {
api.assertVersion(REQUIRED_VERSION(7));

const {
development,
development = process.env.BABEL_8_BREAKING
? api.env(env => env === "development")
: false,
importSource,
pragma,
pragmaFrag,
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-preset-react/src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default function normalizeOptions(options: any = {}) {
const development = v.validateBooleanOption(
TopLevelOptions.development,
options.development,
false,
);
let importSource = v.validateStringOption(
TopLevelOptions.importSource,
Expand Down Expand Up @@ -103,7 +102,8 @@ export default function normalizeOptions(options: any = {}) {
pragmaFrag = pragmaFrag || "React.Fragment";
}

const development = !!options.development;
const development =
options.development == null ? undefined : !!options.development;

return {
development,
Expand Down
31 changes: 31 additions & 0 deletions packages/babel-preset-react/test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as babel from "@babel/core";

import _reactPreset from "../lib/index.js";
const reactPreset = _reactPreset.default || _reactPreset;

Expand All @@ -24,4 +26,33 @@ describe("react preset", () => {
`"@babel/preset-react: 'runtime' option must be a string."`,
);
});

itBabel8("respects envName", () => {
expect(
babel.transformSync("<a />", {
configFile: false,
presets: [reactPreset],
envName: "development",
}).code,
).toMatchInlineSnapshot(`
"var _jsxFileName = \\"\\";
import { jsxDEV as _jsxDEV } from \\"react/jsx-dev-runtime\\";
/*#__PURE__*/_jsxDEV(\\"a\\", {}, void 0, false, {
fileName: _jsxFileName,
lineNumber: 1,
columnNumber: 1
}, this);"
`);

expect(
babel.transformSync("<a />", {
configFile: false,
presets: [reactPreset],
envName: "production",
}).code,
).toMatchInlineSnapshot(`
"import { jsx as _jsx } from \\"react/jsx-runtime\\";
/*#__PURE__*/_jsx(\\"a\\", {});"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("normalize options", () => {
it("default values", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"development": false,
"development": undefined,
"importSource": "react",
"pragma": undefined,
"pragmaFrag": undefined,
Expand All @@ -70,7 +70,7 @@ describe("normalize options", () => {
`);
expect(normalizeOptions({ runtime: "classic" })).toMatchInlineSnapshot(`
Object {
"development": false,
"development": undefined,
"importSource": undefined,
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("normalize options", () => {
it("default values in Babel 7", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"development": false,
"development": undefined,
"importSource": undefined,
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
Expand All @@ -113,7 +113,7 @@ describe("normalize options", () => {
`);
expect(normalizeOptions({ runtime: "automatic" })).toMatchInlineSnapshot(`
Object {
"development": false,
"development": undefined,
"importSource": undefined,
"pragma": undefined,
"pragmaFrag": undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-standalone/test/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe("@babel/standalone", () => {
"const someDiv = <div>{getMessage()}</div>",
{
presets: [["react", { runtime: "classic" }]],
envName: "production",
},
).code;
expect(output).toBe(
Expand Down
Loading