-
Notifications
You must be signed in to change notification settings - Fork 43
Import named vs default from CommonJS packages #260
Comments
I can't quite tell, so just to be 100% sure:
Do you count this as one instance of using an import specifier or two instances of using an import specifier? If it is the latter, I'd like to request a recount. |
Two counts. I was counting specifiers, not statements. I made a separate count of
So 17,751 ImportDefaultSpecifiers across 25,854 |
What about jsnext:main, or packages that didn’t choose to provide a nonstandard package.json field but still used babel? I’d bet if you looked st the github repos for packages that had babel in dev deps youd get a much larger sample size. |
This was a quick-and-dirty analysis. Obviously if I wanted to be more thorough I could have been. You’re welcome to fork the repo and improve it if you’d like. This sample size of 941 packages included 96,923 JavaScript ( I was only curious to see if the usage of named exports of CommonJS packages was significant. My hunch was that it would be infrequent, as many CommonJS packages expect to only be imported as the main, like |
@GeoffreyBooth Thank you for doing all of this data work! So nice! 🤝 |
@GeoffreyBooth makes sense, thanks. I thoroughly expected that; any form of interop we fail to provide is going to cause massive pain for users. It would also be interesting to rerun your data to see if anyone is using |
Based on what we've upstreamed I think we can close this discussion for now. Please reopen if I am mistaken |
In the discussion regarding dynamic modules and CommonJS named exports, and whether or not it’s worth shipping support for
import
statements of CommonJS packages’ default exports if we can’t also supportimport
of named exports, I thought it might be useful to research just how commonimport
statements of named exports versus default exports are. I took my research repo consisting of the 941 packages on the public NPM registry that contain a"module"
field and I analyzed those packages’import
statements.Terminology
Import specifiers come in three types, per Babel’s AST:
ImportSpecifier
is theshuffle
inimport { shuffle } from 'lodash'
.ImportDefaultSpecifier
is the_
inimport _ from 'lodash'
.ImportNamespaceSpecifier
is the* as _
inimport * as _ from 'lodash'
.The import source is the
'lodash'
in the above examples. For the sake of simplicity, I’m only analyzing imports of package entry points (so not'lodash/shuffle.js'
or'./shuffle.js'
). The import source is considered ESM if itspackage.json
has a"module"
field, or CommonJS otherwise.Results
Where the source (e.g.
'lodash'
) is an ESM package:Where the source (e.g.
'lodash'
) is a CommonJS package:With regards to the CommonJS named vs default export debate:
import
statements) are importing a CommonJS package’s default export:import _ from 'lodash'
.import { shuffle } from 'lodash'
.So if we were to allow ImportDefaultSpecifiers and ImportNamespaceSpecifiers but not ImportSpecifiers, which is the option currently being discussed, in this corpus at least we would cause 44% of all ImportSpecifiers to throw errors. An
import
statement importing named exports often imports several at a time (e.g.import { shuffle, throttle } from 'lodash'
) so the percentage ofimport
statements using ImportSpecifiers is lower than 44% of all specifiers being ImportSpecifiers, but the percentage of suchimport
statements is surely still significant. A very large percentage of users will likely expectimport
statements likeimport { shuffle } from 'lodash'
to work, as such statements are commonplace in user ESM code.The text was updated successfully, but these errors were encountered: