Module resolver plugin for Babel that enables custom root directories and aliases for import/require statements
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Babel Plugin Module Resolver configuration options for setting up custom module resolution behavior. The plugin transforms import/require statements during Babel compilation.
Core configuration options for the Babel plugin.
interface PluginOptions {
/** Root directories for module resolution */
root?: string | string[];
/** Path aliases mapping */
alias?: AliasConfig | AliasConfig[];
/** File extensions to resolve */
extensions?: string[];
/** Extensions to strip from resolved paths */
stripExtensions?: string[];
/** Working directory for resolution */
cwd?: string | 'babelrc' | 'packagejson';
/** Additional functions to transform */
transformFunctions?: string[];
/** Custom path resolution function */
resolvePath?: (sourcePath: string, currentFile: string, opts: PluginOptions) => string | undefined;
/** Logging level */
loglevel?: 'silent' | 'error' | 'warn' | 'info' | 'verbose' | 'silly';
}Specify root directories for module resolution. node_modules is implicit.
/** Root directories - string or array of strings, supports glob patterns */
root?: string | string[];Usage Examples:
{
"plugins": [
["module-resolver", {
"root": ["./src"]
}]
]
}Multiple roots with glob patterns:
{
"plugins": [
["module-resolver", {
"root": ["./src", "./lib", "./src/**/components"]
}]
]
}Map aliases to actual paths. Supports regular expressions, arrays, and functions.
interface AliasConfig {
[key: string]: string | string[] | ((matches: RegExpExecArray) => string | string[]);
}
/** Alias configuration - single object or array of objects */
alias?: AliasConfig | AliasConfig[];Basic aliases:
{
"plugins": [
["module-resolver", {
"alias": {
"components": "./src/components",
"utils": "./src/utils",
"underscore": "lodash"
}
}]
]
}Regular expression aliases:
{
"plugins": [
["module-resolver", {
"alias": {
"^@namespace/foo-(.+)": "packages/\\\\1"
}
}]
]
}Function-based aliases:
module.exports = {
plugins: [
["module-resolver", {
alias: {
"foo": ([, name]) => `bar${name}`,
"^@namespace/foo-(.+)": ([, name]) => `packages/${name}`
}
}]
]
}Array of paths:
{
"plugins": [
["module-resolver", {
"alias": {
"my-module": ["./src/my-module", "./lib/my-module"]
}
}]
]
}Configure file extensions for resolution.
/** File extensions to resolve */
extensions?: string[];
/** Extensions to strip from resolved paths */
stripExtensions?: string[];Default extensions: ['.js', '.jsx', '.es', '.es6', '.mjs']
Usage Example:
{
"plugins": [
["module-resolver", {
"extensions": [".js", ".jsx", ".ts", ".tsx"],
"stripExtensions": [".js", ".jsx"]
}]
]
}Configure the working directory for path resolution.
/** Working directory - path string or special values */
cwd?: string | 'babelrc' | 'packagejson';Special values:
'babelrc': Use directory containing nearest .babelrc'packagejson': Use directory containing nearest package.jsonUsage Examples:
{
"plugins": [
["module-resolver", {
"cwd": "babelrc"
}]
]
}Configure additional functions to transform beyond the defaults.
/** Additional function names to transform */
transformFunctions?: string[];Default transform functions:
requirerequire.resolveSystem.importjest.genMockFromModulejest.mockjest.unmockjest.doMockjest.dontMockjest.setMockjest.requireActualjest.requireMockrequire.requireActualrequire.requireMockUsage Example:
{
"plugins": [
["module-resolver", {
"transformFunctions": [
"require",
"require.resolve",
"proxyquire",
"myCustomLoader"
]
}]
]
}Provide a custom path resolution function that replaces the default resolution logic.
/**
* Custom path resolution function
* @param sourcePath - Module specifier to resolve
* @param currentFile - File containing the import
* @param opts - Plugin options (the original options object passed to the plugin)
* @returns Resolved path or undefined to use default behavior
*/
resolvePath?: (
sourcePath: string,
currentFile: string,
opts: PluginOptions
) => string | undefined;Usage Example:
module.exports = {
plugins: [
["module-resolver", {
extensions: [".js"],
resolvePath(sourcePath, currentFile, opts) {
// Custom resolution logic
if (sourcePath.startsWith('special:')) {
return sourcePath.replace('special:', './special/');
}
// Return undefined to use default behavior
return undefined;
}
}]
]
}Configure logging level for path resolution warnings and errors.
/** Logging level */
loglevel?: 'silent' | 'error' | 'warn' | 'info' | 'verbose' | 'silly';Default: 'warn'
Usage Example:
module.exports = {
plugins: [
["module-resolver", {
alias: {
"dependency-string": "module-that-does-not-exist"
},
loglevel: 'silent' // Suppress warnings
}]
]
}// In webpack.config.dev.js
{
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
plugins: [
["module-resolver", {
"root": ["./src/App"],
"alias": {
"components": "./src/components"
}
}]
],
cacheDirectory: true
}
}{
"plugins": [
["module-resolver", {
"root": ["./src"],
"extensions": [".ios.js", ".android.js", ".js", ".json"]
}]
]
}Update .flowconfig:
[options]
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=./srcOr use name mapper:
[options]
module.name_mapper='^components\/\(.*\)$' -> '<PROJECT_ROOT>/src/components/\1'