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