Skip to content

Commit

Permalink
Respect NODE_ENV/BABEL_ENV in preset-react
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 23, 2024
1 parent 816b293 commit b941657
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/babel-preset-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default declarePreset((api, opts: Options) => {
api.assertVersion(REQUIRED_VERSION(7));

const {
development,
development = process.env.BABEL_8_BREAKING ? api.env("development") : true,
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

0 comments on commit b941657

Please sign in to comment.