webpack/examples/code-splitting-harmony/README.md

325 lines
10 KiB
Markdown
Raw Normal View History

2015-09-03 17:36:30 +08:00
This example show how to use Code Splitting with the ES6 module syntax.
The standard `import` is sync.
`System.import(module: string) -> Promise` can be used to load modules on demand. This acts as split point for webpack and creates a chunk.
Providing dynamic expressions to `System.import` is possible. The same limits as with dynamic expressions in `require` calls apply here. Each possible module creates an additional chunk. In this example `System.import("c/" + name)` creates two additional chunks (one for each file in `node_modules/c/`). This is called "async context".
# example.js
``` javascript
import a from "a";
System.import("b").then(function(b) {
console.log("b loaded", b);
})
function loadC(name) {
return System.import("c/" + name)
}
Promise.all([loadC("1"), loadC("2")]).then(function(arr) {
console.log("c/1 and c/2 loaded", arr);
});
```
# js/output.js
2016-09-07 18:28:56 +08:00
<details><summary>`/******/ (function(modules) { /* webpackBootstrap */ })`</summary>
2015-09-03 17:36:30 +08:00
``` javascript
/******/ (function(modules) { // webpackBootstrap
/******/ // install a JSONP callback for chunk loading
/******/ var parentJsonpFunction = window["webpackJsonp"];
2016-06-06 02:51:44 +08:00
/******/ window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {
2015-09-03 17:36:30 +08:00
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
2016-06-06 02:51:44 +08:00
/******/ var moduleId, chunkId, i = 0, resolves = [], result;
2015-09-03 17:36:30 +08:00
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
/******/ if(installedChunks[chunkId])
/******/ resolves.push(installedChunks[chunkId][0]);
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
2016-06-06 02:51:44 +08:00
/******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
/******/ modules[moduleId] = moreModules[moduleId];
/******/ }
2015-09-03 17:36:30 +08:00
/******/ }
2016-06-06 02:51:44 +08:00
/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);
2015-09-03 17:36:30 +08:00
/******/ while(resolves.length)
/******/ resolves.shift()();
/******/ };
/******/ // The module cache
/******/ var installedModules = {};
/******/ // objects to store loaded and loading chunks
/******/ var installedChunks = {
2016-09-07 18:28:56 +08:00
/******/ 3: 0
2015-09-03 17:36:30 +08:00
/******/ };
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2015-09-03 17:36:30 +08:00
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2015-09-03 17:36:30 +08:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // This file contains only the entry chunk.
/******/ // The chunk loading function for additional chunks
/******/ __webpack_require__.e = function requireEnsure(chunkId) {
/******/ if(installedChunks[chunkId] === 0)
2016-06-06 02:51:44 +08:00
/******/ return Promise.resolve();
2015-09-03 17:36:30 +08:00
/******/ // an Promise means "currently loading".
/******/ if(installedChunks[chunkId]) {
/******/ return installedChunks[chunkId][2];
/******/ }
/******/ // start chunk loading
/******/ var head = document.getElementsByTagName('head')[0];
/******/ var script = document.createElement('script');
/******/ script.type = 'text/javascript';
/******/ script.charset = 'utf-8';
/******/ script.async = true;
2016-02-04 07:27:47 +08:00
/******/ script.timeout = 120000;
/******/ script.src = __webpack_require__.p + "" + chunkId + ".output.js";
2016-02-04 07:27:47 +08:00
/******/ var timeout = setTimeout(onScriptComplete, 120000);
/******/ script.onerror = script.onload = onScriptComplete;
/******/ function onScriptComplete() {
/******/ // avoid mem leaks in IE.
/******/ script.onerror = script.onload = null;
/******/ clearTimeout(timeout);
/******/ var chunk = installedChunks[chunkId];
/******/ if(chunk !== 0) {
/******/ if(chunk) chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));
/******/ installedChunks[chunkId] = undefined;
/******/ }
/******/ };
2015-09-03 17:36:30 +08:00
/******/ head.appendChild(script);
/******/ var promise = new Promise(function(resolve, reject) {
/******/ installedChunks[chunkId] = [resolve, reject];
/******/ });
/******/ return installedChunks[chunkId][2] = promise;
/******/ };
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
2016-12-08 02:14:47 +08:00
/******/ // identity function for calling harmony imports with the correct context
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__.i = function(value) { return value; };
2016-12-08 02:14:47 +08:00
/******/ // define getter function for harmony exports
2016-09-07 18:28:56 +08:00
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ };
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2016-02-04 20:02:53 +08:00
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "js/";
2015-09-03 17:36:30 +08:00
/******/ // on error function for async loading
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__.oe = function(err) { console.error(err); throw err; };
2015-09-03 17:36:30 +08:00
/******/ // Load entry module and return exports
2016-09-07 18:28:56 +08:00
/******/ return __webpack_require__(__webpack_require__.s = 5);
2015-09-03 17:36:30 +08:00
/******/ })
/************************************************************************/
2016-09-07 18:28:56 +08:00
```
</details>
``` javascript
2015-09-03 17:36:30 +08:00
/******/ ([
/* 0 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
2015-09-03 17:36:30 +08:00
/*!****************!*\
!*** ./~/a.js ***!
\****************/
/***/ function(module, exports) {
2016-09-07 18:28:56 +08:00
// module a
2015-09-03 17:36:30 +08:00
/***/ },
/* 1 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2015-09-03 17:36:30 +08:00
/*!****************************!*\
!*** ./~/c async ^\.\/.*$ ***!
\****************************/
/***/ function(module, exports, __webpack_require__) {
2016-09-07 18:28:56 +08:00
var map = {
"./1": [
2,
1
],
"./1.js": [
2,
1
],
"./2": [
3,
0
],
"./2.js": [
3,
0
]
};
function webpackAsyncContext(req) {
var ids = map[req]; if(!ids)
return Promise.reject(new Error("Cannot find module '" + req + "'."));
return __webpack_require__.e(ids[1]).then(function() {
return __webpack_require__(ids[0]);
});
};
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
return Object.keys(map);
};
module.exports = webpackAsyncContext;
webpackAsyncContext.id = 1;
2015-09-03 17:36:30 +08:00
/***/ },
/* 2 */,
/* 3 */,
2016-09-07 18:28:56 +08:00
/* 4 */,
/* 5 */
/* unknown exports provided */
/* all exports used */
2015-09-03 17:36:30 +08:00
/*!********************!*\
!*** ./example.js ***!
\********************/
/***/ function(module, exports, __webpack_require__) {
2016-09-07 18:28:56 +08:00
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_a__ = __webpack_require__(/*! a */ 0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_a___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_a__);
2016-02-04 07:27:47 +08:00
2015-09-03 17:36:30 +08:00
2016-09-07 18:28:56 +08:00
__webpack_require__.e/* System.import */(2).then(__webpack_require__.bind(null, /*! b */ 4)).then(function(b) {
console.log("b loaded", b);
})
2015-09-03 17:36:30 +08:00
2016-09-07 18:28:56 +08:00
function loadC(name) {
return __webpack_require__(/*! c */ 1)("./" + name)
}
2015-09-03 17:36:30 +08:00
2016-09-07 18:28:56 +08:00
Promise.all([loadC("1"), loadC("2")]).then(function(arr) {
console.log("c/1 and c/2 loaded", arr);
});
2015-09-03 17:36:30 +08:00
/***/ }
/******/ ]);
```
# Info
## Uncompressed
```
Hash: 4b129c7737355e51d1bd
Version: webpack 2.1.0-beta.25
Time: 151ms
Asset Size Chunks Chunk Names
0.output.js 216 bytes 0 [emitted]
1.output.js 216 bytes 1 [emitted]
2.output.js 208 bytes 2 [emitted]
output.js 6.94 kB 3 [emitted] main
2016-09-07 18:28:56 +08:00
Entrypoint main = output.js
chunk {0} 0.output.js 13 bytes {3} [rendered]
2015-09-03 17:36:30 +08:00
[3] ./~/c/2.js 13 bytes {0} [optional] [built]
context element ./2 [1] ./~/c async ^\.\/.*$
2016-02-04 07:27:47 +08:00
context element ./2.js [1] ./~/c async ^\.\/.*$
chunk {1} 1.output.js 13 bytes {3} [rendered]
2015-09-03 17:36:30 +08:00
[2] ./~/c/1.js 13 bytes {1} [optional] [built]
context element ./1 [1] ./~/c async ^\.\/.*$
2016-02-04 07:27:47 +08:00
context element ./1.js [1] ./~/c async ^\.\/.*$
chunk {2} 2.output.js 11 bytes {3} [rendered]
2016-09-07 18:28:56 +08:00
> [5] ./example.js 3:0-18
[4] ./~/b.js 11 bytes {2} [built]
System.import b [5] ./example.js 3:0-18
chunk {3} output.js (main) 427 bytes [entry] [rendered]
2016-09-07 18:28:56 +08:00
> main [5] ./example.js
[0] ./~/a.js 11 bytes {3} [built]
[no exports used]
harmony import a [5] ./example.js 1:0-18
[1] ./~/c async ^\.\/.*$ 160 bytes {3} [built]
System.import context c [5] ./example.js 8:8-34
[5] ./example.js 256 bytes {3} [built]
2015-09-03 17:36:30 +08:00
```
## Minimized (uglify-js, no zip)
```
Hash: 4b129c7737355e51d1bd
Version: webpack 2.1.0-beta.25
Time: 321ms
Asset Size Chunks Chunk Names
0.output.js 38 bytes 0 [emitted]
1.output.js 38 bytes 1 [emitted]
2.output.js 38 bytes 2 [emitted]
output.js 1.83 kB 3 [emitted] main
2016-09-07 18:28:56 +08:00
Entrypoint main = output.js
chunk {0} 0.output.js 13 bytes {3} [rendered]
2015-09-03 17:36:30 +08:00
[3] ./~/c/2.js 13 bytes {0} [optional] [built]
context element ./2 [1] ./~/c async ^\.\/.*$
2016-02-04 07:27:47 +08:00
context element ./2.js [1] ./~/c async ^\.\/.*$
chunk {1} 1.output.js 13 bytes {3} [rendered]
2015-09-03 17:36:30 +08:00
[2] ./~/c/1.js 13 bytes {1} [optional] [built]
context element ./1 [1] ./~/c async ^\.\/.*$
2016-02-04 07:27:47 +08:00
context element ./1.js [1] ./~/c async ^\.\/.*$
chunk {2} 2.output.js 11 bytes {3} [rendered]
2016-09-07 18:28:56 +08:00
> [5] ./example.js 3:0-18
[4] ./~/b.js 11 bytes {2} [built]
System.import b [5] ./example.js 3:0-18
chunk {3} output.js (main) 427 bytes [entry] [rendered]
2016-09-07 18:28:56 +08:00
> main [5] ./example.js
[0] ./~/a.js 11 bytes {3} [built]
[no exports used]
harmony import a [5] ./example.js 1:0-18
[1] ./~/c async ^\.\/.*$ 160 bytes {3} [built]
System.import context c [5] ./example.js 8:8-34
[5] ./example.js 256 bytes {3} [built]
2015-09-03 17:36:30 +08:00
```