Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the resolvePath option #195

Merged
merged 1 commit into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
});
});
});