@@ -140,15 +140,14 @@ class ContextModule extends Module {
140140 this . addBlock ( block ) ;
141141 }
142142
143- } else if ( this . async === "weak-context" ) {
144-
145- // store the dependences in a different key than `this.dependences`
146- // to prevent them from being bundled in the parent
147- this . weakDependencies = dependencies ;
143+ } else if ( this . async === "weak" ) {
144+ this . dependencies = dependencies ;
148145
149- // if we are lazy create a new async dependency block per dependency
150- // and add all blocks to this context
146+ // if we are weak create a new async dependency block per dependency
147+ // and add all blocks to this context, but mark each dep as weak, to
148+ // prevent them from being bundled in the parent
151149 dependencies . forEach ( ( dep , idx ) => {
150+ dep . weak = true ;
152151 let chunkName = this . chunkName ;
153152
154153 if ( chunkName ) {
@@ -161,10 +160,8 @@ class ContextModule extends Module {
161160 block . addDependency ( dep ) ;
162161 this . addBlock ( block ) ;
163162 } ) ;
164-
165163 } else {
166-
167- // if we are lazy create a new async dependency block per dependency
164+ // if we are lazy or eager-weak create a new async dependency block per dependency
168165 // and add all blocks to this context
169166 dependencies . forEach ( ( dep , idx ) => {
170167 let chunkName = this . chunkName ;
@@ -220,6 +217,68 @@ module.exports = webpackContext;
220217webpackContext.id = ${ JSON . stringify ( id ) } ;` ;
221218 }
222219
220+ getWeakSyncSource ( dependencies , id ) {
221+ const map = this . getUserRequestMap ( dependencies ) ;
222+ return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
223+ function webpackContext(req) {
224+ var id = webpackContextResolve(req);
225+ if(!__webpack_require__.m[id])
226+ throw new Error("Module '" + id + "' is not available (weak dependency)");
227+ return __webpack_require__(id);
228+ };
229+ function webpackContextResolve(req) {
230+ var id = map[req];
231+ if(!(id + 1)) // check for number or string
232+ throw new Error("Cannot find module '" + req + "'.");
233+ return id;
234+ };
235+ webpackContext.keys = function webpackContextKeys() {
236+ return Object.keys(map);
237+ };
238+ webpackContext.resolve = webpackContextResolve;
239+ module.exports = webpackContext;
240+ webpackContext.id = ${ JSON . stringify ( id ) } ;` ;
241+ }
242+
243+ getWeakEagerSource ( blocks , id ) {
244+ const map = blocks
245+ . filter ( block => block . dependencies [ 0 ] . module )
246+ . map ( ( block ) => ( {
247+ dependency : block . dependencies [ 0 ] ,
248+ block : block ,
249+ userRequest : block . dependencies [ 0 ] . userRequest
250+ } ) ) . sort ( ( a , b ) => {
251+ if ( a . userRequest === b . userRequest ) return 0 ;
252+ return a . userRequest < b . userRequest ? - 1 : 1 ;
253+ } ) . reduce ( ( map , item ) => {
254+ const chunks = item . block . chunks || [ ] ;
255+
256+ map [ item . userRequest ] = [ item . dependency . module . id ]
257+ . concat ( chunks . map ( chunk => chunk . id ) ) ;
258+
259+ return map ;
260+ } , Object . create ( null ) ) ;
261+
262+ return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
263+ function webpackAsyncContext(req) {
264+ var ids = map[req];
265+ if(!ids)
266+ return Promise.reject(new Error("Cannot find module '" + req + "'."));
267+ return new Promise(function(resolve, reject) {
268+ var id = ids[0];
269+ if(!__webpack_require__.m[id])
270+ reject(new Error("Module '" + id + "' is not available (weak dependency)"));
271+ else
272+ resolve(__webpack_require__(id));
273+ });
274+ };
275+ webpackAsyncContext.keys = function webpackAsyncContextKeys() {
276+ return Object.keys(map);
277+ };
278+ module.exports = webpackAsyncContext;
279+ webpackAsyncContext.id = ${ JSON . stringify ( id ) } ;` ;
280+ }
281+
223282 getEagerSource ( dependencies , id ) {
224283 const map = this . getUserRequestMap ( dependencies ) ;
225284 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
@@ -329,9 +388,6 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
329388 }
330389
331390 getSourceString ( asyncMode , outputOptions , requestShortener ) {
332- if ( asyncMode === "weak-context" ) {
333- return this . getSyncSource ( this . weakDependencies , this . id ) ;
334- }
335391 if ( asyncMode === "lazy" ) {
336392 if ( this . blocks && this . blocks . length > 0 ) {
337393 return this . getLazySource ( this . blocks , this . id ) ;
@@ -343,13 +399,25 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
343399 return this . getEagerSource ( this . dependencies , this . id ) ;
344400 }
345401 return this . getSourceForEmptyAsyncContext ( this . id ) ;
346- } else if ( asyncMode === "lazy-once" ) {
402+ }
403+ if ( asyncMode === "lazy-once" ) {
347404 const block = this . blocks [ 0 ] ;
348405 if ( block ) {
349406 return this . getLazyOnceSource ( block , block . dependencies , this . id , outputOptions , requestShortener ) ;
350407 }
351408 return this . getSourceForEmptyAsyncContext ( this . id ) ;
352409 }
410+ if ( asyncMode === "eager-weak" ) {
411+ if ( this . blocks && this . blocks . length > 0 ) {
412+ return this . getWeakEagerSource ( this . blocks , this . id ) ;
413+ }
414+ return this . getSourceForEmptyAsyncContext ( this . id ) ;
415+ }
416+ if ( asyncMode === "weak" ) {
417+ if ( this . dependencies && this . dependencies . length > 0 ) {
418+ return this . getWeakSyncSource ( this . dependencies , this . id ) ;
419+ }
420+ }
353421 if ( this . dependencies && this . dependencies . length > 0 ) {
354422 return this . getSyncSource ( this . dependencies , this . id ) ;
355423 }
0 commit comments