33This example illustrates how to leverage the ` import() ` syntax to create ContextModules which are separated into separate chunks for each module in the ` ./templates ` folder.
44
55``` javascript
6-
76async function getTemplate (templateName ) {
87 try {
98 let template = await import (` ./templates/${ templateName} ` );
@@ -14,23 +13,23 @@ async function getTemplate(templateName) {
1413 }
1514}
1615
17- getTemplate (" a " );
18- getTemplate (" b " );
19- getTemplate (" c " );
16+ getTemplate (" foo " );
17+ getTemplate (" bar " );
18+ getTemplate (" baz " );
2019```
2120
2221# templates/
2322
24- * a .js
25- * b .js
26- * c .js
23+ * foo .js
24+ * baz .js
25+ * bar .js
2726
2827All templates are of this pattern:
2928
3029``` javascript
31- module . exports = function () {
32- return " This text was generated by template X " ;
33- }
30+ var foo = " foo " ;
31+
32+ export default foo ;
3433```
3534
3635# js/output.js
@@ -66,7 +65,7 @@ module.exports = function() {
6665
6766/** ****/ // objects to store loaded and loading chunks
6867/** ****/ var installedChunks = {
69- /** ****/ 6 : 0
68+ /** ****/ 3 : 0
7069/** ****/ };
7170
7271/** ****/ // The require function
@@ -174,15 +173,14 @@ module.exports = function() {
174173/** ****/ __webpack_require__ .oe = function (err ) { console .error (err); throw err; };
175174
176175/** ****/ // Load entry module and return exports
177- /** ****/ return __webpack_require__ (__webpack_require__ .s = 7 );
176+ /** ****/ return __webpack_require__ (__webpack_require__ .s = 4 );
178177/** ****/ })
179178/** **********************************************************************/
180179```
181180</details >
182181``` javascript
183- /** ****/ ({
184-
185- /** */ 0 :
182+ /** ****/ ([
183+ /* 0 */
186184/* unknown exports provided */
187185/* all exports used */
188186/* !**********************************!*\
@@ -191,52 +189,28 @@ module.exports = function() {
191189/** */ function (module , exports , __webpack_require__ ) {
192190
193191var map = {
194- " ./a" : [
195- 1 ,
196- 5
197- ],
198- " ./a.js" : [
199- 1 ,
200- 5
201- ],
202- " ./b" : [
203- 2 ,
204- 4
205- ],
206- " ./b.js" : [
207- 2 ,
208- 4
209- ],
210192 " ./bar" : [
211- 3 ,
212- 3
213- ],
214- " ./bar.js" : [
215- 3 ,
216- 3
217- ],
218- " ./baz" : [
219- 4 ,
193+ 1 ,
220194 2
221195 ],
222- " ./baz .js" : [
223- 4 ,
196+ " ./bar .js" : [
197+ 1 ,
224198 2
225199 ],
226- " ./c " : [
227- 5 ,
200+ " ./baz " : [
201+ 2 ,
228202 1
229203 ],
230- " ./c .js" : [
231- 5 ,
204+ " ./baz .js" : [
205+ 2 ,
232206 1
233207 ],
234208 " ./foo" : [
235- 6 ,
209+ 3 ,
236210 0
237211 ],
238212 " ./foo.js" : [
239- 6 ,
213+ 3 ,
240214 0
241215 ]
242216};
@@ -255,21 +229,17 @@ webpackAsyncContext.id = 0;
255229
256230
257231/** */ },
258-
259- /** */ 7 :
232+ /* 1 */ ,
233+ /* 2 */ ,
234+ /* 3 */ ,
235+ /* 4 */
260236/* unknown exports provided */
261237/* all exports used */
262238/* !********************!*\
263239 !*** ./example.js ***!
264240 \********************/
265241/** */ function (module , exports , __webpack_require__ ) {
266242
267- // function getTemplate(templateName) {
268- // return import("./templates/"+templateName);
269- // }
270- // console.log(getTemplate("a"));
271- // console.log(getTemplate("b"));
272-
273243async function getTemplate (templateName ) {
274244 try {
275245 let template = await __webpack_require__ (/* ! ./templates */ 0 )(` ./${ templateName} ` );
@@ -280,115 +250,86 @@ async function getTemplate(templateName) {
280250 }
281251}
282252
283- getTemplate (" a " );
284- getTemplate (" b " );
285- getTemplate (" c " );
253+ getTemplate (" foo " );
254+ getTemplate (" bar " );
255+ getTemplate (" baz " );
286256
287257
288258
289259
290260/** */ }
291-
292- /** ****/ });
261+ /** ****/ ]);
293262```
294263
295264# Info
296265
297266## Uncompressed
298267
299268```
300- Hash: 2143ae7aacbfebfb3d32
269+ Hash: 7808f67813880d1df93e
301270Version: webpack 2.1.0-beta.27
302- Time: 102ms
271+ Time: 188ms
303272 Asset Size Chunks Chunk Names
304- 0.output.js 399 bytes 0 [emitted]
305- 1.output.js 301 bytes 1 [emitted]
306- 2.output.js 399 bytes 2 [emitted]
307- 3.output.js 399 bytes 3 [emitted]
308- 4.output.js 301 bytes 4 [emitted]
309- 5.output.js 307 bytes 5 [emitted]
310- output.js 7.08 kB 6 [emitted] main
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
311277Entrypoint main = output.js
312- chunk {0} 0.output.js 38 bytes {6 } [rendered]
313- [6 ] ./templates/foo.js 38 bytes {0} [optional] [built]
278+ chunk {0} 0.output.js 41 bytes {3 } [rendered]
279+ [3 ] ./templates/foo.js 41 bytes {0} [optional] [built]
314280 [exports: default]
315281 context element ./foo [0] ./templates async ^\.\/.*$
316282 context element ./foo.js [0] ./templates async ^\.\/.*$
317- chunk {1} 1.output.js 80 bytes {6} [rendered]
318- [5] ./templates/c.js 80 bytes {1} [optional] [built]
319- context element ./c [0] ./templates async ^\.\/.*$
320- context element ./c.js [0] ./templates async ^\.\/.*$
321- chunk {2} 2.output.js 38 bytes {6} [rendered]
322- [4] ./templates/baz.js 38 bytes {2} [optional] [built]
283+ chunk {1} 1.output.js 41 bytes {3} [rendered]
284+ [2] ./templates/baz.js 41 bytes {1} [optional] [built]
323285 [exports: default]
324286 context element ./baz [0] ./templates async ^\.\/.*$
325287 context element ./baz.js [0] ./templates async ^\.\/.*$
326- chunk {3} 3 .output.js 38 bytes {6 } [rendered]
327- [3 ] ./templates/bar.js 38 bytes {3 } [optional] [built]
288+ chunk {2} 2 .output.js 41 bytes {3 } [rendered]
289+ [1 ] ./templates/bar.js 41 bytes {2 } [optional] [built]
328290 [exports: default]
329291 context element ./bar [0] ./templates async ^\.\/.*$
330292 context element ./bar.js [0] ./templates async ^\.\/.*$
331- chunk {4} 4.output.js 80 bytes {6} [rendered]
332- [2] ./templates/b.js 80 bytes {4} [optional] [built]
333- context element ./b [0] ./templates async ^\.\/.*$
334- context element ./b.js [0] ./templates async ^\.\/.*$
335- chunk {5} 5.output.js 80 bytes {6} [rendered]
336- [1] ./templates/a.js 80 bytes {5} [optional] [built]
337- context element ./a [0] ./templates async ^\.\/.*$
338- context element ./a.js [0] ./templates async ^\.\/.*$
339- chunk {6} output.js (main) 597 bytes [entry] [rendered]
340- > main [7] ./example.js
341- [0] ./templates async ^\.\/.*$ 160 bytes {6} [optional] [built]
342- System.import context ./templates [7] ./example.js 9:23-60
343- [7] ./example.js 437 bytes {6} [built]
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]
344298```
345299
346300## Minimized (uglify-js, no zip)
347301
348302```
349- Hash: 2143ae7aacbfebfb3d32
303+ Hash: 7808f67813880d1df93e
350304Version: webpack 2.1.0-beta.27
351- Time: 152ms
305+ Time: 293ms
352306 Asset Size Chunks Chunk Names
3533070.output.js 125 bytes 0 [emitted]
354- 1.output.js 105 bytes 1 [emitted]
355- 2.output.js 125 bytes 2 [emitted]
356- 3.output.js 125 bytes 3 [emitted]
357- 4.output.js 105 bytes 4 [emitted]
358- 5.output.js 104 bytes 5 [emitted]
359- output.js 6.75 kB 6 [emitted] main
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
360311Entrypoint main = output.js
361- chunk {0} 0.output.js 38 bytes {6 } [rendered]
362- [6 ] ./templates/foo.js 38 bytes {0} [optional] [built]
312+ chunk {0} 0.output.js 41 bytes {3 } [rendered]
313+ [3 ] ./templates/foo.js 41 bytes {0} [optional] [built]
363314 [exports: default]
364315 context element ./foo [0] ./templates async ^\.\/.*$
365316 context element ./foo.js [0] ./templates async ^\.\/.*$
366- chunk {1} 1.output.js 80 bytes {6} [rendered]
367- [5] ./templates/c.js 80 bytes {1} [optional] [built]
368- context element ./c [0] ./templates async ^\.\/.*$
369- context element ./c.js [0] ./templates async ^\.\/.*$
370- chunk {2} 2.output.js 38 bytes {6} [rendered]
371- [4] ./templates/baz.js 38 bytes {2} [optional] [built]
317+ chunk {1} 1.output.js 41 bytes {3} [rendered]
318+ [2] ./templates/baz.js 41 bytes {1} [optional] [built]
372319 [exports: default]
373320 context element ./baz [0] ./templates async ^\.\/.*$
374321 context element ./baz.js [0] ./templates async ^\.\/.*$
375- chunk {3} 3 .output.js 38 bytes {6 } [rendered]
376- [3 ] ./templates/bar.js 38 bytes {3 } [optional] [built]
322+ chunk {2} 2 .output.js 41 bytes {3 } [rendered]
323+ [1 ] ./templates/bar.js 41 bytes {2 } [optional] [built]
377324 [exports: default]
378325 context element ./bar [0] ./templates async ^\.\/.*$
379326 context element ./bar.js [0] ./templates async ^\.\/.*$
380- chunk {4} 4.output.js 80 bytes {6} [rendered]
381- [2] ./templates/b.js 80 bytes {4} [optional] [built]
382- context element ./b [0] ./templates async ^\.\/.*$
383- context element ./b.js [0] ./templates async ^\.\/.*$
384- chunk {5} 5.output.js 80 bytes {6} [rendered]
385- [1] ./templates/a.js 80 bytes {5} [optional] [built]
386- context element ./a [0] ./templates async ^\.\/.*$
387- context element ./a.js [0] ./templates async ^\.\/.*$
388- chunk {6} output.js (main) 597 bytes [entry] [rendered]
389- > main [7] ./example.js
390- [0] ./templates async ^\.\/.*$ 160 bytes {6} [optional] [built]
391- System.import context ./templates [7] ./example.js 9:23-60
392- [7] ./example.js 437 bytes {6} [built]
393-
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]
394335```
0 commit comments