Skip to content

Commit 342d655

Browse files
authored
Merge pull request webpack#5809 from ljcrapo/import-context-filter
Add include and exclude options for dynamic import statements
2 parents 5b1bf29 + 7874aa1 commit 342d655

47 files changed

Lines changed: 747 additions & 262 deletions

Some content is hidden

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

0 commit comments

Comments
 (0)