babel-plugin-transform-inline-imports-commonjs
raw JSON → 1.2.0 verified Sat Apr 25 auth: no javascript maintenance
A Babel plugin (v1.2.0, last updated 2019) that transforms ES module imports into lazily loaded, memoized CommonJS requires, deferring require() calls until the imported identifier is actually referenced. Unlike the standard babel-plugin-transform-es2015-modules-commonjs, this avoids upfront I/O and execution costs for imported modules. Maintained by the author of babel-plugin-module-resolver, but now in maintenance mode as most projects have moved to @babel/preset-env or other bundlers. Supports options like excludeModules and excludeNodeBuiltins to skip lazy loading for specific modules or Node builtins. No TypeScript types, minimal breaking changes across versions.
Common errors
error Error: Plugin/Preset files are not allowed to export objects, only functions. ↓
cause Using the plugin incorrectly with a bundler that doesn't support Babel 6-style plugins, or using a Babel 7 package with Babel 6.
fix
Ensure you are using Babel 6 with this plugin. For Babel 7, use a different module transformation plugin.
error Cannot find module 'babel-plugin-transform-inline-imports-commonjs' ↓
cause Package not installed or wrong name in .babelrc.
fix
Run: npm install --save-dev babel-plugin-transform-inline-imports-commonjs and verify the name in .babelrc.
Warnings
deprecated This plugin is in maintenance mode; it has not been updated since Babel 6 era. For Babel 7+, consider using @babel/plugin-transform-modules-commonjs with lazy evaluation via other means. ↓
fix Migrate to @babel/preset-env or @babel/plugin-transform-modules-commonjs with babel-plugin-lazy-require or similar.
breaking In version 1.0.0, the option 'exclude' was renamed to 'excludeModules'. ↓
fix Use 'excludeModules' instead of 'exclude' in plugin options.
gotcha If you use this plugin alongside @babel/plugin-transform-runtime, the lazy loading may conflict with runtime helpers. ↓
fix Order plugins correctly: transform-inline-imports-commonjs should come before transform-runtime in your plugin list.
gotcha Lazy loading can cause issues with circular dependencies because the require is deferred until the variable is used. ↓
fix Use the 'excludeModules' option to skip lazy loading for modules involved in circular dependencies.
Install
npm install babel-plugin-transform-inline-imports-commonjs yarn add babel-plugin-transform-inline-imports-commonjs pnpm add babel-plugin-transform-inline-imports-commonjs Imports
- default wrong
import plugin from 'babel-plugin-transform-inline-imports-commonjs'correctmodule.exports = require('babel-plugin-transform-inline-imports-commonjs') - babel-plugin-transform-inline-imports-commonjs wrong
// Incorrect path { "plugins": ["@babel/plugin-transform-inline-imports-commonjs"] }correct// .babelrc { "plugins": ["transform-inline-imports-commonjs"] } - excludeModules wrong
// Typo in option name { "plugins": [["transform-inline-imports-commonjs", { "exclude": ["atom"] }]] }correct// .babelrc { "plugins": [["transform-inline-imports-commonjs", { "excludeModules": ["atom"] }]] }
Quickstart
// .babelrc
{
"plugins": [
["transform-inline-imports-commonjs", {
"excludeNodeBuiltins": true,
"excludeModules": ["react"]
}]
]
}
// index.js (will be transformed)
import express from 'express';
const app = express();
export default app;