Skip to content

Commit 59115e0

Browse files
fatfisztleunen
authored andcommitted
feat: Add the resolvePath option (#195)
The `resolvePath` option allows end users to use a custom resolving strategy based on their own needs. Closes #165
1 parent 547578f commit 59115e0

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Are you a plugin author (e.g. IDE integration)? We have [documented the exposed
5454
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
5555
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
5656
- `transformFunctions`: Array of functions and methods that will have their first argument transformed. By default those methods are: `require`, `require.resolve`, `System.import`, `jest.genMockFromModule`, `jest.mock`, `jest.unmock`, `jest.doMock`, `jest.dontMock`.
57+
- `resolvePath(sourcePath, currentFile, opts)`: A function that is called for each path in the file. By default module-resolver is using an internal function, exposed like so: `import { resolvePath } from 'babel-plugin-module-resolver`'. The `opts` argument is the options object that is passed through the Babel config.
5758

5859
### Regular expression alias
5960

src/normalizeOptions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import findBabelConfig from 'find-babel-config';
66
import glob from 'glob';
77
import pkgUp from 'pkg-up';
88

9+
import defaultResolvePath from './resolvePath';
10+
911

1012
const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
1113
const defaultTransformedFunctions = [
@@ -117,13 +119,15 @@ export default createSelector(
117119
const alias = normalizeAlias(opts.alias);
118120
const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
119121
const extensions = opts.extensions || defaultExtensions;
122+
const resolvePath = opts.resolvePath || defaultResolvePath;
120123

121124
return {
122125
cwd,
123126
root,
124127
alias,
125128
transformFunctions,
126129
extensions,
130+
resolvePath,
127131
};
128132
},
129133
);

src/utils.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path';
22

33
import resolve from 'resolve';
4-
import resolvePath from './resolvePath';
54

65

76
export function nodeResolvePath(modulePath, basedir, extensions) {
@@ -50,9 +49,8 @@ export function mapPathString(nodePath, state) {
5049

5150
const sourcePath = nodePath.node.value;
5251
const currentFile = state.file.opts.filename;
53-
const opts = state.opts;
5452

55-
const modulePath = resolvePath(sourcePath, currentFile, opts);
53+
const modulePath = state.normalizedOpts.resolvePath(sourcePath, currentFile, state.opts);
5654
if (modulePath) {
5755
if (nodePath.node.pathResolved) {
5856
return;

test/index.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,4 +847,44 @@ describe('module-resolver', () => {
847847
});
848848
});
849849
});
850+
851+
describe('resolvePath', () => {
852+
it('should work with a custom function', () => {
853+
const rootTransformerOpts = {
854+
babelrc: false,
855+
plugins: [
856+
[plugin, {
857+
root: './test/testproject/src',
858+
resolvePath() {
859+
return 'real path';
860+
},
861+
}],
862+
],
863+
};
864+
865+
testWithImport(
866+
'app',
867+
'real path',
868+
rootTransformerOpts,
869+
);
870+
});
871+
872+
it('should work with the original function', () => {
873+
const rootTransformerOpts = {
874+
babelrc: false,
875+
plugins: [
876+
[plugin, {
877+
root: './test/testproject/src',
878+
resolvePath,
879+
}],
880+
],
881+
};
882+
883+
testWithImport(
884+
'app',
885+
'./test/testproject/src/app',
886+
rootTransformerOpts,
887+
);
888+
});
889+
});
850890
});

0 commit comments

Comments
 (0)