babel-plugin-unify-imports
v1.0.1
Published
A Babel plugin to automatically unify default and named imports at build time.
Maintainers
Readme
Babel Plugin: Unify Imports A Babel plugin that automatically unifies default and named imports at build-time, giving you a cleaner, more convenient way to access your modules.
The Problem When you import a module that has both a default export and several named exports, you often end up with two separate variables to track.
import MyDefaultComponent, { helper1, helper2 } from 'my-awesome-lib';
// To use them, you have to remember which is which: MyDefaultComponent(); helper1();
While you can access the named exports as properties of the default export in some module systems, this isn't guaranteed, and it requires you to write MyDefaultComponent.helper1(), which can be verbose.
The Solution This Babel plugin transforms your code at build time to automatically attach the named imports as properties onto the default import.
Your code (before):
import MyDefault, { named1, named2 } from 'my-lib';
What Babel outputs (after):
import MyDefault from 'my-lib'; import { named1, named2 } from 'my-lib';
MyDefault.named1 = named1; MyDefault.named2 = named2;
This means you can now access everything through the default import in a clean and predictable way:
MyDefault(); // Works MyDefault.named1(); // Also works! MyDefault.named2(); // And this too!
Installation npm install --save-dev @your-username/babel-plugin-unify-imports
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-unify-imports', // ... your other plugins ], };
Babel will now automatically apply this transformation to all of your import statements during your build process.
