Skip to content

Commit

Permalink
Add the getRealPath option
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz committed Jun 11, 2017
1 parent 7fb28fb commit e693840
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 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`.
- `getRealPath(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 { getRealPath } from 'babel-plugin-module-resolver`'. The `opts` argument is the options object that is passed through the Babel config, after normalization.

### 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 @@ -5,6 +5,8 @@ import findBabelConfig from 'find-babel-config';
import glob from 'glob';
import pkgUp from 'pkg-up';

import defaultGetRealPath from './getRealPath';


const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
const defaultTransformedFunctions = [
Expand Down Expand Up @@ -112,12 +114,14 @@ export default function normalizeOptions(currentFile, opts) {
const alias = normalizeAlias(opts.alias);
const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
const extensions = opts.extensions || defaultExtensions;
const getRealPath = opts.getRealPath || defaultGetRealPath;

return {
cwd,
root,
alias,
transformFunctions,
extensions,
getRealPath,
};
}
3 changes: 1 addition & 2 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 getRealPath from './getRealPath';


export function nodeResolvePath(modulePath, basedir, extensions) {
Expand Down Expand Up @@ -52,7 +51,7 @@ export function mapPathString(nodePath, state) {
const currentFile = state.file.opts.filename;
const opts = state.opts;

const modulePath = getRealPath(sourcePath, currentFile, opts);
const modulePath = opts.getRealPath(sourcePath, currentFile, opts);
if (modulePath) {
nodePath.replaceWith(state.types.stringLiteral(modulePath));
}
Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Array [
"alias",
"cwd",
"extensions",
"getRealPath",
"root",
"transformFunctions",
]
Expand Down
40 changes: 40 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,44 @@ describe('module-resolver', () => {
});
});
});

describe('getRealPath', () => {
it('should work with a custom function', () => {
const rootTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './test/testproject/src',
getRealPath() {
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',
getRealPath,
}],
],
};

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

0 comments on commit e693840

Please sign in to comment.