Skip to content

Commit 24c5d1f

Browse files
committed
Describe the new feature
1 parent 7c1a475 commit 24c5d1f

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

DOCS.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ You can reference the n-th matched group with `'\\n'` (`'\\0'` refers to the who
6666

6767
To use the backslash character (`\`) just escape it like so: `'\\\\'` (double escape is needed because of JSON already using `\` for escaping).
6868

69+
### Regular expressions with a function
70+
71+
If you need even more power over the aliased path, you can pass a function in the alias configuration:
72+
73+
```js
74+
module.exports = {
75+
plugins: [
76+
["module-resolver", {
77+
alias: {
78+
"^@namespace/foo-(.+)": ([, name]) => path.join('packages', name)
79+
}
80+
}]
81+
]
82+
}
83+
```
84+
85+
Using the config from this example `'@namespace/foo-bar'` will become `'packages/bar'` or `'packages\\bar'` depending on the OS.
86+
87+
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.
88+
89+
Because the function is only called when there is a match, the argument can never be `null`.
90+
91+
*Important: a function can only be used with regular expressions.*
92+
6993
## extensions
7094

7195
An array of extensions used in the resolver.
@@ -95,7 +119,7 @@ Array of functions and methods that will have their first argument transformed.
95119
"plugins": [
96120
["module-resolver", {
97121
"transformFunctions": [
98-
"require",
122+
"require",
99123
"require.resolve",
100124
"System.import",
101125
"jest.genMockFromModule",
@@ -224,4 +248,4 @@ const realPath = resolvePath(sourcePath, currentFile, opts);
224248

225249
For each path in the file you can use `resolvePath` to get the same path that module-resolver will output.
226250

227-
`currentFile` can be either a relative path (will be resolved with respect to the CWD, not `opts.cwd`), or an absolute path.
251+
`currentFile` can be either a relative path (will be resolved with respect to the CWD, not `opts.cwd`), or an absolute path.

0 commit comments

Comments
 (0)