-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path57_dynamic_imports.js
118 lines (107 loc) · 3.82 KB
/
57_dynamic_imports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* ========================================================
* Dynamic Import in JavaScript
* ========================================================
* Dynamic import() syntax in JavaScript enables you to import modules on-the-fly, providing the
* ability to load modules conditionally or on demand.
*/
/**
* ========================================================
* 1. Basic Usage
* ========================================================
* Using 'await import()' allows you to import a module dynamically.
* This returns a promise that resolves to the imported module.
*/
async function basicUsage() {
const module = await import("./myModule.js");
module.someFunction(); // Call the function from the dynamically imported module
}
/**
* ========================================================
* 2. Using with Promises
* ========================================================
* Alternatively, you can use the Promise-based '.then()' syntax to handle dynamic imports.
*/
function usingPromise() {
import("./myModule.js")
.then((module) => {
module.someFunction(); // Call the function from the dynamically imported module
})
.catch((err) => {
console.error("Failed to load the module:", err);
});
}
/**
* ========================================================
* 3. Dynamic Path
* ========================================================
* The path from which to dynamically import can be constructed at runtime.
*/
const moduleName = "module1";
import(`./${moduleName}.js`).then((module) => {
module.someFunction(); // Invoke function from dynamically determined module
});
/**
* ========================================================
* 4. Importing Multiple Modules
* ========================================================
* 'Promise.all()' can be used to import multiple modules simultaneously.
*/
Promise.all([import("./module1.js"), import("./module2.js")]).then(([module1, module2]) => {
module1.someFunction();
module2.anotherFunction();
});
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. Code Splitting
* -----------------
* Dynamic imports enable code splitting, particularly useful when used with bundlers like Webpack.
*/
if (someCondition) {
import("./heavyModule.js").then((module) => {
module.heavyComputation(); // Load and use only when 'someCondition' is true
});
}
/**
* 2. Fallback Logic
* -----------------
* If importing a module fails, you can specify fallback behavior.
*/
import("./optionalModule.js")
.catch(() => import("./fallbackModule.js"))
.then((module) => {
module.someFunction(); // Use the fallback module if the original fails to import
});
/**
* 3. Importing JSON and Other Non-JS Files
* ----------------------------------------
* With proper bundler configuration, you can dynamically import file types other than JavaScript.
*/
import("./config.json").then((config) => {
console.log("Config loaded:", config.default);
});
/**
* 4. Prefetching Modules
* ----------------------
* Prefetching can be done by importing a module and not using it immediately.
*/
import("./prefetchMe.js").catch(() => {
// Prefetch silently and handle any load errors
});
/**
* 5. Use With React Lazy
* ----------------------
* In React applications, dynamic import can be combined with React.lazy for component-level code splitting.
*/
// const LazyComponent = React.lazy(() => import('./LazyComponent'));
/**
* 6. Caching
* ----------
* Once a module is dynamically imported, it gets cached. Further imports won't trigger additional network requests.
*/
import("./myModule.js"); // Network request made
import("./myModule.js"); // Cached, no additional network request