Skip to content

Commit fcc15a4

Browse files
committed
feat: typeofReplacements option for plugin-replace
1 parent 9d9b359 commit fcc15a4

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

β€Žpackages/replace/README.mdβ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,39 @@ Default: `['\b', '\b']`
6565

6666
Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html). See [Word Boundaries](#word-boundaries) below for more information.
6767

68+
### `typeofReplacements`
69+
70+
Type: `Boolean`<br>
71+
Default: `false`
72+
73+
When replacing dot-separated object properties like `process.env.NODE_ENV`, will also replace typeof checks against the objects
74+
with `"object"`.
75+
76+
For example:
77+
78+
```js
79+
replace({
80+
values: {
81+
'process.env.NODE_ENV': '"production"',
82+
},
83+
});
84+
```
85+
86+
```js
87+
// Input
88+
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
89+
console.log('production');
90+
}
91+
// Without `typeofReplacements`
92+
if (typeof process !== 'undefined' && 'production' === 'production') {
93+
console.log('production');
94+
}
95+
// With `typeofReplacements`
96+
if ('object' !== 'undefined' && 'production' === 'production') {
97+
console.log('production');
98+
}
99+
```
100+
68101
### `preventAssignment`
69102

70103
Type: `Boolean`<br>

β€Žpackages/replace/src/index.jsβ€Ž

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function getReplacements(options) {
2424
delete values.exclude;
2525
delete values.sourcemap;
2626
delete values.sourceMap;
27+
delete values.typeofReplacements;
2728
return values;
2829
}
2930

@@ -35,10 +36,27 @@ function mapToFunctions(object) {
3536
}, {});
3637
}
3738

39+
const objKeyRegEx = /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/;
40+
function expandTypeofReplacements(replacements) {
41+
Object.keys(replacements).forEach((key) => {
42+
const objMatch = key.match(objKeyRegEx);
43+
if (!objMatch) return;
44+
let dotIndex = objMatch[1].length;
45+
let lastIndex = 0;
46+
do {
47+
replacements[`typeof ${key.slice(lastIndex, dotIndex)}`] = '"object"';
48+
lastIndex = dotIndex + 1;
49+
dotIndex = key.indexOf('.', lastIndex);
50+
} while (dotIndex !== -1);
51+
});
52+
}
53+
3854
export default function replace(options = {}) {
3955
const filter = createFilter(options.include, options.exclude);
40-
const { delimiters, preventAssignment } = options;
41-
const functionValues = mapToFunctions(getReplacements(options));
56+
const { delimiters, preventAssignment, typeofReplacements } = options;
57+
const replacements = getReplacements(options);
58+
if (typeofReplacements) expandTypeofReplacements(replacements);
59+
const functionValues = mapToFunctions(replacements);
4260
const keys = Object.keys(functionValues).sort(longest).map(escape);
4361
const lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
4462
const pattern = delimiters
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
description: 'Handles process type guards in replacements',
3+
options: {
4+
'process.env.NODE_ENV': '"production"',
5+
preventAssignment: true,
6+
typeofReplacements: true
7+
}
8+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
2+
console.log('production');
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.env.DEBUG = 'test';

β€Žpackages/replace/test/snapshots/form.js.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ Generated by [AVA](https://ava.li).
6464
> Snapshot 1
6565
6666
'process.env.DEBUG = \'test\';'
67+
68+
## process-check: Handles process type guards in replacements
69+
70+
> Snapshot 1
71+
72+
`if ("object" !== 'undefined' && "production" ==='production') {␊
73+
console.log('production');␊
74+
}`
88 Bytes
Binary file not shown.

0 commit comments

Comments
Β (0)