Import All Named Exports into One Variable
We can import all of a JavaScript module’s contents into a single variable with * as
.
Now stringUtils
can reference any exports in string-utils.mjs
using property access syntax.
Where is this a good practice?
Importing all named exports into a single variable is great feature for application code.
Consider these UI modules that have a similar export signatur.
email.js
contact.js
Renaming all of these named exports is super irritating!
By reducing moduling imports to a single reference, we eliminate the need to rename the colliding exports.
And these namespaces guarantee that we never experience a collision between named exports.
My take
This is my preferred way to import named exports. It has eliminated pointless hours spent justifying local references between modules I don’t control.
Go pro
This is part of a course I’m build on modules at lunch.dev.
When live, members get access to this and other courses on React.