webpack/examples/scope-hoisting/template.md

114 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

2017-05-10 19:15:14 +08:00
This example demonstrates Scope Hoisting in combination with Code Splitting.
This is the dependency graph for the example: (solid lines express sync imports, dashed lines async imports)
![](graph.png)
All modules except `cjs` are EcmaScript modules. `cjs` is a CommonJs module.
The interesting thing here is that putting all modules in single scope won't work, because of multiple reasons:
- Modules `lazy`, `c`, `d` and `cjs` need to be in a separate chunk
- Module `shared` is accessed by two chunks (different scopes)
- Module `cjs` is a CommonJs module
2017-05-10 19:15:14 +08:00
![](graph2.png)
webpack therefore uses a approach called **"Partial Scope Hoisting"** or "Module concatenation", which chooses the largest possible subsets of ES modules which can be scope hoisted and combines them with the default webpack primitives.
![](graph3.png)
2018-02-26 10:26:51 +08:00
While module concatenation identifiers in modules are renamed to avoid conflicts and internal imports are simplified. External imports and exports from the root module use the existing ESM constructs.
2017-05-10 19:15:14 +08:00
# example.js
```javascript
_{{example.js}}_
2017-05-10 19:15:14 +08:00
```
# lazy.js
```javascript
_{{lazy.js}}_
2017-05-10 19:15:14 +08:00
```
# a.js
```javascript
_{{node_modules/a.js}}_
2017-05-10 19:15:14 +08:00
```
# b.js
```javascript
_{{node_modules/b.js}}_
2017-05-10 19:15:14 +08:00
```
# c.js
```javascript
_{{node_modules/c.js}}_
2017-05-10 19:15:14 +08:00
```
# d.js
```javascript
_{{node_modules/d.js}}_
2017-05-10 19:15:14 +08:00
```
# cjs.js
```javascript
_{{node_modules/cjs.js}}_
2017-05-10 19:15:14 +08:00
```
# shared.js
```javascript
_{{node_modules/shared.js}}_
2017-05-10 19:15:14 +08:00
```
# shared2.js
```javascript
_{{node_modules/shared2.js}}_
2017-05-10 19:15:14 +08:00
```
# webpack.config.js
```javascript
_{{webpack.config.js}}_
2017-05-10 19:15:14 +08:00
```
# dist/output.js
2017-05-10 19:15:14 +08:00
```javascript
_{{dist/output.js}}_
2017-05-10 19:15:14 +08:00
```
2020-09-21 04:39:12 +08:00
# dist/872.output.js
2017-05-10 19:15:14 +08:00
```javascript
2020-09-21 04:39:12 +08:00
_{{dist/872.output.js}}_
2017-05-10 19:15:14 +08:00
```
Minimized
```javascript
2020-09-21 04:39:12 +08:00
_{{production:dist/872.output.js}}_
2017-05-10 19:15:14 +08:00
```
# Info
## Unoptimized
2017-05-10 19:15:14 +08:00
```
_{{stdout}}_
2017-05-10 19:15:14 +08:00
```
## Production mode
2017-05-10 19:15:14 +08:00
```
_{{production:stdout}}_
2017-05-10 19:15:14 +08:00
```