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