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.

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.
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.
npm install babel-plugin-bulk-import
yarn add babel-plugin-bulk-import
pnpm add babel-plugin-bulk-import

Configures .babelrc, creates a module with CJS export, and uses wildcard import to collect all modules from a directory.

// .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' } }