You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`'foo/baz'` will become `'bar/baz'` (`name` includes the slash in this case)
97
-
*`'@namespace/foo-bar'` will become `'packages/bar'`
106
+
107
+
-`'foo'` will become `'bar'` (`name` is empty)
108
+
-`'foo/baz'` will become `'bar/baz'` (`name` includes the slash in this case)
109
+
-`'@namespace/foo-bar'` will become `'packages/bar'`
98
110
99
111
The only argument is the result of calling `RegExp.prototype.exec` on the matched path. It's an array with the matched string and all matched groups.
100
112
@@ -137,23 +149,33 @@ An array of extensions that will be stripped from file paths. Defaults to the `e
137
149
## cwd
138
150
139
151
By default, the working directory is the one used for the resolver, but you can override it for your project.
140
-
* The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
152
+
153
+
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
154
+
141
155
```json
142
156
{
143
157
"plugins": [
144
-
["module-resolver", {
145
-
"cwd": "babelrc"
146
-
}]
158
+
[
159
+
"module-resolver",
160
+
{
161
+
"cwd": "babelrc"
162
+
}
163
+
]
147
164
]
148
165
}
149
166
```
150
-
* The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
167
+
168
+
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
169
+
151
170
```json
152
171
{
153
172
"plugins": [
154
-
["module-resolver", {
155
-
"cwd": "packagejson"
156
-
}]
173
+
[
174
+
"module-resolver",
175
+
{
176
+
"cwd": "packagejson"
177
+
}
178
+
]
157
179
]
158
180
}
159
181
```
@@ -165,8 +187,10 @@ Array of functions and methods that will have their first argument transformed.
165
187
```json
166
188
{
167
189
"plugins": [
168
-
["module-resolver", {
169
-
"transformFunctions": [
190
+
[
191
+
"module-resolver",
192
+
{
193
+
"transformFunctions": [
170
194
"require",
171
195
"require.resolve",
172
196
"System.import",
@@ -175,8 +199,9 @@ Array of functions and methods that will have their first argument transformed.
175
199
"jest.unmock",
176
200
"jest.doMock",
177
201
"jest.dontMock"
178
-
]
179
-
}]
202
+
]
203
+
}
204
+
]
180
205
]
181
206
}
182
207
```
@@ -188,21 +213,24 @@ A function that is called to resolve each path in the project. By default `modul
188
213
```js
189
214
module.exports= {
190
215
plugins: [
191
-
["module-resolver", {
192
-
extensions: [".js"],
193
-
resolvePath(sourcePath, currentFile, opts) {
194
-
/**
195
-
* The `opts` argument is the options object that is passed through the Babel config.
196
-
* opts = {
197
-
* extensions: [".js"],
198
-
* resolvePath: ...,
199
-
* }
200
-
*/
201
-
return"resolvedPath";
202
-
}
203
-
}]
204
-
]
205
-
}
216
+
[
217
+
'module-resolver',
218
+
{
219
+
extensions: ['.js'],
220
+
resolvePath(sourcePath, currentFile, opts) {
221
+
/**
222
+
* The `opts` argument is the options object that is passed through the Babel config.
223
+
* opts = {
224
+
* extensions: [".js"],
225
+
* resolvePath: ...,
226
+
* }
227
+
*/
228
+
return'resolvedPath';
229
+
},
230
+
},
231
+
],
232
+
],
233
+
};
206
234
```
207
235
208
236
If you want to leave some paths as-is, then you can return `undefined` or any other falsy value from the function.
@@ -214,14 +242,17 @@ One of the [NPM log level options](https://docs.npmjs.com/misc/config#loglevel)
214
242
```js
215
243
module.exports= {
216
244
plugins: [
217
-
["module-resolver", {
218
-
alias: {
219
-
"dependency-string":"module-that-does-not-exist"// warning will not log
245
+
[
246
+
'module-resolver',
247
+
{
248
+
alias: {
249
+
'dependency-string':'module-that-does-not-exist', // warning will not log
250
+
},
251
+
loglevel:'silent',
220
252
},
221
-
loglevel:'silent'
222
-
}]
223
-
]
224
-
}
253
+
],
254
+
],
255
+
};
225
256
```
226
257
227
258
# Usage with create-react-app
@@ -250,7 +281,7 @@ create-react-app by default don't use .babelrc, so in webpack.config.dev.js, add
250
281
251
282
# Usage with React Native
252
283
253
-
To let the packager resolve the right module for each platform, you have to add the ```.ios.js```and ```.android.js``` extensions :
284
+
To let the packager resolve the right module for each platform, you have to add the `.ios.js`and `.android.js` extensions :
254
285
255
286
```json
256
287
{
@@ -268,17 +299,18 @@ To let the packager resolve the right module for each platform, you have to add
268
299
269
300
# Usage with Proxyquire
270
301
271
-
If you use the mocking library [proxyquire](https://github.com/thlorenz/proxyquire), or otherwise need to define path strings which aren't direct arguments to `transformFunctions`, you have a problem: the plug-in won't convert them.
302
+
If you use the mocking library [proxyquire](https://github.com/thlorenz/proxyquire), or otherwise need to define path strings which aren't direct arguments to `transformFunctions`, you have a problem: the plug-in won't convert them.
272
303
273
304
Because proxyquire expects paths not just as direct arguments, but also as object keys, simply adding proxyquire to `transformFunctions` isn't enough:
274
305
275
306
```js
276
-
const { functionToTest } =proxyquire('~/modifiedPathToTestedModule', { // this path will be converted
277
-
'~/modifiedPathToDependency': { mockVersionOfDependency } // this path won't be converted
'~/modifiedPathToDependency': { mockVersionOfDependency }, // this path won't be converted
278
310
});
279
311
```
280
312
281
-
The solution in this case is to use or create a function like Lodash's/Underscore's `_.identity`, which simply returns its argument. Next, add it to `transformFunctions`, and then use it to convert the problematic path string:
313
+
The solution in this case is to use or create a function like Lodash's/Underscore's `_.identity`, which simply returns its argument. Next, add it to `transformFunctions`, and then use it to convert the problematic path string:
282
314
283
315
```json
284
316
"transformFunctions": [
@@ -288,9 +320,10 @@ The solution in this case is to use or create a function like Lodash's/Underscor
288
320
```
289
321
290
322
```js
291
-
constresolvePath=x=> x;
292
-
const { functionToTest } =proxyquire('~/modifiedPathToTestedModule', { // this path will be converted
293
-
[resolvePath('~/modifiedPathToDependency')]: { mockVersionOfDependency } // this path will be converted
0 commit comments