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/03-modules-dynamic-imports/article.md
+14-16
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Dynamic imports
2
2
3
-
Export and import statements that we covered in previous chapters are called "static".
4
-
5
-
That's because they are indeed static. The syntax is very strict.
3
+
Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
6
4
7
5
First, we can't dynamically generate any parameters of `import`.
8
6
@@ -24,27 +22,27 @@ if(...) {
24
22
}
25
23
```
26
24
27
-
That's because `import`/`export` aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled together, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
25
+
That's because `import`/`export` aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled into one file by special tools, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
28
26
29
27
But how can we import a module dynamically, on-demand?
30
28
31
-
## The import() function
29
+
## The import() expression
32
30
33
-
The `import(module)`function can be called from anywhere. It returns a promise that resolves into a module object.
31
+
The `import(module)`expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can be called from any place in the code.
34
32
35
-
The usage pattern looks like this:
33
+
We can use it dynamically in any place of the code, for instance:
36
34
37
-
```js run
38
-
let modulePath =prompt("Module path?");
35
+
```js
36
+
let modulePath =prompt("Which module to load?");
39
37
40
38
import(modulePath)
41
39
.then(obj=><module object>)
42
-
.catch(err=><loading error, no such module?>)
40
+
.catch(err=><loading error, e.g. ifno such module>)
43
41
```
44
42
45
43
Or, we could use `let module = await import(modulePath)` if inside an async function.
46
44
47
-
For instance, if we have the following `say.js`:
45
+
For instance, if we have the following module `say.js`:
48
46
49
47
```js
50
48
// 📁 say.js
@@ -75,12 +73,12 @@ export default function() {
75
73
}
76
74
```
77
75
78
-
...Then, in order to access it, we can use `default` property of the module object, as explained in the [previous chapter](info:import-export).
79
-
80
-
So, the dynamic import will be like this:
76
+
...Then, in order to access it, we can use `default` property of the module object:
81
77
82
78
```js
83
-
let {default: say} =awaitimport('./say.js'); // save .default property in say variable
79
+
let obj =awaitimport('./say.js');
80
+
let say =obj.default;
81
+
// or, in one line: let {default: say} = await import('./say.js');
84
82
85
83
say();
86
84
```
@@ -96,5 +94,5 @@ Dynamic imports work in regular scripts, they don't require `script type="module
96
94
```smart
97
95
Although `import()` looks like a function call, it's a special syntax that just happens to use parentheses (similar to `super()`).
98
96
99
-
So we can't copy `import` to a variable or use `.call/apply` with it.
97
+
So we can't copy `import` to a variable or use `.call/apply` with it. That's not a function.
0 commit comments