Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c589176

Browse files
committed
closes #3183
1 parent c27a7b4 commit c589176

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

β€Ž1-js/13-modules/02-import-export/article.mdβ€Ž

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Also, we can put `export` separately.
4646

4747
Here we first declare, and then export:
4848

49-
```js
49+
```js
5050
// πŸ“ say.js
5151
function sayHi(user) {
5252
alert(`Hello, ${user}!`);
@@ -93,25 +93,14 @@ At first sight, "import everything" seems such a cool thing, short to write, why
9393

9494
Well, there are few reasons.
9595

96-
1. Modern build tools ([webpack](https://webpack.js.org/) and others) bundle modules together and optimize them to speedup loading and remove unused stuff.
97-
98-
Let's say, we added a 3rd-party library `say.js` to our project with many functions:
99-
```js
100-
// πŸ“ say.js
101-
export function sayHi() { ... }
102-
export function sayBye() { ... }
103-
export function becomeSilent() { ... }
104-
```
96+
1. Explicitly listing what to import gives shorter names: `sayHi()` instead of `say.sayHi()`.
97+
2. Explicit list of imports gives better overview of the code structure: what is used and where. It makes code support and refactoring easier.
10598

106-
Now if we only use one of `say.js` functions in our project:
107-
```js
108-
// πŸ“ main.js
109-
import {sayHi} from './say.js';
110-
```
111-
...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".
99+
```smart header="Don't be afraid to import too much"
100+
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.
112101
113-
2. Explicitly listing what to import gives shorter names: `sayHi()` instead of `say.sayHi()`.
114-
3. Explicit list of imports gives better overview of the code structure: what is used and where. It makes code support and refactoring easier.
102+
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.
103+
```
115104

116105
## Import "as"
117106

@@ -224,7 +213,7 @@ Without `default`, such an export would give an error:
224213
export class { // Error! (non-default export needs a name)
225214
constructor() {}
226215
}
227-
```
216+
```
228217

229218
### The "default" name
230219

@@ -326,7 +315,7 @@ Imagine, we're writing a "package": a folder with a lot of modules, with some of
326315
The file structure could be like this:
327316
```
328317
auth/
329-
index.js
318+
index.js
330319
user.js
331320
helpers.js
332321
tests/
@@ -372,15 +361,15 @@ The syntax `export ... from ...` is just a shorter notation for such import-expo
372361

373362
```js
374363
// πŸ“ auth/index.js
375-
// re-export login/logout
364+
// re-export login/logout
376365
export {login, logout} from './helpers.js';
377366

378367
// re-export the default export as User
379368
export {default as User} from './user.js';
380369
...
381370
```
382371

383-
The notable difference of `export ... from` compared to `import/export` is that re-exported modules aren't available in the current file. So inside the above example of `auth/index.js` we can't use re-exported `login/logout` functions.
372+
The notable difference of `export ... from` compared to `import/export` is that re-exported modules aren't available in the current file. So inside the above example of `auth/index.js` we can't use re-exported `login/logout` functions.
384373

385374
### Re-exporting the default export
386375

@@ -399,7 +388,7 @@ We can come across two problems with it:
399388

400389
1. `export User from './user.js'` won't work. That would lead to a syntax error.
401390

402-
To re-export the default export, we have to write `export {default as User}`, as in the example above.
391+
To re-export the default export, we have to write `export {default as User}`, as in the example above.
403392

404393
2. `export * from './user.js'` re-exports only named exports, but ignores the default one.
405394

@@ -430,7 +419,7 @@ Import:
430419

431420
- Importing named exports:
432421
- `import {x [as y], ...} from "module"`
433-
- Importing the default export:
422+
- Importing the default export:
434423
- `import x from "module"`
435424
- `import {default as x} from "module"`
436425
- Import all:

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /