@@ -140,8 +140,13 @@ class ContextModule extends Module {
140140 this . addBlock ( block ) ;
141141 }
142142
143- } else {
143+ } else if ( this . async === "weak" || this . async === "async-weak" ) {
144+
145+ // we mark all dependencies as weak
146+ dependencies . forEach ( dep => dep . weak = true ) ;
147+ this . dependencies = dependencies ;
144148
149+ } else {
145150 // if we are lazy create a new async dependency block per dependency
146151 // and add all blocks to this context
147152 dependencies . forEach ( ( dep , idx ) => {
@@ -198,27 +203,80 @@ module.exports = webpackContext;
198203webpackContext.id = ${ JSON . stringify ( id ) } ;` ;
199204 }
200205
206+ getWeakSyncSource ( dependencies , id ) {
207+ const map = this . getUserRequestMap ( dependencies ) ;
208+ return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
209+ function webpackContext(req) {
210+ var id = webpackContextResolve(req);
211+ if(!__webpack_require__.m[id])
212+ throw new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
213+ return __webpack_require__(id);
214+ };
215+ function webpackContextResolve(req) {
216+ var id = map[req];
217+ if(!(id + 1)) // check for number or string
218+ throw new Error("Cannot find module '" + req + "'.");
219+ return id;
220+ };
221+ webpackContext.keys = function webpackContextKeys() {
222+ return Object.keys(map);
223+ };
224+ webpackContext.resolve = webpackContextResolve;
225+ webpackContext.id = ${ JSON . stringify ( id ) } ;
226+ module.exports = webpackContext;` ;
227+ }
228+
229+ getAsyncWeakSource ( dependencies , id ) {
230+ const map = this . getUserRequestMap ( dependencies ) ;
231+
232+ return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
233+ function webpackAsyncContext(req) {
234+ return webpackAsyncContextResolve(req).then(function(id) {
235+ if(!__webpack_require__.m[id])
236+ throw new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
237+ return __webpack_require__(id);
238+ });
239+ };
240+ function webpackAsyncContextResolve(req) {
241+ // Here Promise.resolve().then() is used instead of new Promise() to prevent
242+ // uncatched exception popping up in devtools
243+ return Promise.resolve().then(function() {
244+ var id = map[req];
245+ if(!(id + 1)) // check for number or string
246+ throw new Error("Cannot find module '" + req + "'.");
247+ return id;
248+ });
249+ };
250+ webpackAsyncContext.keys = function webpackAsyncContextKeys() {
251+ return Object.keys(map);
252+ };
253+ webpackAsyncContext.resolve = webpackAsyncContextResolve;
254+ webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;
255+ module.exports = webpackAsyncContext;` ;
256+ }
257+
201258 getEagerSource ( dependencies , id ) {
202259 const map = this . getUserRequestMap ( dependencies ) ;
203260 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
204261function webpackAsyncContext(req) {
205262 return webpackAsyncContextResolve(req).then(__webpack_require__);
206263};
207264function webpackAsyncContextResolve(req) {
208- return new Promise(function(resolve, reject) {
265+ // Here Promise.resolve().then() is used instead of new Promise() to prevent
266+ // uncatched exception popping up in devtools
267+ return Promise.resolve().then(function() {
209268 var id = map[req];
210269 if(!(id + 1)) // check for number or string
211- reject(new Error("Cannot find module '" + req + "'."));
212- else
213- resolve(id);
270+ throw new Error("Cannot find module '" + req + "'.");
271+ return id;
214272 });
215273};
216274webpackAsyncContext.keys = function webpackAsyncContextKeys() {
217275 return Object.keys(map);
218276};
219277webpackAsyncContext.resolve = webpackAsyncContextResolve;
220- module.exports = webpackAsyncContext ;
221- webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;`;
278+ webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;
279+ module.exports = webpackAsyncContext ;`;
222280 }
223281
224282 getLazyOnceSource ( block , dependencies , id , outputOptions , requestShortener ) {
@@ -240,8 +298,8 @@ webpackAsyncContext.keys = function webpackAsyncContextKeys() {
240298 return Object.keys(map);
241299};
242300webpackAsyncContext.resolve = webpackAsyncContextResolve;
243- module.exports = webpackAsyncContext ;
244- webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;`;
301+ webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;
302+ module.exports = webpackAsyncContext ;`;
245303 }
246304
247305 getLazySource ( blocks , id ) {
@@ -282,8 +340,8 @@ function webpackAsyncContext(req) {
282340webpackAsyncContext.keys = function webpackAsyncContextKeys() {
283341 return Object.keys(map);
284342};
285- module.exports = webpackAsyncContext ;
286- webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;`;
343+ webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;
344+ module.exports = webpackAsyncContext ;`;
287345 }
288346
289347 getSourceForEmptyContext ( id ) {
@@ -298,7 +356,11 @@ webpackEmptyContext.id = ${JSON.stringify(id)};`;
298356
299357 getSourceForEmptyAsyncContext ( id ) {
300358 return `function webpackEmptyAsyncContext(req) {
301- return new Promise(function(resolve, reject) { reject(new Error("Cannot find module '" + req + "'.")); });
359+ // Here Promise.resolve().then() is used instead of new Promise() to prevent
360+ // uncatched exception popping up in devtools
361+ return Promise.resolve().then(function() {
362+ throw new Error("Cannot find module '" + req + "'.");
363+ });
302364}
303365webpackEmptyAsyncContext.keys = function() { return []; };
304366webpackEmptyAsyncContext.resolve = webpackEmptyAsyncContext;
@@ -318,13 +380,25 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
318380 return this . getEagerSource ( this . dependencies , this . id ) ;
319381 }
320382 return this . getSourceForEmptyAsyncContext ( this . id ) ;
321- } else if ( asyncMode === "lazy-once" ) {
383+ }
384+ if ( asyncMode === "lazy-once" ) {
322385 const block = this . blocks [ 0 ] ;
323386 if ( block ) {
324387 return this . getLazyOnceSource ( block , block . dependencies , this . id , outputOptions , requestShortener ) ;
325388 }
326389 return this . getSourceForEmptyAsyncContext ( this . id ) ;
327390 }
391+ if ( asyncMode === "async-weak" ) {
392+ if ( this . dependencies && this . dependencies . length > 0 ) {
393+ return this . getAsyncWeakSource ( this . dependencies , this . id ) ;
394+ }
395+ return this . getSourceForEmptyAsyncContext ( this . id ) ;
396+ }
397+ if ( asyncMode === "weak" ) {
398+ if ( this . dependencies && this . dependencies . length > 0 ) {
399+ return this . getWeakSyncSource ( this . dependencies , this . id ) ;
400+ }
401+ }
328402 if ( this . dependencies && this . dependencies . length > 0 ) {
329403 return this . getSyncSource ( this . dependencies , this . id ) ;
330404 }
0 commit comments