babel-plugin-bulk-import
raw JSON → 2.0.0 verified Fri May 01 auth: no javascript maintenance
Babel plugin (v2.0.0) that transforms glob-based import paths (e.g., `import * as Features from './features/*.js'`) into explicit per-module imports at build time. Designed to solve the problem of dynamically discovering modules in a directory while ensuring webpack bundles them correctly for client-side applications. Uses node-glob for path resolution. Not actively maintained (last release 2017). Key differentiator: enables automatic module registration without runtime dynamic imports, avoiding webpack dead-code elimination. Alternatives include `babel-plugin-wildcard` or manual `require.context` with webpack.
Common errors
error Error: Cannot find module 'glob' ↓
cause glob is a peer dependency not included automatically.
fix
Install glob: npm install glob --save-dev or --save
error SyntaxError: Unexpected token * ↓
cause Using wildcard import without the Babel plugin active.
fix
Ensure babel-plugin-bulk-import is added to the plugins list in .babelrc.
error Module parse failed: Unexpected character '*' ↓
cause Webpack encountered wildcard import before Babel transpilation.
fix
Ensure the webpack config processes the file with Babel (e.g., use babel-loader) and the plugin is configured.
Warnings
gotcha Named imports from wildcard paths may resolve to undefined if the path matches nested directories without matching files. ↓
fix Ensure the glob pattern directly matches files, not directories. Use `**/*.js` for recursion but verify file structure.
deprecated Plugin has not been updated since 2017 and may not work with Babel 7+ or modern module systems. ↓
fix Consider alternative plugins like babel-plugin-wildcard or use webpack's require.context.
gotcha The plugin adds 'node_modules/' prefix to imports from node_modules, resulting in nested objects. ↓
fix Avoid wildcard imports from node_modules; they cause unexpected nesting and potential bundle bloat.
Install
npm install babel-plugin-bulk-import yarn add babel-plugin-bulk-import pnpm add babel-plugin-bulk-import Imports
- default (plugin) wrong
require('babel-plugin-bulk-import') in .babelrccorrectin .babelrc: {"plugins": ["babel-plugin-bulk-import"]} - import * as all from './path/*.js' wrong
import all from './features/*.js'correctimport * as all from './features/*.js' - import { named } from './path/*.js' wrong
import { featureA as moduleA } from './features/*.js'correctimport { featureA } from './features/*.js'
Quickstart
// .babelrc
{
"presets": ["@babel/preset-env"],
"plugins": ["babel-plugin-bulk-import"]
}
// features/featureA.js
module.exports = { feature: 'A' };
// src/index.js
import * as Features from '../features/*.js';
console.log(Features); // { featureA: { feature: 'A' } }