webpack/examples/code-splitting-native-impor.../README.md

393 lines
13 KiB
Markdown
Raw Normal View History

# example.js
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.
``` javascript
async function getTemplate(templateName) {
try {
let template = await import(`./templates/${templateName}`);
console.log(template);
} catch(err) {
console.error("template error");
return new Error(err);
}
}
getTemplate("foo");
getTemplate("bar");
getTemplate("baz");
```
# templates/
* foo.js
* baz.js
* bar.js
All templates are of this pattern:
``` javascript
var foo = "foo";
export default foo;
```
# dist/output.js
2017-03-31 02:42:42 +08:00
<details><summary><code>/******/ (function(modules) { /* webpackBootstrap */ })</code></summary>
``` javascript
/******/ (function(modules) { // webpackBootstrap
/******/ // install a JSONP callback for chunk loading
2017-11-23 16:47:19 +08:00
/******/ function webpackJsonpCallback(data) {
/******/ var chunkIds = data[0];
2018-04-04 21:17:13 +08:00
/******/ var moreModules = data[1];
/******/
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
/******/ var moduleId, chunkId, i = 0, resolves = [];
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
2017-05-23 04:45:18 +08:00
/******/ if(installedChunks[chunkId]) {
/******/ resolves.push(installedChunks[chunkId][0]);
2017-05-23 04:45:18 +08:00
/******/ }
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
/******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
/******/ modules[moduleId] = moreModules[moduleId];
/******/ }
/******/ }
2017-11-23 16:47:19 +08:00
/******/ if(parentJsonpFunction) parentJsonpFunction(data);
2017-05-23 04:45:18 +08:00
/******/ while(resolves.length) {
/******/ resolves.shift()();
2017-05-23 04:45:18 +08:00
/******/ }
2017-03-31 02:25:01 +08:00
/******/
/******/ };
2017-03-31 02:25:01 +08:00
/******/
/******/
/******/ // The module cache
/******/ var installedModules = {};
2017-03-31 02:25:01 +08:00
/******/
2017-11-23 16:47:19 +08:00
/******/ // object to store loaded and loading chunks
2018-05-07 18:36:38 +08:00
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
/******/ // Promise = chunk loading, 0 = chunk loaded
/******/ var installedChunks = {
/******/ 3: 0
/******/ };
2017-03-31 02:25:01 +08:00
/******/
/******/
2017-11-23 16:47:19 +08:00
/******/
2018-05-07 18:36:38 +08:00
/******/ // script path function
/******/ function jsonpScriptSrc(chunkId) {
/******/ return __webpack_require__.p + "" + chunkId + ".output.js"
/******/ }
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
2017-03-31 02:25:01 +08:00
/******/
/******/ // Check if module is in cache
2017-05-23 04:45:18 +08:00
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
2017-05-23 04:45:18 +08:00
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
2017-03-31 02:25:01 +08:00
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2017-03-31 02:25:01 +08:00
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
2017-03-31 02:25:01 +08:00
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2017-03-31 02:25:01 +08:00
/******/
/******/ // This file contains only the entry chunk.
/******/ // The chunk loading function for additional chunks
/******/ __webpack_require__.e = function requireEnsure(chunkId) {
2017-11-23 16:47:19 +08:00
/******/ var promises = [];
2017-03-31 02:25:01 +08:00
/******/
/******/
2017-11-23 16:47:19 +08:00
/******/ // JSONP chunk loading for javascript
2017-03-31 02:25:01 +08:00
/******/
2017-11-23 16:47:19 +08:00
/******/ var installedChunkData = installedChunks[chunkId];
/******/ if(installedChunkData !== 0) { // 0 means "already installed".
2017-03-31 02:25:01 +08:00
/******/
2017-11-23 16:47:19 +08:00
/******/ // a Promise means "currently loading".
/******/ if(installedChunkData) {
/******/ promises.push(installedChunkData[2]);
/******/ } else {
/******/ // setup Promise in chunk cache
/******/ var promise = new Promise(function(resolve, reject) {
/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject];
/******/ });
/******/ promises.push(installedChunkData[2] = promise);
/******/
/******/ // start chunk loading
/******/ var head = document.getElementsByTagName('head')[0];
/******/ var script = document.createElement('script');
2018-01-05 04:14:02 +08:00
/******/
2017-11-23 16:47:19 +08:00
/******/ script.charset = 'utf-8';
2018-04-04 21:17:13 +08:00
/******/ script.timeout = 120;
2017-11-23 16:47:19 +08:00
/******/
/******/ if (__webpack_require__.nc) {
/******/ script.setAttribute("nonce", __webpack_require__.nc);
2017-05-23 04:45:18 +08:00
/******/ }
2018-05-07 18:36:38 +08:00
/******/ script.src = jsonpScriptSrc(chunkId);
2017-11-23 16:47:19 +08:00
/******/ var timeout = setTimeout(function(){
/******/ onScriptComplete({ type: 'timeout', target: script });
/******/ }, 120000);
/******/ script.onerror = script.onload = onScriptComplete;
/******/ function onScriptComplete(event) {
/******/ // avoid mem leaks in IE.
/******/ script.onerror = script.onload = null;
/******/ clearTimeout(timeout);
/******/ var chunk = installedChunks[chunkId];
/******/ if(chunk !== 0) {
/******/ if(chunk) {
/******/ var errorType = event && (event.type === 'load' ? 'missing' : event.type);
/******/ var realSrc = event && event.target && event.target.src;
/******/ var error = new Error('Loading chunk ' + chunkId + ' failed.\n(' + errorType + ': ' + realSrc + ')');
/******/ error.type = errorType;
/******/ error.request = realSrc;
/******/ chunk[1](error);
/******/ }
/******/ installedChunks[chunkId] = undefined;
/******/ }
/******/ };
/******/ head.appendChild(script);
/******/ }
2017-11-23 16:47:19 +08:00
/******/ }
/******/ return Promise.all(promises);
/******/ };
2017-03-31 02:25:01 +08:00
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
2017-03-31 02:25:01 +08:00
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
2017-03-31 02:25:01 +08:00
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2017-11-24 15:40:39 +08:00
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // 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;
/******/ };
2017-03-31 02:25:01 +08:00
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-31 02:25:01 +08:00
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "dist/";
2017-03-31 02:25:01 +08:00
/******/
/******/ // on error function for async loading
/******/ __webpack_require__.oe = function(err) { console.error(err); throw err; };
2017-03-31 02:25:01 +08:00
/******/
2017-11-23 16:47:19 +08:00
/******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];
/******/ var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);
2017-11-23 16:47:19 +08:00
/******/ jsonpArray.push = webpackJsonpCallback;
/******/ jsonpArray = jsonpArray.slice();
/******/ for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);
/******/ var parentJsonpFunction = oldJsonpFunction;
/******/
2017-11-23 16:47:19 +08:00
/******/
/******/ // Load entry module and return exports
2017-12-21 18:19:53 +08:00
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
```
2017-03-31 02:42:42 +08:00
</details>
2017-03-31 02:42:42 +08:00
``` javascript
/******/ ([
2017-06-05 22:12:12 +08:00
/* 0 */,
/* 1 */,
/* 2 */,
/* 3 */
2017-12-04 21:35:55 +08:00
/*!**************************************************!*\
!*** ./templates lazy ^\.\/.*$ namespace object ***!
\**************************************************/
2017-06-05 22:12:12 +08:00
/*! no static exports found */
2017-02-13 15:49:35 +08:00
/***/ (function(module, exports, __webpack_require__) {
var map = {
"./bar": [
2017-12-21 18:19:53 +08:00
2,
2
],
"./bar.js": [
2017-12-21 18:19:53 +08:00
2,
2
],
"./baz": [
2017-06-05 22:12:12 +08:00
1,
1
],
"./baz.js": [
2017-06-05 22:12:12 +08:00
1,
1
],
"./foo": [
2017-12-21 18:19:53 +08:00
0,
0
],
"./foo.js": [
2017-12-21 18:19:53 +08:00
0,
0
]
};
function webpackAsyncContext(req) {
2017-03-31 02:25:01 +08:00
var ids = map[req];
2018-01-03 18:03:20 +08:00
if(!ids) {
return Promise.resolve().then(function() {
var e = new Error("Cannot find module '" + req + "'");
2018-01-03 18:03:20 +08:00
e.code = 'MODULE_NOT_FOUND';
throw e;
});
}
return __webpack_require__.e(ids[1]).then(function() {
2017-12-04 21:35:55 +08:00
var module = __webpack_require__(ids[0]);
2018-01-06 17:46:03 +08:00
return module;
});
2017-12-04 21:35:55 +08:00
}
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
return Object.keys(map);
};
2017-12-21 18:19:53 +08:00
webpackAsyncContext.id = 3;
2017-08-08 20:00:06 +08:00
module.exports = webpackAsyncContext;
2017-12-21 18:19:53 +08:00
/***/ }),
/* 4 */
/*!********************!*\
!*** ./example.js ***!
\********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
async function getTemplate(templateName) {
try {
2018-01-03 18:03:20 +08:00
let template = await __webpack_require__(3)(`./${templateName}`);
2017-12-21 18:19:53 +08:00
console.log(template);
} catch(err) {
console.error("template error");
return new Error(err);
}
}
getTemplate("foo");
getTemplate("bar");
getTemplate("baz");
2017-02-13 15:49:35 +08:00
/***/ })
/******/ ]);
```
# Info
2017-12-14 17:58:03 +08:00
## Unoptimized
```
2017-12-14 17:58:03 +08:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-05-07 18:36:38 +08:00
Version: webpack 4.8.0
Asset Size Chunks Chunk Names
2017-12-21 18:19:53 +08:00
0.output.js 436 bytes 0 [emitted]
2017-12-14 17:58:03 +08:00
1.output.js 445 bytes 1 [emitted]
2017-12-21 18:19:53 +08:00
2.output.js 439 bytes 2 [emitted]
2018-05-07 18:36:38 +08:00
output.js 8.3 KiB 3 [emitted] main
Entrypoint main = output.js
chunk {0} 0.output.js 41 bytes <{3}> [rendered]
> ./foo [3] ./templates lazy ^\.\/.*$ namespace object ./foo
> ./foo.js [3] ./templates lazy ^\.\/.*$ namespace object ./foo.js
2018-05-07 18:36:38 +08:00
[0] ./templates/foo.js 41 bytes {0} [optional] [built]
[exports: default]
context element ./foo.js [3] ./templates lazy ^\.\/.*$ namespace object ./foo.js
context element ./foo [3] ./templates lazy ^\.\/.*$ namespace object ./foo
chunk {1} 1.output.js 41 bytes <{3}> [rendered]
> ./baz [3] ./templates lazy ^\.\/.*$ namespace object ./baz
> ./baz.js [3] ./templates lazy ^\.\/.*$ namespace object ./baz.js
2018-05-07 18:36:38 +08:00
[1] ./templates/baz.js 41 bytes {1} [optional] [built]
[exports: default]
context element ./baz.js [3] ./templates lazy ^\.\/.*$ namespace object ./baz.js
context element ./baz [3] ./templates lazy ^\.\/.*$ namespace object ./baz
chunk {2} 2.output.js 41 bytes <{3}> [rendered]
> ./bar [3] ./templates lazy ^\.\/.*$ namespace object ./bar
> ./bar.js [3] ./templates lazy ^\.\/.*$ namespace object ./bar.js
2018-05-07 18:36:38 +08:00
[2] ./templates/bar.js 41 bytes {2} [optional] [built]
[exports: default]
context element ./bar.js [3] ./templates lazy ^\.\/.*$ namespace object ./bar.js
context element ./bar [3] ./templates lazy ^\.\/.*$ namespace object ./bar
chunk {3} output.js (main) 456 bytes >{0}< >{1}< >{2}< [entry] [rendered]
> .\example.js main
2018-05-07 18:36:38 +08:00
[3] ./templates lazy ^\.\/.*$ namespace object 160 bytes {3} [optional] [built]
import() context lazy ./templates [4] ./example.js 3:23-60
[4] ./example.js 296 bytes {3} [built]
single entry .\example.js main
```
2017-12-14 17:58:03 +08:00
## Production mode
```
2017-12-14 17:58:03 +08:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-05-07 18:36:38 +08:00
Version: webpack 4.8.0
2017-12-14 17:58:03 +08:00
Asset Size Chunks Chunk Names
2017-12-21 18:19:53 +08:00
0.output.js 113 bytes 0 [emitted]
2017-12-14 17:58:03 +08:00
1.output.js 114 bytes 1 [emitted]
2017-12-21 18:19:53 +08:00
2.output.js 115 bytes 2 [emitted]
2018-05-07 18:36:38 +08:00
output.js 2.12 KiB 3 [emitted] main
Entrypoint main = output.js
chunk {0} 0.output.js 41 bytes <{3}> [rendered]
> ./foo [3] ./templates lazy ^\.\/.*$ namespace object ./foo
> ./foo.js [3] ./templates lazy ^\.\/.*$ namespace object ./foo.js
2018-05-07 18:36:38 +08:00
[0] ./templates/foo.js 41 bytes {0} [optional] [built]
[exports: default]
context element ./foo.js [3] ./templates lazy ^\.\/.*$ namespace object ./foo.js
context element ./foo [3] ./templates lazy ^\.\/.*$ namespace object ./foo
chunk {1} 1.output.js 41 bytes <{3}> [rendered]
> ./baz [3] ./templates lazy ^\.\/.*$ namespace object ./baz
> ./baz.js [3] ./templates lazy ^\.\/.*$ namespace object ./baz.js
2018-05-07 18:36:38 +08:00
[1] ./templates/baz.js 41 bytes {1} [optional] [built]
[exports: default]
context element ./baz.js [3] ./templates lazy ^\.\/.*$ namespace object ./baz.js
context element ./baz [3] ./templates lazy ^\.\/.*$ namespace object ./baz
chunk {2} 2.output.js 41 bytes <{3}> [rendered]
> ./bar [3] ./templates lazy ^\.\/.*$ namespace object ./bar
> ./bar.js [3] ./templates lazy ^\.\/.*$ namespace object ./bar.js
2018-05-07 18:36:38 +08:00
[2] ./templates/bar.js 41 bytes {2} [optional] [built]
[exports: default]
context element ./bar.js [3] ./templates lazy ^\.\/.*$ namespace object ./bar.js
context element ./bar [3] ./templates lazy ^\.\/.*$ namespace object ./bar
chunk {3} output.js (main) 456 bytes >{0}< >{1}< >{2}< [entry] [rendered]
> .\example.js main
2018-05-07 18:36:38 +08:00
[3] ./templates lazy ^\.\/.*$ namespace object 160 bytes {3} [optional] [built]
import() context lazy ./templates [4] ./example.js 3:23-60
[4] ./example.js 296 bytes {3} [built]
single entry .\example.js main
```