Registry / web-framework / babel-plugin-module-rewrite

babel-plugin-module-rewrite

JSON →
library0.2.0jsnpmunverified

Babel plugin that rewrites module import paths (both `import` and `require`) at compile time using a custom replacement function. Version 0.2.0 (latest) appears to be a stable, early-stage package with no active releases in recent years. It allows dynamic path transformations based on the calling file, unlike static alias plugins like babel-plugin-module-resolver. No major updates or security fixes have been published; the package should be considered minimal-risk but potentially unmaintained. Key differentiator is the per-file dynamic replacement capability.

npm install babel-plugin-module-rewrite
INSTALL
IMPORT
SIG · BABEL-PLUGIN-MODUL
B
babel-plugin-module-rewrite
web-frameworkjavascriptv0.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (replace function)
export default function replaceImport(originalPath, callingFileName, options) { ... }
module.exports = function(originalPath, callingFileName, options) { ... }
The replace function must be a default export from the file specified in the plugin options. Using CJS `module.exports` works, but plugin expects a default export in some setups.
babel-plugin-module-rewrite (plugin usage)
import babel from '@babel/core'; // or add to .babelrc: ["module-rewrite", { replaceFunc: "./path" }]
const babel = require('babel-core')
Plugin is configured via .babelrc or programmatic API; no direct import needed in user code.
Babel visitor (if extending)
no default export from the plugin; it's used as a Babel plugin
import plugin from 'babel-plugin-module-rewrite'
This package is a Babel plugin, not a library to import. You only configure it in .babelrc.

Configures the plugin to rewrite '~' prefix to 'utils' or 'common' based on calling file path.

npm install --save-dev babel-plugin-module-rewrite // .babelrc { "plugins": [ ["module-rewrite", { "replaceFunc": "./replace-module-paths.js" }] ] } // replace-module-paths.js export default function replaceImport(originalPath, callingFileName, options) { if (callingFileName.indexOf('/utils/') !== -1) { return originalPath.replace('~', 'utils'); } else { return originalPath.replace('~', 'common'); } } // Source file: src/utils/test.js import something from '~/moduleFile'; // becomes 'utils/moduleFile'
Debug
Known issues
breakingPlugin requires Babel 6.x; not compatible with Babel 7+ without modification.
fix
Use babel-plugin-module-resolver or upgrade with @babel/core adapter.
affects: >=0.0.0
gotchaThe replace function file must be a module that exports a default function; other export patterns may silently fail.
fix
Ensure the file uses `export default function` or `module.exports = function`.
affects: >=0.0.0
gotchaPlugin does not transform dynamic imports (import()) due to Babel 6 limitations.
fix
Use babel-plugin-dynamic-import-node or a newer plugin for dynamic imports.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Plugin babel-plugin-module-rewrite threw: TypeError: Cannot read property 'replace' of undefined
The replace function does not return a string, or the originalPath is undefined.
fix
Check that your replace function always returns a string, even if no replacement needed (return originalPath).
ReferenceError: [BABEL] unknown: Cannot find module './replace-module-paths.js'
The replaceFunc path is relative to the working directory, not the file's location.
fix
Use an absolute path or a path relative to process.cwd().
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
babelrequiredRequired as a peer to use the plugin
babel-coreoptionalMay be needed depending on Babel version
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
babel-plugin-module-rewrite — npm install babel-plugin-module-rewrite · libregistry