[Proposal] Limited namespace imports

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Proposal] Limited namespace imports

N. Oxer
This is best introduced in a possible syntax:

```javascript
import { A, B, C } as d from "module";
```

Essentially, it would allow for some form of importing only some exports into a namespace. This would not affect the browser or environments like node, but it would be a boon for module bundlers, such as webpack, rollup, parcel, etc. It would allow you to specifically define which imports you want, and the rest to be tree-shaken, and without any quirks of trying to guess which imports from a

```javascript
import * as d from "module";
```

are used.

It also would prevent the scope from getting cluttered. An example of where this would be undesirable/annoying is the `@material-ui/icons` package on npm. It exports many different icons, and an example of using them might be as such:

```jsx
import {
  Wifi,
  Accessibility,
  Add,
  Save,
  Search
} from "@material-ui/icons";
import SearchBar from "./search";
// use them in some way
export default () => (
  <>
    <SearchBar />
    <Wifi />
    <Accessibility />
    <Add />
    <Save />
    <Search />
  </>
)
```

Alternatively, you could just import them `* as icons`, but then that imports all of them and inflates the bundle size.

If this proposal were to be implemented, you could do something like

```jsx
import {
  Wifi,
  Accessibility,
  Add,
  Save,
  Search
} as icons from "@material-ui/icons";
import Search from "./search";
// use them in some way
export default () => (
  <>
    <Search />
    <icons.Wifi />
    <icons.Accessibility />
    <icons.Add />
    <icons.Save />
    <icons.Search />
  </>
)
```

and avoid the downsides.

This would also work for a library like lodash. It currently has ~320 utility functions, looking through their documentation, so importing `* as _` would conflate the bundle immensely. With this proposal, issues like that would be much less prevalent.

--
Noah Oxer
Student, Programmer, and Video Gamer

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss