5 ms·
Alternatively, with namespace imports in JS you can write [1]: import * as someLibrary from "some-library" someLibrary.someFunction() Which works pretty w
by throwitaway1123 1y ago
Alternatively, with namespace imports in JS you can write [1]:
import * as someLibrary from "some-library"
someLibrary.someFunction()
Which works pretty well with IDE autocomplete in my experience.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#namespace_import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Sammi 1y agoThis is a non starter for anything you want to publish online, as it breaks tree shaking which will cause size bloat and therefore slow loading.
- sebws 1y agoI don't think this is true. The example from the esbuild docs uses `import * as lib from './lib.js'` in an example for tree shaking. https://esbuild.github.io/api/#tree-shaking https://esbuild.github.io/api/#tree-shaking Although there are associated issues but they may be specific to esbuild. https://github.com/evanw/esbuild/issues/1420 https://github.com/evanw/esbuild/issues/1420
- throwitaway1123 1y agoYes, as you pointed out, not only can bundlers tree shake namespace imports, but they're literally used in the esbuild documentation to demonstrate the concept of tree shaking. The issue you linked to is referring to the case in which you import a namespace object and then re-export it. Bundlers like webpack and rollup (which vite uses in production) can tree shake this pattern as well, but esbuild struggles with it. If you're using esbuild instead of this: import * as someLibrary from "some-library" someLibrary.someFunction() export { someLibrary } You can still do this: import * as someLibrary from "some-library" someLibrary.someFunction() export * from "some-library" export { default as someLibraryDefault } from "some-library" Tree shaking works as expected for downstream packages using esbuild in the second case, which someone else in the linked issue pointed out: https://github.com/evanw/esbuild/issues/1420#issuecomment-961323828 https://github.com/evanw/esbuild/issues/1420#issuecomment-96...