You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/13-modules/02-import-export/article.md
+12-4Lines changed: 12 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -337,13 +337,19 @@ auth/
337
337
...
338
338
```
339
339
340
-
We'd like to expose the package functionality via a single entry point, the "main file" `auth/index.js`, to be used like this:
340
+
We'd like to expose the package functionality via a single entry point.
341
+
342
+
In other words, a person who would like to use our package, should import only from the "main file" `auth/index.js`.
343
+
344
+
Like this:
341
345
342
346
```js
343
347
import {login, logout} from 'auth/index.js'
344
348
```
345
349
346
-
The idea is that outsiders, developers who use our package, should not meddle with its internal structure, search for files inside our package folder. We export only what's necessary in `auth/index.js` and keep the rest hidden from prying eyes.
350
+
The "main file", `auth/index.js` exports all the functionality that we'd like to provide in our package.
351
+
352
+
The idea is that outsiders, other programmers who use our package, should not meddle with its internal structure, search for files inside our package folder. We export only what's necessary in `auth/index.js` and keep the rest hidden from prying eyes.
347
353
348
354
As the actual exported functionality is scattered among the package, we can import it into `auth/index.js` and export from it:
349
355
@@ -366,14 +372,16 @@ The syntax `export ... from ...` is just a shorter notation for such import-expo
366
372
367
373
```js
368
374
// 📁 auth/index.js
369
-
//import login/logout and immediately export them
375
+
//re-export login/logout
370
376
export {login, logout} from'./helpers.js';
371
377
372
-
//import default as User and export it
378
+
//re-export the default export as User
373
379
export {defaultasUser} from'./user.js';
374
380
...
375
381
```
376
382
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.
384
+
377
385
### Re-exporting the default export
378
386
379
387
The default export needs separate handling when re-exporting.
0 commit comments