From 59115e0b391e2dbabba00e696120eea214c72639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ruci=C5=84ski?= Date: Fri, 7 Jul 2017 18:18:57 +0200 Subject: [PATCH] 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 --- README.md | 1 + src/normalizeOptions.js | 4 ++++ src/utils.js | 4 +--- test/index.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d5de7bb..11cea6e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/normalizeOptions.js b/src/normalizeOptions.js index 69c4077..30c55fa 100644 --- a/src/normalizeOptions.js +++ b/src/normalizeOptions.js @@ -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 = [ @@ -117,6 +119,7 @@ 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, @@ -124,6 +127,7 @@ export default createSelector( alias, transformFunctions, extensions, + resolvePath, }; }, ); diff --git a/src/utils.js b/src/utils.js index 6cad89a..8dd8907 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,7 +1,6 @@ import path from 'path'; import resolve from 'resolve'; -import resolvePath from './resolvePath'; export function nodeResolvePath(modulePath, basedir, extensions) { @@ -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; diff --git a/test/index.test.js b/test/index.test.js index e594756..f2e2774 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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, + ); + }); + }); });