3

Original question:JavaScript - Does the use of namespace imports have an effect on a module's treeshake-ability?

Assume that we are using ES6 module system. According to the docs, when we use an * as Alias import, we are taking all of the exports from the file and containing them inside of an object of the alias's name.

I am wondering if this has any impact on the tree-shakeability of the imported module?

If so, are named and default imports better in that regard?

user1261710
  • 2,539
  • 5
  • 41
  • 72
  • 1
    Does https://stackoverflow.com/questions/45735534/is-using-an-es6-import-to-load-specific-names-faster-than-importing-a-namespace/ answer your question (despite not being an exact duplicate)? – Bergi Mar 10 '23 at 19:22
  • 1
    Depends what you are doing with them. If you try to enumerate the properties of the namespace object or something like, all exports will be have to be loaded of course. But that doesn't mean default-exporting an object is any better… – Bergi Mar 10 '23 at 19:24
  • @Bergi, I'm not doing anything non-standard with them, just importing and using as normal. – user1261710 Mar 10 '23 at 23:37
  • @Bergi, I tried to use this tool: http://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&targets=&browsers=&builtIns=false&debug=false&experimental=false&loose=false&spec=false&code_lz=JYWwDg9gTgLgBAbzgYQuCA7AphmAaOABSgjABUBPMLAZzgF84AzEkOAciiwEMBjGdgG4AUMN6YaEADZYAdFIgBzABSp02XAEoR4jJJnyly4qUrUa24UA&playground=true and I found that these had identical outputs: import * as React from 'react'; import React, {useConst} from 'react'; but other styles did not. – user1261710 Mar 15 '23 at 12:47
  • That would be a special case where react default-exports their module namespace object. Not all modules do this (and tbh, it's a bit weird) – Bergi Mar 15 '23 at 15:29

0 Answers0