Skip to content

Commit f884136

Browse files
committed
created filter logic for dynamic import statements
1 parent 33818bc commit f884136

22 files changed

Lines changed: 521 additions & 4 deletions

File tree

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
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: d7134f7aa757ec509958
298+
Version: webpack 3.6.0
299+
Time: 74ms
300+
Asset Size Chunks  Chunk Names
301+
0.output.js 444 bytes 0 [emitted]
302+
1.output.js 450 bytes 1 [emitted]
303+
2.output.js 441 bytes 2 [emitted]
304+
output.js 7.25 kB 3 [emitted] main
305+
Entrypoint main = output.js
306+
chunk {0} 0.output.js 41 bytes {3} [rendered]
307+
[2] ./templates/foo.js 41 bytes {0} [optional] [built]
308+
[exports: default]
309+
context element ./foo.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./foo.js
310+
context element ./foo [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./foo
311+
chunk {1} 1.output.js 41 bytes {3} [rendered]
312+
[1] ./templates/baz.js 41 bytes {1} [optional] [built]
313+
[exports: default]
314+
context element ./baz.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./baz.js
315+
context element ./baz [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./baz
316+
chunk {2} 2.output.js 41 bytes {3} [rendered]
317+
[0] ./templates/bar.js 41 bytes {2} [optional] [built]
318+
[exports: default]
319+
context element ./bar.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./bar.js
320+
context element ./bar [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./bar
321+
chunk {3} output.js (main) 611 bytes [entry] [rendered]
322+
> main [3] ./example.js
323+
[3] ./example.js 451 bytes {3} [built]
324+
[4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ 160 bytes {3} [optional] [built]
325+
import() context lazy ./templates [3] ./example.js 3:23-6:3
326+
```
327+
328+
## Minimized (uglify-js, no zip)
329+
330+
```
331+
Hash: d7134f7aa757ec509958
332+
Version: webpack 3.6.0
333+
Time: 115ms
334+
Asset Size Chunks  Chunk Names
335+
0.output.js 117 bytes 0 [emitted]
336+
1.output.js 116 bytes 1 [emitted]
337+
2.output.js 115 bytes 2 [emitted]
338+
output.js 6.83 kB 3 [emitted] main
339+
Entrypoint main = output.js
340+
chunk {0} 0.output.js 41 bytes {3} [rendered]
341+
[2] ./templates/foo.js 41 bytes {0} [optional] [built]
342+
[exports: default]
343+
context element ./foo.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./foo.js
344+
context element ./foo [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./foo
345+
chunk {1} 1.output.js 41 bytes {3} [rendered]
346+
[1] ./templates/baz.js 41 bytes {1} [optional] [built]
347+
[exports: default]
348+
context element ./baz.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./baz.js
349+
context element ./baz [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./baz
350+
chunk {2} 2.output.js 41 bytes {3} [rendered]
351+
[0] ./templates/bar.js 41 bytes {2} [optional] [built]
352+
[exports: default]
353+
context element ./bar.js [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./bar.js
354+
context element ./bar [4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ ./bar
355+
chunk {3} output.js (main) 611 bytes [entry] [rendered]
356+
> main [3] ./example.js
357+
[3] ./example.js 451 bytes {3} [built]
358+
[4] ./templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ 160 bytes {3} [optional] [built]
359+
import() context lazy ./templates [3] ./example.js 3:23-6:3
360+
361+
ERROR in output.js from UglifyJs
362+
Unexpected token: keyword (function) [output.js:155,6]
363+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../build-common");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
async function getTemplate(templateName) {
2+
try {
3+
let template = await import(
4+
/* webpackFilter: "^(?:[^.]*(?:\\.(?!noimport(?:\\.js)?$))?)*$" */
5+
`./templates/${templateName}`
6+
);
7+
console.log(template);
8+
} catch(err) {
9+
console.error(err);
10+
return new Error(err);
11+
}
12+
}
13+
14+
getTemplate("foo");
15+
getTemplate("bar");
16+
getTemplate("baz");
17+
getTemplate("foo.noimport");
18+
getTemplate("bar.noimport");
19+
getTemplate("baz.noimport");
20+

0 commit comments

Comments
 (0)