babel-plugin-module-name-mapper
Advanced tools
Comparing version
@@ -13,6 +13,8 @@ "use strict"; | ||
const mapModuleNames = (path, opts, filename) => { | ||
const mapModuleNames = (path, opts, fileOpts) => { | ||
const moduleNameMapper = opts.moduleNameMapper, | ||
_opts$resolveRelative = opts.resolveRelativePaths, | ||
resolveRelativePaths = _opts$resolveRelative === void 0 ? true : _opts$resolveRelative; | ||
const root = fileOpts.root, | ||
filename = fileOpts.filename; | ||
@@ -23,2 +25,3 @@ if (!moduleNameMapper) { | ||
const fileDirname = (0, _path.dirname)(filename); | ||
let nextPath = path; | ||
@@ -34,10 +37,15 @@ Object.keys(moduleNameMapper).forEach(find => { | ||
nextPath = replace; | ||
const fileDirname = (0, _path.dirname)(filename); | ||
const replaceUsesRootDir = replace.includes('<rootDir>'); | ||
if (replaceUsesRootDir) { | ||
const rootDir = (0, _utils.lazyFindPkg)(fileDirname); | ||
nextPath = nextPath.replace('<rootDir>', rootDir); | ||
nextPath = nextPath.replace('<rootDir>', root); | ||
} | ||
const replaceUsesPkgDir = replace.includes('<pkgDir>'); | ||
if (replaceUsesPkgDir) { | ||
const pkgDir = (0, _utils.lazyFindPkg)(fileDirname); | ||
nextPath = nextPath.replace('<pkgDir>', pkgDir); | ||
} | ||
const replaceUsedIndexes = replace.match(INDEXED_REPLACE_REGEX); | ||
@@ -53,4 +61,10 @@ | ||
if (resolveRelativePaths) { | ||
nextPath = (0, _path.relative)(fileDirname, nextPath); | ||
const replaceCouldBeRelative = replaceUsesRootDir || replaceUsesPkgDir; | ||
if (replaceCouldBeRelative && resolveRelativePaths) { | ||
nextPath = (0, _path.relative)(fileDirname, nextPath); // Handle siblings/children paths | ||
if (!nextPath.startsWith('.')) { | ||
nextPath = `./${nextPath}`; | ||
} | ||
} | ||
@@ -79,3 +93,3 @@ }); | ||
if (firstArg) { | ||
firstArg.value = mapModuleNames(firstArg.value, state.opts, state.file.opts.filename); | ||
firstArg.value = mapModuleNames(firstArg.value, state.opts, state.file.opts); | ||
} | ||
@@ -85,3 +99,3 @@ }, | ||
ImportDeclaration(path, state) { | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts.filename); // eslint-disable-line no-param-reassign | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts); // eslint-disable-line no-param-reassign | ||
}, | ||
@@ -91,3 +105,3 @@ | ||
if (path.node.source) { | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts.filename); // eslint-disable-line no-param-reassign | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts); // eslint-disable-line no-param-reassign | ||
} | ||
@@ -98,3 +112,3 @@ }, | ||
if (path.node.source) { | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts.filename); // eslint-disable-line no-param-reassign | ||
path.node.source.value = mapModuleNames(path.node.source.value, state.opts, state.file.opts); // eslint-disable-line no-param-reassign | ||
} | ||
@@ -101,0 +115,0 @@ } |
{ | ||
"name": "babel-plugin-module-name-mapper", | ||
"version": "1.0.4", | ||
"version": "1.1.1", | ||
"description": "Babel plugin to map module resolution", | ||
@@ -17,3 +17,3 @@ "main": "lib/index.js", | ||
"pretty": "prettier --check 'src/**/*.js'", | ||
"build": "babel --source-maps --out-dir lib/ --ignore **/__tests__ src/", | ||
"build": "babel --source-maps --out-dir lib/ --delete-dir-on-start --ignore **/__tests__ src/", | ||
"build:watch": "yarn build --watch --verbose", | ||
@@ -20,0 +20,0 @@ "prepublish": "yarn build" |
@@ -1,2 +0,2 @@ | ||
# Babel Component Resolver Plugin | ||
# Babel Module Name Mapper Plugin | ||
@@ -10,10 +10,14 @@  | ||
[](https://david-dm.org/mgcrea/babel-plugin-module-name-mapper#info=devDependencies) | ||
[](https://www.codacy.com/app/mgcrea/babel-plugin-module-name-mapper?utm_source=github.com&utm_medium=referral&utm_content=mgcrea/babel-plugin-module-name-mapper&utm_campaign=Badge_Grade) | ||
[](https://www.codacy.com/app/mgcrea/babel-plugin-module-name-mapper?utm_source=github.com&utm_medium=referral&utm_content=mgcrea/babel-plugin-module-name-mapper&utm_campaign=Badge_Coverage) | ||
[](https://www.codacy.com/app/mgcrea/babel-plugin-module-name-mapper?utm_source=github.com&utm_medium=referral&utm_content=mgcrea/babel-plugin-module-name-mapper&utm_campaign=Badge_Grade) | ||
[](https://www.codacy.com/app/mgcrea/babel-plugin-module-name-mapper?utm_source=github.com&utm_medium=referral&utm_content=mgcrea/babel-plugin-module-name-mapper&utm_campaign=Badge_Coverage) | ||
This plugin adds Jest [moduleNameMapper](https://jestjs.io/docs/en/configuration.html#modulenamemapper-object-string-string) option as a babel plugin with special support for monorepos, (which is lacking for now in Jest). | ||
This plugin adds a Jest-like [moduleNameMapper](https://jestjs.io/docs/en/configuration.html#modulenamemapper-object-string-string) option as a babel plugin. | ||
It mostly adds a `<pkgDir>` string token that will resolve to the dirname of the closest `package.json` in order to support monorepos, (which is lacking for now in both Jest/Webpack). | ||
It also supports the classic `<rootDir>` string token. | ||
## Usage | ||
Allow you to drop relative path usage for prefixed paths. | ||
This plugin allows you to drop relative path usage for prefixed paths. | ||
@@ -45,3 +49,3 @@ ```js | ||
moduleNameMapper: { | ||
'^src/(.*)': '<rootDir>/src/$1', | ||
'^src/(.*)': '<pkgDir>/src/$1', | ||
underscore: 'lodash' | ||
@@ -55,2 +59,18 @@ } | ||
### Examples | ||
```js | ||
module.exports = { | ||
moduleNameMapper: { | ||
'^src/(.*)': '<pkgDir>/src/$1', | ||
'^image![a-zA-Z0-9$_-]+$': 'GlobalImageStub', | ||
'^[./a-zA-Z0-9$_-]+\\.png$': '<rootDir>/RelativeImageStub.js', | ||
'^module_name_(.*)': '<rootDir>/substituted_module_$1.js', | ||
underscore: 'lodash' | ||
} | ||
}; | ||
``` | ||
> Note: If you provide module name without boundaries `^\$` it may cause hard to spot errors. E.g. relay will replace all modules which contain relay as a substring in its name: relay, react-relay and graphql-relay will all be pointed to your stub. | ||
### Available scripts | ||
@@ -57,0 +77,0 @@ |
Sorry, the diff of this file is not supported yet
20099
11.06%116
9.43%118
20.41%