Skip to content

Commit fdc781e

Browse files
authored
Merge pull request webpack#3454 from webpack/feature/add_context_native_import_code_split_example
feat(examples): add example for context modules + code split via import()
2 parents 2e2eac7 + 3bd4a4c commit fdc781e

7 files changed

Lines changed: 399 additions & 0 deletions

File tree

Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
# example.js
2+
3+
This example illustrates how to leverage the `import()` syntax to create ContextModules which are separated into separate chunks for each module in the `./templates` folder.
4+
5+
``` javascript
6+
async function getTemplate(templateName) {
7+
try {
8+
let template = await import(`./templates/${templateName}`);
9+
console.log(template);
10+
} catch(err) {
11+
console.error("template error");
12+
return new Error(err);
13+
}
14+
}
15+
16+
getTemplate("foo");
17+
getTemplate("bar");
18+
getTemplate("baz");
19+
```
20+
21+
# templates/
22+
23+
* foo.js
24+
* baz.js
25+
* bar.js
26+
27+
All templates are of this pattern:
28+
29+
``` javascript
30+
var foo = "foo";
31+
32+
export default foo;
33+
```
34+
35+
# js/output.js
36+
37+
<details><summary>`/******/ (function(modules) { /* webpackBootstrap */ })`</summary>
38+
``` javascript
39+
/******/ (function(modules) { // webpackBootstrap
40+
/******/ // install a JSONP callback for chunk loading
41+
/******/ var parentJsonpFunction = window["webpackJsonp"];
42+
/******/ window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {
43+
/******/ // add "moreModules" to the modules object,
44+
/******/ // then flag all "chunkIds" as loaded and fire callback
45+
/******/ var moduleId, chunkId, i = 0, resolves = [], result;
46+
/******/ for(;i < chunkIds.length; i++) {
47+
/******/ chunkId = chunkIds[i];
48+
/******/ if(installedChunks[chunkId])
49+
/******/ resolves.push(installedChunks[chunkId][0]);
50+
/******/ installedChunks[chunkId] = 0;
51+
/******/ }
52+
/******/ for(moduleId in moreModules) {
53+
/******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
54+
/******/ modules[moduleId] = moreModules[moduleId];
55+
/******/ }
56+
/******/ }
57+
/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);
58+
/******/ while(resolves.length)
59+
/******/ resolves.shift()();
60+
61+
/******/ };
62+
63+
/******/ // The module cache
64+
/******/ var installedModules = {};
65+
66+
/******/ // objects to store loaded and loading chunks
67+
/******/ var installedChunks = {
68+
/******/ 3: 0
69+
/******/ };
70+
71+
/******/ // The require function
72+
/******/ function __webpack_require__(moduleId) {
73+
74+
/******/ // Check if module is in cache
75+
/******/ if(installedModules[moduleId])
76+
/******/ return installedModules[moduleId].exports;
77+
78+
/******/ // Create a new module (and put it into the cache)
79+
/******/ var module = installedModules[moduleId] = {
80+
/******/ i: moduleId,
81+
/******/ l: false,
82+
/******/ exports: {}
83+
/******/ };
84+
85+
/******/ // Execute the module function
86+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
87+
88+
/******/ // Flag the module as loaded
89+
/******/ module.l = true;
90+
91+
/******/ // Return the exports of the module
92+
/******/ return module.exports;
93+
/******/ }
94+
95+
/******/ // This file contains only the entry chunk.
96+
/******/ // The chunk loading function for additional chunks
97+
/******/ __webpack_require__.e = function requireEnsure(chunkId) {
98+
/******/ if(installedChunks[chunkId] === 0)
99+
/******/ return Promise.resolve();
100+
101+
/******/ // an Promise means "currently loading".
102+
/******/ if(installedChunks[chunkId]) {
103+
/******/ return installedChunks[chunkId][2];
104+
/******/ }
105+
/******/ // start chunk loading
106+
/******/ var head = document.getElementsByTagName('head')[0];
107+
/******/ var script = document.createElement('script');
108+
/******/ script.type = 'text/javascript';
109+
/******/ script.charset = 'utf-8';
110+
/******/ script.async = true;
111+
/******/ script.timeout = 120000;
112+
113+
/******/ if (__webpack_require__.nc) {
114+
/******/ script.setAttribute("nonce", __webpack_require__.nc);
115+
/******/ }
116+
/******/ script.src = __webpack_require__.p + "" + chunkId + ".output.js";
117+
/******/ var timeout = setTimeout(onScriptComplete, 120000);
118+
/******/ script.onerror = script.onload = onScriptComplete;
119+
/******/ function onScriptComplete() {
120+
/******/ // avoid mem leaks in IE.
121+
/******/ script.onerror = script.onload = null;
122+
/******/ clearTimeout(timeout);
123+
/******/ var chunk = installedChunks[chunkId];
124+
/******/ if(chunk !== 0) {
125+
/******/ if(chunk) chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));
126+
/******/ installedChunks[chunkId] = undefined;
127+
/******/ }
128+
/******/ };
129+
/******/ head.appendChild(script);
130+
131+
/******/ var promise = new Promise(function(resolve, reject) {
132+
/******/ installedChunks[chunkId] = [resolve, reject];
133+
/******/ });
134+
/******/ return installedChunks[chunkId][2] = promise;
135+
/******/ };
136+
137+
/******/ // expose the modules object (__webpack_modules__)
138+
/******/ __webpack_require__.m = modules;
139+
140+
/******/ // expose the module cache
141+
/******/ __webpack_require__.c = installedModules;
142+
143+
/******/ // identity function for calling harmony imports with the correct context
144+
/******/ __webpack_require__.i = function(value) { return value; };
145+
146+
/******/ // define getter function for harmony exports
147+
/******/ __webpack_require__.d = function(exports, name, getter) {
148+
/******/ if(!__webpack_require__.o(exports, name)) {
149+
/******/ Object.defineProperty(exports, name, {
150+
/******/ configurable: false,
151+
/******/ enumerable: true,
152+
/******/ get: getter
153+
/******/ });
154+
/******/ }
155+
/******/ };
156+
157+
/******/ // getDefaultExport function for compatibility with non-harmony modules
158+
/******/ __webpack_require__.n = function(module) {
159+
/******/ var getter = module && module.__esModule ?
160+
/******/ function getDefault() { return module['default']; } :
161+
/******/ function getModuleExports() { return module; };
162+
/******/ __webpack_require__.d(getter, 'a', getter);
163+
/******/ return getter;
164+
/******/ };
165+
166+
/******/ // Object.prototype.hasOwnProperty.call
167+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
168+
169+
/******/ // __webpack_public_path__
170+
/******/ __webpack_require__.p = "js/";
171+
172+
/******/ // on error function for async loading
173+
/******/ __webpack_require__.oe = function(err) { console.error(err); throw err; };
174+
175+
/******/ // Load entry module and return exports
176+
/******/ return __webpack_require__(__webpack_require__.s = 4);
177+
/******/ })
178+
/************************************************************************/
179+
```
180+
</details>
181+
``` javascript
182+
/******/ ([
183+
/* 0 */
184+
/* unknown exports provided */
185+
/* all exports used */
186+
/*!**********************************!*\
187+
!*** ./templates async ^\.\/.*$ ***!
188+
\**********************************/
189+
/***/ function(module, exports, __webpack_require__) {
190+
191+
var map = {
192+
"./bar": [
193+
1,
194+
2
195+
],
196+
"./bar.js": [
197+
1,
198+
2
199+
],
200+
"./baz": [
201+
2,
202+
1
203+
],
204+
"./baz.js": [
205+
2,
206+
1
207+
],
208+
"./foo": [
209+
3,
210+
0
211+
],
212+
"./foo.js": [
213+
3,
214+
0
215+
]
216+
};
217+
function webpackAsyncContext(req) {
218+
var ids = map[req]; if(!ids)
219+
return Promise.reject(new Error("Cannot find module '" + req + "'."));
220+
return __webpack_require__.e(ids[1]).then(function() {
221+
return __webpack_require__(ids[0]);
222+
});
223+
};
224+
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
225+
return Object.keys(map);
226+
};
227+
module.exports = webpackAsyncContext;
228+
webpackAsyncContext.id = 0;
229+
230+
231+
/***/ },
232+
/* 1 */,
233+
/* 2 */,
234+
/* 3 */,
235+
/* 4 */
236+
/* unknown exports provided */
237+
/* all exports used */
238+
/*!********************!*\
239+
!*** ./example.js ***!
240+
\********************/
241+
/***/ function(module, exports, __webpack_require__) {
242+
243+
async function getTemplate(templateName) {
244+
try {
245+
let template = await __webpack_require__(/*! ./templates */ 0)(`./${templateName}`);
246+
console.log(template);
247+
} catch(err) {
248+
console.error("template error");
249+
return new Error(err);
250+
}
251+
}
252+
253+
getTemplate("foo");
254+
getTemplate("bar");
255+
getTemplate("baz");
256+
257+
258+
259+
260+
/***/ }
261+
/******/ ]);
262+
```
263+
264+
# Info
265+
266+
## Uncompressed
267+
268+
```
269+
Hash: 7808f67813880d1df93e
270+
Version: webpack 2.1.0-beta.27
271+
Time: 188ms
272+
Asset Size Chunks Chunk Names
273+
0.output.js 402 bytes 0 [emitted]
274+
1.output.js 402 bytes 1 [emitted]
275+
2.output.js 408 bytes 2 [emitted]
276+
output.js 6.81 kB 3 [emitted] main
277+
Entrypoint main = output.js
278+
chunk {0} 0.output.js 41 bytes {3} [rendered]
279+
[3] ./templates/foo.js 41 bytes {0} [optional] [built]
280+
[exports: default]
281+
context element ./foo [0] ./templates async ^\.\/.*$
282+
context element ./foo.js [0] ./templates async ^\.\/.*$
283+
chunk {1} 1.output.js 41 bytes {3} [rendered]
284+
[2] ./templates/baz.js 41 bytes {1} [optional] [built]
285+
[exports: default]
286+
context element ./baz [0] ./templates async ^\.\/.*$
287+
context element ./baz.js [0] ./templates async ^\.\/.*$
288+
chunk {2} 2.output.js 41 bytes {3} [rendered]
289+
[1] ./templates/bar.js 41 bytes {2} [optional] [built]
290+
[exports: default]
291+
context element ./bar [0] ./templates async ^\.\/.*$
292+
context element ./bar.js [0] ./templates async ^\.\/.*$
293+
chunk {3} output.js (main) 456 bytes [entry] [rendered]
294+
> main [4] ./example.js
295+
[0] ./templates async ^\.\/.*$ 160 bytes {3} [optional] [built]
296+
System.import context ./templates [4] ./example.js 3:23-60
297+
[4] ./example.js 296 bytes {3} [built]
298+
```
299+
300+
## Minimized (uglify-js, no zip)
301+
302+
```
303+
Hash: 7808f67813880d1df93e
304+
Version: webpack 2.1.0-beta.27
305+
Time: 293ms
306+
Asset Size Chunks Chunk Names
307+
0.output.js 125 bytes 0 [emitted]
308+
1.output.js 125 bytes 1 [emitted]
309+
2.output.js 124 bytes 2 [emitted]
310+
output.js 6.49 kB 3 [emitted] main
311+
Entrypoint main = output.js
312+
chunk {0} 0.output.js 41 bytes {3} [rendered]
313+
[3] ./templates/foo.js 41 bytes {0} [optional] [built]
314+
[exports: default]
315+
context element ./foo [0] ./templates async ^\.\/.*$
316+
context element ./foo.js [0] ./templates async ^\.\/.*$
317+
chunk {1} 1.output.js 41 bytes {3} [rendered]
318+
[2] ./templates/baz.js 41 bytes {1} [optional] [built]
319+
[exports: default]
320+
context element ./baz [0] ./templates async ^\.\/.*$
321+
context element ./baz.js [0] ./templates async ^\.\/.*$
322+
chunk {2} 2.output.js 41 bytes {3} [rendered]
323+
[1] ./templates/bar.js 41 bytes {2} [optional] [built]
324+
[exports: default]
325+
context element ./bar [0] ./templates async ^\.\/.*$
326+
context element ./bar.js [0] ./templates async ^\.\/.*$
327+
chunk {3} output.js (main) 456 bytes [entry] [rendered]
328+
> main [4] ./example.js
329+
[0] ./templates async ^\.\/.*$ 160 bytes {3} [optional] [built]
330+
System.import context ./templates [4] ./example.js 3:23-60
331+
[4] ./example.js 296 bytes {3} [built]
332+
333+
ERROR in output.js from UglifyJs
334+
SyntaxError: Unexpected token: keyword (function) [output.js:192,6]
335+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../build-common");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
async function getTemplate(templateName) {
2+
try {
3+
let template = await import(`./templates/${templateName}`);
4+
console.log(template);
5+
} catch(err) {
6+
console.error("template error");
7+
return new Error(err);
8+
}
9+
}
10+
11+
getTemplate("foo");
12+
getTemplate("bar");
13+
getTemplate("baz");
14+
15+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# example.js
2+
3+
This example illustrates how to leverage the `import()` syntax to create ContextModules which are separated into separate chunks for each module in the `./templates` folder.
4+
5+
``` javascript
6+
{{example.js}}
7+
```
8+
9+
# templates/
10+
11+
* foo.js
12+
* baz.js
13+
* bar.js
14+
15+
All templates are of this pattern:
16+
17+
``` javascript
18+
{{templates/foo.js}}
19+
```
20+
21+
# js/output.js
22+
23+
``` javascript
24+
{{js/output.js}}
25+
```
26+
27+
# Info
28+
29+
## Uncompressed
30+
31+
```
32+
{{stdout}}
33+
```
34+
35+
## Minimized (uglify-js, no zip)
36+
37+
```
38+
{{min:stdout}}
39+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var bar = "bar";
2+
3+
export default bar;

0 commit comments

Comments
 (0)