/babel-plugin-import-glob-simplified

Babel plugin to enable importing modules using a glob pattern

Primary LanguageJavaScriptISC LicenseISC

babel-plugin-import-glob-simplified

Babel 7 plugin to enable importing modules using a glob pattern. Tested with Node.js 4 and above.

What was changed?

  • Babel 7 support (@babel/core)
  • Fixed bug on windows, now tested on Win & Mac
  • Import specific names NOT supported (import {name1, name2} from 'glob:...')
  • Both default import and namespace import supported:
    • Namespace import (import * as modules from 'glob:...') compiles to:

      import * as _modules_$path$to$file1 from './path/to/file1'
      import * as _modules_$path$to$file2 from './path/to/file1'
      const modules = {
        './path/to/file1': _modules_$path$to$file1,
        './path/to/file2': _modules_$path$to$file2
        };
      Object.freeze(modules);
    • Default imports (import modules from 'glob:...') compiles to:

      import _modules_$path$to$file1 from './path/to/file1'
      import _modules_$path$to$file2 from './path/to/file1'
      const modules = {
        './path/to/file1': _modules_$path$to$file1,
        './path/to/file2': _modules_$path$to$file2
        };
      Object.freeze(modules);
    • Plugin Options:

      //.babelrc:
      {
        "plugins":[
          ["@babel/plugin-import-glob-simplified",{
            'trimFileExtensions':["js","jsx","ts","tsx"]
          }]
        ]
      }
      • trimFileExtensions - array of file extensions to trim from discovered files when put it to pathToTrim in import ... from '{pathToTrim}'

Installation

Add to your package.json/devDependencies:

"babel-plugin-import-glob-simplified": "https://github.com/pmunin/babel-plugin-import-glob-simplified.git"

Then register it in your .babelrc file, like:

{
  "plugins": [
    ["@babel/plugin-import-glob-simplified", {
      "trimFileExtensions":["js","jsx", "ts", "tsx"] //optional
    }]
  ]
}

TODO:

Tests need to be fixed accordingly to new design