Skip to content

Commit

Permalink
feat: Add the resolvePath option (#195)
Browse files Browse the repository at this point in the history
The `resolvePath` option allows end users to use a custom resolving strategy based on their own needs.

Closes #165
  • Loading branch information
fatfisz authored and tleunen committed Jul 7, 2017
1 parent 547578f commit 59115e0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Are you a plugin author (e.g. IDE integration)? We have [documented the exposed
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
- `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`.
- `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.

### Regular expression alias

Expand Down
4 changes: 4 additions & 0 deletions src/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import findBabelConfig from 'find-babel-config';
import glob from 'glob';
import pkgUp from 'pkg-up';

import defaultResolvePath from './resolvePath';


const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
const defaultTransformedFunctions = [
Expand Down Expand Up @@ -117,13 +119,15 @@ export default createSelector(
const alias = normalizeAlias(opts.alias);
const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
const extensions = opts.extensions || defaultExtensions;
const resolvePath = opts.resolvePath || defaultResolvePath;

return {
cwd,
root,
alias,
transformFunctions,
extensions,
resolvePath,
};
},
);
4 changes: 1 addition & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path';

import resolve from 'resolve';
import resolvePath from './resolvePath';


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

const sourcePath = nodePath.node.value;
const currentFile = state.file.opts.filename;
const opts = state.opts;

const modulePath = resolvePath(sourcePath, currentFile, opts);
const modulePath = state.normalizedOpts.resolvePath(sourcePath, currentFile, state.opts);
if (modulePath) {
if (nodePath.node.pathResolved) {
return;
Expand Down
40 changes: 40 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,44 @@ describe('module-resolver', () => {
});
});
});

describe('resolvePath', () => {
it('should work with a custom function', () => {
const rootTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './test/testproject/src',
resolvePath() {
return 'real path';
},
}],
],
};

testWithImport(
'app',
'real path',
rootTransformerOpts,
);
});

it('should work with the original function', () => {
const rootTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './test/testproject/src',
resolvePath,
}],
],
};

testWithImport(
'app',
'./test/testproject/src/app',
rootTransformerOpts,
);
});
});
});

0 comments on commit 59115e0

Please sign in to comment.