/babel-plugin-module-resolver-standalone

Module resolver plugin for Babel usable in both Node.js the web browser environments

Primary LanguageJavaScriptMIT LicenseMIT

babel-plugin-module-resolver-standalone

NPM version devDependency Status

A Babel plugin to add a new resolver for your modules when compiling your code using Babel. This plugin allows you to transform the path of each source module using a custom JavaScript function.

This plugin can be used instead of babel-plugin-module-resolver, if the target environment is a web browser using @babel/standalone, with which the original plugin does not work. This plugin supports only the method resolvePath for the time being.

Table of Contents

Installation

This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 6 or newer.

npm i -D babel-plugin-module-resolver-standalone
yarn add babel-plugin-module-resolver-standalone

Babel Configuration Examples

Prepend path to utility modules to be able to import them from utils/... without always providing the actual full path:

{
  plugins: [
    [
      'module-resolver',
      {
        resolvePath: function (sourcePath, currentFile, opts) {
          if (sourcePath.startsWith('utils/')) {
            return '../../'+ sourcePath
          }
        }
      }
    ]
  ]
}

Ensure, that all JavaScript module paths are prefixed by es6!, so that requirejs-babel will be applied by RequireJS to nested modules too:

{
  plugins: [
    'transform-modules-amd',
    [
      'module-resolver',
      {
        resolvePath: function (sourcePath, currentFile, opts) {
          // Avoid prefixing modules handled by other plugins.
          if (sourcePath.indexOf('!') < 0) {
            return 'es6!' + sourcePath;
          }
        }
      }
    ]
  ]
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

License

Copyright (c) 2019 Ferdinand Prantl

Licensed under the MIT license.