0

I was taking a look to the swiper.esm.js from Swiper.js (source copied here for reference), and the first line is

export { default as Swiper, default } from './core/core.js';

while the following are of the form:

export { default as Virtual } from './modules/virtual/virtual.js';

and in MDN docs i can find that also something like

export { Something as default }

is possible. So I can currently see the following syntaxes:

export { Something as default }
export { default as Something }
export { default as Something, default }

What are the difference between the 3?

1
  • It should become pretty clear when you try to import from modules with those exports. Commented Feb 8, 2022 at 14:05

1 Answer 1

4
// Import name `Something` from `...`, 
//   re-export as this module's default export
export { Something as default } from '...';

// Import default export from `...`, 
//   re-export as `Something` from this module
export { default as Something } from '...';

// Import default export from `...`, 
//   re-export it as `Something` from this module 
//   and this module's default export
export { default as Something, default } from '...';
Sign up to request clarification or add additional context in comments.

6 Comments

Really nicely put! I might just add something saying they're all examples of export renaming.
thank you. The last 'and this module's default export' is for sure correct but quite strange to me considering the source code where it's written: jsfiddle.net/fw4zj8qk that is the swiper.esm.js source code. So what would the default be here, that is exported in the last line of your answer?
@LucaReghellin It means that as an user of Swiper, doing import Swiper from 'swiper'; and import {Swiper} from 'swiper'; would be equivalent. This would be convenient if you wanted to import multiple things from the package without one looking special: import {Swiper, Zoom, Grid} from 'swiper'; works, so does import Swiper, {Zoom, Grid} from 'swiper';.
@LucaReghellin - export { default as Swiper, default } from './core/core.js'; re-exports the default export of "./core/core.js" as Swiper and also as default.
@AKX - Although you could just do import Swiper, {Zoom, Grid} from 'swiper';. So not really all that much more convenient, though more robust if you had a typo like Swper. (One of many reasons to use named exports, not defaults, but of course when importing you're stuck with what the source package did.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.