JSPM

babel-plugin-module-rewrite

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3998
  • Score
    100M100P100Q127791F
  • License MIT

Babel plugin to dynamically rewrite the path in require() and ES6 import

Package Exports

  • babel-plugin-module-rewrite

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-module-rewrite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

babel-plugin-module-rewrite

Version Build Status

A babel plugin to rewrite module imports (and require) using a custom function

Description

You can supply a replace function to dynamically replace module paths when Babel traverses them.

Usage

Install the plugin

$ npm install --save-dev babel babel-plugin-module-rewrite

Specify the plugin in your .babelrc with the file that exports the replace function.

{
  "plugins": [
    ["module-rewrite", { "replaceFunc": "./utils/replace-module-paths.js" }]
  ]
}

Let's say you want ~/moduleFile to be replaced to utils/moduleFile if the calling file is in utils, and common/moduleFile otherwise. So in your replace-module-paths.js, just export:

export default function replaceImport(originalPath, callingFileName, options) {
    if(callingFileName.indexOf('/utils/') !== -1) {
        return originalPath.replace('~', 'utils');
    } else {
        return originalPath.replace('~', 'common');
    }
}

License

MIT