Skip to content

Commit

Permalink
fix(replace): objectGuards doesn't take effects
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
  • Loading branch information
ikkz committed Sep 23, 2024
1 parent 28b9701 commit 6b4ee00
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 6b4ee00

Please sign in to comment.