This commit is contained in:
Ilya Kantor 2022-10-01 19:45:06 +02:00
parent c27a7b4d40
commit c5891761a3

View file

@ -93,26 +93,15 @@ At first sight, "import everything" seems such a cool thing, short to write, why
Well, there are few reasons. Well, there are few reasons.
1. Modern build tools ([webpack](https://webpack.js.org/) and others) bundle modules together and optimize them to speedup loading and remove unused stuff. 1. Explicitly listing what to import gives shorter names: `sayHi()` instead of `say.sayHi()`.
2. Explicit list of imports gives better overview of the code structure: what is used and where. It makes code support and refactoring easier.
Let's say, we added a 3rd-party library `say.js` to our project with many functions: ```smart header="Don't be afraid to import too much"
```js Modern build tools, such as [webpack](https://webpack.js.org/) and others, bundle modules together and optimize them to speedup loading. They also removed unused imports.
// 📁 say.js
export function sayHi() { ... } For instance, if you `import * as library` from a huge code library, and then use only few methods, then unused ones [will not be included](https://github.com/webpack/webpack/tree/main/examples/harmony-unused#examplejs) into the optimzed bundle.
export function sayBye() { ... }
export function becomeSilent() { ... }
``` ```
Now if we only use one of `say.js` functions in our project:
```js
// 📁 main.js
import {sayHi} from './say.js';
```
...Then the optimizer will see that and remove the other functions from the bundled code, thus making the build smaller. That is called "tree-shaking".
2. Explicitly listing what to import gives shorter names: `sayHi()` instead of `say.sayHi()`.
3. Explicit list of imports gives better overview of the code structure: what is used and where. It makes code support and refactoring easier.
## Import "as" ## Import "as"
We can also use `as` to import under different names. We can also use `as` to import under different names.