Skip to content

Commit

Permalink
fix(replace)!: objectGuards doesn't take effects (#1764)
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Fixed the problem that the objectGuards option did not take effect, so the bundle results before and after may be inconsistent

Co-authored-by: shellscape <[email protected]>
  • Loading branch information
ikkz and shellscape committed Sep 23, 2024
1 parent 1e73857 commit 8c213e0
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 20 deletions.
20 changes: 2 additions & 18 deletions packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,10 @@ function expandTypeofReplacements(replacements) {
const objMatch = key.match(objKeyRegEx);
if (!objMatch) return;
let dotIndex = objMatch[1].length;
let lastIndex = 0;
do {
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)} ===`] = '"object" ===';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)} !==`] = '"object" !==';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)}===`] = '"object"===';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)}!==`] = '"object"!==';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)} ==`] = '"object" ===';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)} !=`] = '"object" !==';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)}==`] = '"object"===';
// eslint-disable-next-line no-param-reassign
replacements[`typeof ${key.slice(lastIndex, dotIndex)}!=`] = '"object"!==';
lastIndex = dotIndex + 1;
dotIndex = key.indexOf('.', lastIndex);
replacements[`typeof ${key.slice(0, dotIndex)}`] = '"object"';
dotIndex = key.indexOf('.', dotIndex + 1);
} while (dotIndex !== -1);
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/replace/test/fixtures/form/process-check/input.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
if (typeof process !== 'undefined' && typeof process.env === "object" && process.env.NODE_ENV === 'production') {
console.log('production');
}
2 changes: 1 addition & 1 deletion packages/replace/test/snapshots/form.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev).

> Snapshot 1
`if (typeof process !== 'undefined' && "production" === 'production') {␊
`if ("object" !== 'undefined' && "object" === "object" && "production" === 'production') {␊
console.log('production');␊
}`

Expand Down
Binary file modified packages/replace/test/snapshots/form.js.snap
Binary file not shown.

0 comments on commit 8c213e0

Please sign in to comment.