babel-resolve-barrel-conflicts
v1.0.0
Published
A Babel plugin that automatically resolves conflicting star exports from barrel files.
Downloads
10
Maintainers
Readme
Babel Plugin: Resolve Barrel Conflicts A Babel plugin that automatically resolves conflicting star exports from barrel files by making exports explicit and renaming duplicates.
The Problem When using "barrel files" (index.js files that re-export from multiple other files), you can easily run into build errors if two or more files export a variable with the same name.
// player.js export const health = 100;
// enemy.js export const health = 50;
// store/index.js (Your Barrel File) export * from './player'; export * from './enemy';
When another file tries to import { health } from './store', the bundler (like Next.js or Webpack) doesn't know which health variable to use, and the build fails with a "conflicting star exports" error.
The Solution This Babel plugin automatically transforms your barrel files at build time to resolve these conflicts. It first expands all export * statements into explicit named exports, then intelligently renames any duplicates by prefixing them with their source filename.
Your code (before):
// store/index.js export * from './player'; export * from './enemy';
What Babel outputs (after applying this plugin):
export { health, level } from "./player"; export { health as enemyHealth, type } from "./enemy";
Now, you can import health (from player) and enemyHealth without any conflicts!
Installation npm install --save-dev @your-username/babel-plugin-resolve-barrel-conflicts
Usage In your Babel configuration file (babel.config.js or .babelrc), add the plugin to your plugins array.
// babel.config.js module.exports = { presets: [ // ... your other presets ], plugins: [ '@your-username/babel-plugin-resolve-barrel-conflicts', // ... your other plugins ], };
