babel-plugin-module-resolver
Advanced tools
Comparing version 4.1.0 to 5.0.0
@@ -5,16 +5,10 @@ "use strict"; | ||
exports.default = void 0; | ||
var _normalizeOptions = _interopRequireDefault(require("./normalizeOptions")); | ||
var _resolvePath = _interopRequireDefault(require("./resolvePath")); | ||
exports.resolvePath = _resolvePath.default; | ||
var _call = _interopRequireDefault(require("./transformers/call")); | ||
var _import = _interopRequireDefault(require("./transformers/import")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Public API for external plugins | ||
// Public API for external plugins | ||
const importVisitors = { | ||
@@ -29,10 +23,7 @@ CallExpression: _call.default, | ||
}, | ||
exit(programPath, state) { | ||
programPath.traverse(importVisitors, state); | ||
} | ||
} | ||
}; | ||
var _default = ({ | ||
@@ -42,3 +33,2 @@ types | ||
name: 'module-resolver', | ||
manipulateOptions(opts) { | ||
@@ -49,20 +39,15 @@ if (opts.filename === undefined) { | ||
}, | ||
pre(file) { | ||
this.types = types; | ||
const currentFile = file.opts.filename; | ||
this.normalizedOpts = (0, _normalizeOptions.default)(currentFile, this.opts); // We need to keep track of all handled nodes so we do not try to transform them twice, | ||
this.normalizedOpts = (0, _normalizeOptions.default)(currentFile, this.opts); | ||
// We need to keep track of all handled nodes so we do not try to transform them twice, | ||
// because we run before (enter) and after (exit) all nodes are handled | ||
this.moduleResolverVisited = new Set(); | ||
}, | ||
visitor, | ||
post() { | ||
this.moduleResolverVisited.clear(); | ||
} | ||
}); | ||
exports.default = _default; |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.warn = warn; | ||
// This module exists only for abstracting logging away and making testing easier | ||
// This module exists only for abstracting logging away and making testing easier | ||
// eslint-disable-next-line import/prefer-default-export | ||
@@ -9,0 +9,0 @@ function warn(...args) { |
@@ -5,7 +5,4 @@ "use strict"; | ||
exports.default = mapPathString; | ||
var _resolvePath = _interopRequireDefault(require("./resolvePath")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function mapPathString(nodePath, state) { | ||
@@ -15,3 +12,2 @@ if (!state.types.isStringLiteral(nodePath)) { | ||
} | ||
const sourcePath = nodePath.node.value; | ||
@@ -21,3 +17,2 @@ const currentFile = state.file.opts.filename; | ||
const modulePath = resolvePath(sourcePath, currentFile, state.opts); | ||
if (modulePath) { | ||
@@ -27,3 +22,2 @@ if (nodePath.node.pathResolved) { | ||
} | ||
nodePath.replaceWith(state.types.stringLiteral(modulePath)); | ||
@@ -30,0 +24,0 @@ nodePath.node.pathResolved = true; |
@@ -5,20 +5,12 @@ "use strict"; | ||
exports.default = mapToRelative; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function mapToRelative(cwd, currentFile, module) { | ||
let from = _path.default.dirname(currentFile); | ||
let to = _path.default.normalize(module); | ||
from = _path.default.resolve(cwd, from); | ||
to = _path.default.resolve(cwd, to); | ||
const moduleMapped = _path.default.relative(from, to); | ||
return (0, _utils.toPosixPath)(moduleMapped); | ||
} |
@@ -5,28 +5,19 @@ "use strict"; | ||
exports.default = void 0; | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _reselect = require("reselect"); | ||
var _findBabelConfig = _interopRequireDefault(require("find-babel-config")); | ||
var _glob = _interopRequireDefault(require("glob")); | ||
var _pkgUp = _interopRequireDefault(require("pkg-up")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs']; | ||
const defaultTransformedFunctions = ['require', 'require.resolve', 'System.import', // Jest methods | ||
'jest.genMockFromModule', 'jest.mock', 'jest.unmock', 'jest.doMock', 'jest.dontMock', 'jest.setMock', 'jest.requireActual', 'jest.requireMock', // Older Jest methods | ||
const defaultTransformedFunctions = ['require', 'require.resolve', 'System.import', | ||
// Jest methods | ||
'jest.genMockFromModule', 'jest.mock', 'jest.unmock', 'jest.doMock', 'jest.dontMock', 'jest.setMock', 'jest.requireActual', 'jest.requireMock', | ||
// Older Jest methods | ||
'require.requireActual', 'require.requireMock']; | ||
function isRegExp(string) { | ||
return string.startsWith('^') || string.endsWith('$'); | ||
} | ||
const specialCwd = { | ||
@@ -38,6 +29,4 @@ babelrc: startPath => _findBabelConfig.default.sync(startPath).file, | ||
}; | ||
function normalizeCwd(optsCwd, currentFile) { | ||
let cwd; | ||
if (optsCwd in specialCwd) { | ||
@@ -50,6 +39,4 @@ const startPath = currentFile === 'unknown' ? './' : currentFile; | ||
} | ||
return cwd || process.cwd(); | ||
} | ||
function normalizeRoot(optsRoot, cwd) { | ||
@@ -59,3 +46,2 @@ if (!optsRoot) { | ||
} | ||
const rootArray = Array.isArray(optsRoot) ? optsRoot : [optsRoot]; | ||
@@ -65,10 +51,7 @@ return rootArray.map(dirPath => _path.default.resolve(cwd, dirPath)).reduce((resolvedDirs, absDirPath) => { | ||
const roots = _glob.default.sync(absDirPath).filter(resolvedPath => _fs.default.lstatSync(resolvedPath).isDirectory()); | ||
return [...resolvedDirs, ...roots]; | ||
} | ||
return [...resolvedDirs, absDirPath]; | ||
}, []); | ||
} | ||
function getAliasTarget(key, isKeyRegExp) { | ||
@@ -78,3 +61,2 @@ const regExpPattern = isKeyRegExp ? key : `^${(0, _utils.escapeRegExp)(key)}(/.*|)$`; | ||
} | ||
function getAliasSubstitute(value, isKeyRegExp) { | ||
@@ -84,3 +66,2 @@ if (typeof value === 'function') { | ||
} | ||
if (!isKeyRegExp) { | ||
@@ -92,11 +73,8 @@ return ([, match]) => { | ||
} | ||
return `${value}${match}`; | ||
}; | ||
} | ||
const parts = value.split('\\\\'); | ||
return execResult => parts.map(part => part.replace(/\\\d+/g, number => execResult[number.slice(1)] || '')).join('\\'); | ||
} | ||
function normalizeAlias(optsAlias) { | ||
@@ -106,3 +84,2 @@ if (!optsAlias) { | ||
} | ||
const aliasArray = Array.isArray(optsAlias) ? optsAlias : [optsAlias]; | ||
@@ -118,3 +95,2 @@ return aliasArray.reduce((aliasPairs, alias) => { | ||
} | ||
function normalizeTransformedFunctions(optsTransformFunctions) { | ||
@@ -124,11 +100,9 @@ if (!optsTransformFunctions) { | ||
} | ||
return [...defaultTransformedFunctions, ...optsTransformFunctions]; | ||
} | ||
function normalizeLoglevel(optsLoglevel) { | ||
return optsLoglevel || 'warn'; | ||
} | ||
var _default = (0, _reselect.createSelector)( // The currentFile should have an extension; otherwise it's considered a special value | ||
var _default = (0, _reselect.createSelector)( | ||
// The currentFile should have an extension; otherwise it's considered a special value | ||
currentFile => currentFile.includes('.') ? _path.default.dirname(currentFile) : currentFile, (_, opts) => opts, (currentFile, opts) => { | ||
@@ -153,3 +127,2 @@ const cwd = normalizeCwd(opts.cwd, currentFile); | ||
}); | ||
exports.default = _default; |
@@ -5,29 +5,17 @@ "use strict"; | ||
exports.default = resolvePath; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _log = require("./log"); | ||
var _mapToRelative = _interopRequireDefault(require("./mapToRelative")); | ||
var _normalizeOptions = _interopRequireDefault(require("./normalizeOptions")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) { | ||
const realSourceFileExtension = _path.default.extname(absFileInRoot); | ||
const sourceFileExtension = _path.default.extname(sourcePath); | ||
let relativePath = (0, _mapToRelative.default)(opts.cwd, currentFile, absFileInRoot); | ||
if (realSourceFileExtension !== sourceFileExtension) { | ||
relativePath = (0, _utils.replaceExtension)(relativePath, opts); | ||
} | ||
return (0, _utils.toLocalPath)((0, _utils.toPosixPath)(relativePath)); | ||
} | ||
function findPathInRoots(sourcePath, { | ||
@@ -45,16 +33,11 @@ extensions, | ||
} | ||
function resolvePathFromRootConfig(sourcePath, currentFile, opts) { | ||
const absFileInRoot = findPathInRoots(sourcePath, opts); | ||
if (!absFileInRoot) { | ||
return null; | ||
} | ||
return getRelativePath(sourcePath, currentFile, absFileInRoot, opts); | ||
} | ||
function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) { | ||
const resolvedPath = (0, _utils.nodeResolvePath)(modulePath, currentFile, extensions); | ||
if (resolvedPath === null && loglevel !== 'silent') { | ||
@@ -64,3 +47,2 @@ (0, _log.warn)(`Could not resolve "${modulePath}" in file ${currentFile}.`); | ||
} | ||
function resolvePathFromAliasConfig(sourcePath, currentFile, opts) { | ||
@@ -70,16 +52,13 @@ let aliasedSourceFile; | ||
const execResult = regExp.exec(sourcePath); | ||
if (execResult === null) { | ||
return false; | ||
} | ||
aliasedSourceFile = substitute(execResult); | ||
return true; | ||
}); | ||
if (!aliasedSourceFile) { | ||
return null; | ||
} // Alias with array of paths | ||
} | ||
// Alias with array of paths | ||
if (Array.isArray(aliasedSourceFile)) { | ||
@@ -90,20 +69,14 @@ return aliasedSourceFile.map(asf => { | ||
} | ||
return asf; | ||
}).find(src => (0, _utils.nodeResolvePath)(src, _path.default.dirname(currentFile), opts.extensions)); | ||
} | ||
if ((0, _utils.isRelativePath)(aliasedSourceFile)) { | ||
return (0, _utils.toLocalPath)((0, _utils.toPosixPath)((0, _mapToRelative.default)(opts.cwd, currentFile, aliasedSourceFile))); | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel); | ||
} | ||
return aliasedSourceFile; | ||
} | ||
const resolvers = [resolvePathFromAliasConfig, resolvePathFromRootConfig]; | ||
function resolvePath(sourcePath, currentFile, opts) { | ||
@@ -113,8 +86,7 @@ if ((0, _utils.isRelativePath)(sourcePath)) { | ||
} | ||
const normalizedOpts = (0, _normalizeOptions.default)(currentFile, opts); | ||
const normalizedOpts = (0, _normalizeOptions.default)(currentFile, opts); // File param is a relative path from the environment current working directory | ||
// File param is a relative path from the environment current working directory | ||
// (not from cwd param) | ||
const absoluteCurrentFile = _path.default.resolve(currentFile); | ||
let resolvedPath = null; | ||
@@ -121,0 +93,0 @@ resolvers.some(resolver => { |
@@ -5,9 +5,5 @@ "use strict"; | ||
exports.default = transformCall; | ||
var _utils = require("../utils"); | ||
var _mapPath = _interopRequireDefault(require("../mapPath")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function transformCall(nodePath, state) { | ||
@@ -17,6 +13,4 @@ if (state.moduleResolverVisited.has(nodePath)) { | ||
} | ||
const calleePath = nodePath.get('callee'); | ||
const isNormalCall = state.normalizedOpts.transformFunctions.some(pattern => (0, _utils.matchesPattern)(state.types, calleePath, pattern)); | ||
if (isNormalCall || (0, _utils.isImportCall)(state.types, nodePath)) { | ||
@@ -23,0 +17,0 @@ state.moduleResolverVisited.add(nodePath); |
@@ -5,7 +5,4 @@ "use strict"; | ||
exports.default = transformImport; | ||
var _mapPath = _interopRequireDefault(require("../mapPath")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function transformImport(nodePath, state) { | ||
@@ -15,5 +12,4 @@ if (state.moduleResolverVisited.has(nodePath)) { | ||
} | ||
state.moduleResolverVisited.add(nodePath); | ||
(0, _mapPath.default)(nodePath.get('source'), state); | ||
} |
"use strict"; | ||
exports.__esModule = true; | ||
exports.escapeRegExp = escapeRegExp; | ||
exports.isImportCall = isImportCall; | ||
exports.isRelativePath = isRelativePath; | ||
exports.matchesPattern = matchesPattern; | ||
exports.nodeResolvePath = nodeResolvePath; | ||
exports.isRelativePath = isRelativePath; | ||
exports.replaceExtension = replaceExtension; | ||
exports.stripExtension = stripExtension; | ||
exports.toLocalPath = toLocalPath; | ||
exports.toPosixPath = toPosixPath; | ||
exports.toLocalPath = toLocalPath; | ||
exports.stripExtension = stripExtension; | ||
exports.replaceExtension = replaceExtension; | ||
exports.matchesPattern = matchesPattern; | ||
exports.isImportCall = isImportCall; | ||
exports.escapeRegExp = escapeRegExp; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _resolve = _interopRequireDefault(require("resolve")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function nodeResolvePath(modulePath, basedir, extensions) { | ||
@@ -30,14 +26,10 @@ try { | ||
} | ||
function isRelativePath(nodePath) { | ||
return nodePath.match(/^\.?\.\//); | ||
} | ||
function toPosixPath(modulePath) { | ||
return modulePath.replace(/\\/g, '/'); | ||
} | ||
function toLocalPath(modulePath) { | ||
let localPath = modulePath.replace(/\/index$/, ''); // remove trailing /index | ||
if (!isRelativePath(localPath)) { | ||
@@ -49,6 +41,4 @@ localPath = `./${localPath}`; // insert `./` to make it a relative path | ||
} | ||
function stripExtension(modulePath, stripExtensions) { | ||
let name = _path.default.basename(modulePath); | ||
stripExtensions.some(extension => { | ||
@@ -59,3 +49,2 @@ if (name.endsWith(extension)) { | ||
} | ||
return false; | ||
@@ -65,3 +54,2 @@ }); | ||
} | ||
function replaceExtension(modulePath, opts) { | ||
@@ -71,3 +59,2 @@ const filename = stripExtension(modulePath, opts.stripExtensions); | ||
} | ||
function matchesPattern(types, calleePath, pattern) { | ||
@@ -77,21 +64,16 @@ const { | ||
} = calleePath; | ||
if (types.isMemberExpression(node)) { | ||
return calleePath.matchesPattern(pattern); | ||
} | ||
if (!types.isIdentifier(node) || pattern.includes('.')) { | ||
return false; | ||
} | ||
const name = pattern.split('.')[0]; | ||
return node.name === name; | ||
} | ||
function isImportCall(types, calleePath) { | ||
return types.isImport(calleePath.node.callee); | ||
} | ||
function escapeRegExp(string) { | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
} |
{ | ||
"name": "babel-plugin-module-resolver", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"main": "lib/index.js", | ||
@@ -11,3 +11,3 @@ "description": "Module resolver plugin for Babel", | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
"node": ">= 16" | ||
}, | ||
@@ -37,17 +37,17 @@ "files": [ | ||
"dependencies": { | ||
"find-babel-config": "^1.2.0", | ||
"glob": "^7.1.6", | ||
"find-babel-config": "^2.0.0", | ||
"glob": "^8.0.3", | ||
"pkg-up": "^3.1.0", | ||
"reselect": "^4.0.0", | ||
"resolve": "^1.13.1" | ||
"reselect": "^4.1.7", | ||
"resolve": "^1.22.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.7.5", | ||
"@babel/core": "^7.7.5", | ||
"@babel/plugin-syntax-dynamic-import": "^7.7.4", | ||
"@babel/plugin-transform-modules-commonjs": "^7.7.5", | ||
"@babel/preset-env": "^7.7.6", | ||
"@babel/cli": "^7.20.7", | ||
"@babel/core": "^7.20.12", | ||
"@babel/plugin-syntax-dynamic-import": "^7.8.3", | ||
"@babel/plugin-transform-modules-commonjs": "^7.20.11", | ||
"@babel/preset-env": "^7.20.2", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^24.9.0", | ||
"common-tags": "^1.8.0", | ||
"babel-jest": "^29.3.1", | ||
"common-tags": "^1.8.2", | ||
"eslint": "^6.7.2", | ||
@@ -58,3 +58,3 @@ "eslint-config-airbnb-base": "^14.0.0", | ||
"husky": "^4.3.6", | ||
"jest": "^24.9.0", | ||
"jest": "^29.3.1", | ||
"lint-staged": "^10.5.3", | ||
@@ -81,4 +81,3 @@ "prettier-eslint-cli": "^5.0.0", | ||
"*.{js}": [ | ||
"prettier-eslint --write", | ||
"git add" | ||
"prettier-eslint --write" | ||
] | ||
@@ -85,0 +84,0 @@ }, |
@@ -28,7 +28,7 @@ # babel-plugin-module-resolver | ||
``` | ||
$ npm install --save-dev babel-plugin-module-resolver | ||
npm install --save-dev babel-plugin-module-resolver | ||
``` | ||
or | ||
``` | ||
$ yarn add --dev babel-plugin-module-resolver | ||
yarn add --dev babel-plugin-module-resolver | ||
``` | ||
@@ -35,0 +35,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
386
25107
12
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedfind-babel-config@2.1.2(transitive)
+ Addedglob@8.1.0(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedminimatch@5.1.6(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfind-babel-config@1.2.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedjson5@1.0.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
Updatedfind-babel-config@^2.0.0
Updatedglob@^8.0.3
Updatedreselect@^4.1.7
Updatedresolve@^1.22.1