@mui/internal-babel-plugin-minify-errors 2.0.8-canary.15 → 2.0.8-canary.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +67 -0
  2. package/index.test.js +10 -0
  3. package/package.json +4 -4
package/index.js CHANGED
@@ -43,6 +43,69 @@ const COMMENT_OPT_OUT_MARKER = 'minify-error-disabled';
43
43
  * }} Options
44
44
  */
45
45
 
46
+ /**
47
+ * Checks if a node is `process.env.NODE_ENV` using Babel types.
48
+ * @param {babel.types} t
49
+ * @param {babel.types.Node} node
50
+ * @returns {boolean}
51
+ */
52
+ function isProcessEnvNodeEnv(t, node) {
53
+ return (
54
+ t.isMemberExpression(node) &&
55
+ t.isMemberExpression(node.object) &&
56
+ t.isIdentifier(node.object.object, { name: 'process' }) &&
57
+ t.isIdentifier(node.object.property, { name: 'env' }) &&
58
+ t.isIdentifier(node.property, { name: 'NODE_ENV' })
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Checks if a binary expression compares `process.env.NODE_ENV` with a value using the given operator.
64
+ * Handles both `process.env.NODE_ENV op value` and `value op process.env.NODE_ENV`.
65
+ * @param {babel.types} t
66
+ * @param {babel.types.BinaryExpression} node
67
+ * @param {string} operator
68
+ * @param {string} value
69
+ * @returns {boolean}
70
+ */
71
+ function isNodeEnvComparison(t, node, operator, value) {
72
+ if (node.operator !== operator) {
73
+ return false;
74
+ }
75
+ return (
76
+ (isProcessEnvNodeEnv(t, node.left) && t.isStringLiteral(node.right, { value })) ||
77
+ (t.isStringLiteral(node.left, { value }) && isProcessEnvNodeEnv(t, node.right))
78
+ );
79
+ }
80
+
81
+ /**
82
+ * Checks if the given path is inside a dev-only branch
83
+ * (e.g. `if (process.env.NODE_ENV !== 'production') { ... }`).
84
+ * Errors inside such branches are already stripped in production,
85
+ * so minification is unnecessary.
86
+ * @param {babel.types} t
87
+ * @param {babel.NodePath} path
88
+ * @returns {boolean}
89
+ */
90
+ function isInsideDevOnlyBranch(t, path) {
91
+ let current = path;
92
+ while (current.parentPath) {
93
+ const parent = current.parentPath;
94
+ if (parent.isIfStatement()) {
95
+ const isInConsequent = current.key === 'consequent';
96
+ const isInAlternate = current.key === 'alternate';
97
+ if ((isInConsequent || isInAlternate) && t.isBinaryExpression(parent.node.test)) {
98
+ const operator = isInConsequent ? '!==' : '===';
99
+ if (isNodeEnvComparison(t, parent.node.test, operator, 'production')) {
100
+ return true;
101
+ }
102
+ }
103
+ }
104
+ current = current.parentPath;
105
+ }
106
+ return false;
107
+ }
108
+
46
109
  /**
47
110
  * Extracts the message and expressions from a node.
48
111
  * @param {babel.types} t
@@ -113,6 +176,10 @@ function findMessageNode(t, newExpressionPath, { detection, missingError }) {
113
176
  return null;
114
177
  }
115
178
 
179
+ if (isInsideDevOnlyBranch(t, newExpressionPath)) {
180
+ return null;
181
+ }
182
+
116
183
  switch (detection) {
117
184
  case 'opt-in': {
118
185
  if (
package/index.test.js CHANGED
@@ -167,5 +167,15 @@ pluginTester({
167
167
  fixture: path.join(fixturePath, 'custom-runtime-imports-recursive', 'input.js'),
168
168
  output: readOutputFixtureSync('custom-runtime-imports-recursive', 'output.js'),
169
169
  },
170
+ {
171
+ title: 'skips errors inside dev-only branches',
172
+ pluginOptions: {
173
+ errorCodesPath: path.join(fixturePath, 'dev-only-branch', 'error-codes.json'),
174
+ runtimeModule: '@mui/utils/formatMuiErrorMessage',
175
+ detection: 'opt-out',
176
+ },
177
+ fixture: path.join(fixturePath, 'dev-only-branch', 'input.js'),
178
+ output: readOutputFixtureSync('dev-only-branch', 'output.js'),
179
+ },
170
180
  ],
171
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/internal-babel-plugin-minify-errors",
3
- "version": "2.0.8-canary.15",
3
+ "version": "2.0.8-canary.17",
4
4
  "author": "MUI Team",
5
5
  "description": "This is an internal package not meant for general use.",
6
6
  "repository": {
@@ -17,11 +17,11 @@
17
17
  "url": "https://opencollective.com/mui-org"
18
18
  },
19
19
  "dependencies": {
20
- "@babel/helper-module-imports": "^7.27.1",
20
+ "@babel/helper-module-imports": "^7.28.6",
21
21
  "find-package-json": "^1.2.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@babel/core": "7.28.5",
24
+ "@babel/core": "7.28.6",
25
25
  "@types/babel__core": "7.20.5",
26
26
  "@types/babel__helper-module-imports": "7.18.3",
27
27
  "@types/find-package-json": "1.2.7",
@@ -42,6 +42,6 @@
42
42
  "publishConfig": {
43
43
  "access": "public"
44
44
  },
45
- "gitSha": "ccf82dfe8f0e2c810ad9c6b6e2810e4e37ed27a3",
45
+ "gitSha": "a38e519df883cb7e993dd0980712643a16e5f51a",
46
46
  "scripts": {}
47
47
  }