@@ -66,6 +66,8 @@ class ContextModule extends Module {
6666
6767 identifier ( ) {
6868 let identifier = this . context ;
69+ if ( this . options . resourceQuery )
70+ identifier += ` ${ this . options . resourceQuery } ` ;
6971 if ( this . options . mode )
7072 identifier += ` ${ this . options . mode } ` ;
7173 if ( ! this . options . recursive )
@@ -78,12 +80,18 @@ class ContextModule extends Module {
7880 identifier += ` include: ${ this . options . include } ` ;
7981 if ( this . options . exclude )
8082 identifier += ` exclude: ${ this . options . exclude } ` ;
83+ if ( this . options . namespaceObject === "strict" )
84+ identifier += " strict namespace object" ;
85+ else if ( this . options . namespaceObject )
86+ identifier += " namespace object" ;
8187
8288 return identifier ;
8389 }
8490
8591 readableIdentifier ( requestShortener ) {
8692 let identifier = requestShortener . shorten ( this . context ) ;
93+ if ( this . options . resourceQuery )
94+ identifier += ` ${ this . options . resourceQuery } ` ;
8795 if ( this . options . mode )
8896 identifier += ` ${ this . options . mode } ` ;
8997 if ( ! this . options . recursive )
@@ -96,6 +104,10 @@ class ContextModule extends Module {
96104 identifier += ` include: ${ this . prettyRegExp ( this . options . include + "" ) } ` ;
97105 if ( this . options . exclude )
98106 identifier += ` exclude: ${ this . prettyRegExp ( this . options . exclude + "" ) } ` ;
107+ if ( this . options . namespaceObject === "strict" )
108+ identifier += " strict namespace object" ;
109+ else if ( this . options . namespaceObject )
110+ identifier += " namespace object" ;
99111
100112 return identifier ;
101113 }
@@ -214,18 +226,60 @@ class ContextModule extends Module {
214226 } , Object . create ( null ) ) ;
215227 }
216228
229+ getFakeMap ( dependencies ) {
230+ if ( ! this . options . namespaceObject ) return false ;
231+ // if we filter first we get a new array
232+ // therefor we dont need to create a clone of dependencies explicitly
233+ // therefore the order of this is !important!
234+ let hasNonHarmony = false ;
235+ let hasHarmony = false ;
236+ const fakeMap = dependencies
237+ . filter ( dependency => dependency . module )
238+ . sort ( ( a , b ) => {
239+ return b . module . id - a . module . id ;
240+ } ) . reduce ( ( map , dep ) => {
241+ const harmonyModule = dep . module . meta && dep . module . meta . harmonyModule ;
242+ if ( ! harmonyModule ) hasNonHarmony = true ;
243+ if ( harmonyModule ) hasHarmony = true ;
244+ map [ dep . module . id ] = harmonyModule ? 0 : 1 ;
245+ return map ;
246+ } , Object . create ( null ) ) ;
247+ if ( hasHarmony && ! hasNonHarmony ) return false ;
248+ if ( ! hasHarmony && hasNonHarmony ) return true ;
249+ return fakeMap ;
250+ }
251+
252+ getModuleObjectSource ( fakeMap , fakeMapDataExpression = "fakeMap[id]" ) {
253+ const strict = this . options . namespaceObject === "strict" ;
254+ if ( strict ) {
255+ if ( fakeMap === true ) return "/* fake namespace object */ { \"default\": module }" ;
256+ if ( fakeMap ) return `${ fakeMapDataExpression } ? /* fake namespace object */ { "default": module } : module` ;
257+ } else {
258+ if ( fakeMap === true ) return "typeof module !== \"object\" || !module.__esModule ? /* fake namespace object */ { \"default\": module } : module;" ;
259+ if ( fakeMap ) return `${ fakeMapDataExpression } && (typeof module !== "object" || !module.__esModule) ? /* fake namespace object */ { "default": module } : module;` ;
260+ }
261+ return "module" ;
262+ }
263+
217264 getSyncSource ( dependencies , id ) {
218265 const map = this . getUserRequestMap ( dependencies ) ;
266+ const fakeMap = this . getFakeMap ( dependencies ) ;
267+ const returnModuleObject = `return ${ this . getModuleObjectSource ( fakeMap ) } ;` ;
268+
219269 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
270+ ${ typeof fakeMap === "object" ? `var fakeMap = ${ JSON . stringify ( fakeMap , null , "\t" ) } ;` : "" }
271+
220272function webpackContext(req) {
221- return __webpack_require__(webpackContextResolve(req));
222- };
273+ var id = webpackContextResolve(req);
274+ var module = __webpack_require__(id);
275+ ${ returnModuleObject }
276+ }
223277function webpackContextResolve(req) {
224278 var id = map[req];
225279 if(!(id + 1)) // check for number or string
226280 throw new Error("Cannot find module '" + req + "'.");
227281 return id;
228- };
282+ }
229283webpackContext.keys = function webpackContextKeys() {
230284 return Object.keys(map);
231285};
@@ -236,19 +290,25 @@ webpackContext.id = ${JSON.stringify(id)};`;
236290
237291 getWeakSyncSource ( dependencies , id ) {
238292 const map = this . getUserRequestMap ( dependencies ) ;
293+ const fakeMap = this . getFakeMap ( dependencies ) ;
294+ const returnModuleObject = `return ${ this . getModuleObjectSource ( fakeMap ) } ;` ;
295+
239296 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
297+ ${ typeof fakeMap === "object" ? `var fakeMap = ${ JSON . stringify ( fakeMap , null , "\t" ) } ;` : "" }
298+
240299function webpackContext(req) {
241300 var id = webpackContextResolve(req);
242301 if(!__webpack_require__.m[id])
243302 throw new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
244- return __webpack_require__(id);
245- };
303+ var module = __webpack_require__(id);
304+ ${ returnModuleObject }
305+ }
246306function webpackContextResolve(req) {
247307 var id = map[req];
248308 if(!(id + 1)) // check for number or string
249309 throw new Error("Cannot find module '" + req + "'.");
250310 return id;
251- };
311+ }
252312webpackContext.keys = function webpackContextKeys() {
253313 return Object.keys(map);
254314};
@@ -259,15 +319,20 @@ module.exports = webpackContext;`;
259319
260320 getAsyncWeakSource ( dependencies , id ) {
261321 const map = this . getUserRequestMap ( dependencies ) ;
322+ const fakeMap = this . getFakeMap ( dependencies ) ;
323+ const returnModuleObject = `return ${ this . getModuleObjectSource ( fakeMap ) } ;` ;
262324
263325 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
326+ ${ typeof fakeMap === "object" ? `var fakeMap = ${ JSON . stringify ( fakeMap , null , "\t" ) } ;` : "" }
327+
264328function webpackAsyncContext(req) {
265329 return webpackAsyncContextResolve(req).then(function(id) {
266330 if(!__webpack_require__.m[id])
267331 throw new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
268- return __webpack_require__(id);
332+ var module = __webpack_require__(id);
333+ ${ returnModuleObject }
269334 });
270- };
335+ }
271336function webpackAsyncContextResolve(req) {
272337 // Here Promise.resolve().then() is used instead of new Promise() to prevent
273338 // uncatched exception popping up in devtools
@@ -277,7 +342,7 @@ function webpackAsyncContextResolve(req) {
277342 throw new Error("Cannot find module '" + req + "'.");
278343 return id;
279344 });
280- };
345+ }
281346webpackAsyncContext.keys = function webpackAsyncContextKeys() {
282347 return Object.keys(map);
283348};
@@ -288,10 +353,19 @@ module.exports = webpackAsyncContext;`;
288353
289354 getEagerSource ( dependencies , id ) {
290355 const map = this . getUserRequestMap ( dependencies ) ;
356+ const fakeMap = this . getFakeMap ( dependencies ) ;
357+ const thenFunction = fakeMap ?
358+ `function(id) {
359+ var module = __webpack_require__(id);
360+ return ${ this . getModuleObjectSource ( fakeMap ) } ;
361+ }` :
362+ "__webpack_require__" ;
291363 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
364+ ${ typeof fakeMap === "object" ? `var fakeMap = ${ JSON . stringify ( fakeMap , null , "\t" ) } ;` : "" }
365+
292366function webpackAsyncContext(req) {
293- return webpackAsyncContextResolve(req).then(__webpack_require__ );
294- };
367+ return webpackAsyncContextResolve(req).then(${ thenFunction } );
368+ }
295369function webpackAsyncContextResolve(req) {
296370 // Here Promise.resolve().then() is used instead of new Promise() to prevent
297371 // uncatched exception popping up in devtools
@@ -301,7 +375,7 @@ function webpackAsyncContextResolve(req) {
301375 throw new Error("Cannot find module '" + req + "'.");
302376 return id;
303377 });
304- };
378+ }
305379webpackAsyncContext.keys = function webpackAsyncContextKeys() {
306380 return Object.keys(map);
307381};
@@ -313,18 +387,28 @@ module.exports = webpackAsyncContext;`;
313387 getLazyOnceSource ( block , dependencies , id , outputOptions , requestShortener ) {
314388 const promise = DepBlockHelpers . getDepBlockPromise ( block , outputOptions , requestShortener , "lazy-once context" ) ;
315389 const map = this . getUserRequestMap ( dependencies ) ;
390+ const fakeMap = this . getFakeMap ( dependencies ) ;
391+ const thenFunction = fakeMap ?
392+ `function(id) {
393+ var module = __webpack_require__(id);
394+ return ${ this . getModuleObjectSource ( fakeMap ) } ;
395+ }` :
396+ "__webpack_require__" ;
397+
316398 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
399+ ${ typeof fakeMap === "object" ? `var fakeMap = ${ JSON . stringify ( fakeMap , null , "\t" ) } ;` : "" }
400+
317401function webpackAsyncContext(req) {
318- return webpackAsyncContextResolve(req).then(__webpack_require__ );
319- };
402+ return webpackAsyncContextResolve(req).then(${ thenFunction } );
403+ }
320404function webpackAsyncContextResolve(req) {
321405 return ${ promise } .then(function() {
322406 var id = map[req];
323407 if(!(id + 1)) // check for number or string
324408 throw new Error("Cannot find module '" + req + "'.");
325409 return id;
326410 });
327- };
411+ }
328412webpackAsyncContext.keys = function webpackAsyncContextKeys() {
329413 return Object.keys(map);
330414};
@@ -335,6 +419,7 @@ module.exports = webpackAsyncContext;`;
335419
336420 getLazySource ( blocks , id ) {
337421 let hasMultipleOrNoChunks = false ;
422+ const fakeMap = this . getFakeMap ( blocks . map ( b => b . dependencies [ 0 ] ) ) ;
338423 const map = blocks
339424 . filter ( block => block . dependencies [ 0 ] . module )
340425 . map ( ( block ) => ( {
@@ -349,25 +434,31 @@ module.exports = webpackAsyncContext;`;
349434 if ( chunks . length !== 1 ) {
350435 hasMultipleOrNoChunks = true ;
351436 }
352- map [ item . userRequest ] = [ item . dependency . module . id ]
437+ const arrayStart = [ item . dependency . module . id ] ;
438+ if ( typeof fakeMap === "object" )
439+ arrayStart . push ( fakeMap [ item . dependency . module . id ] ) ;
440+ map [ item . userRequest ] = arrayStart
353441 . concat ( chunks . map ( chunk => chunk . id ) ) ;
354442
355443 return map ;
356444 } , Object . create ( null ) ) ;
357445
446+ const chunksStartPosition = typeof fakeMap === "object" ? 2 : 1 ;
358447 const requestPrefix = hasMultipleOrNoChunks ?
359- "Promise.all(ids.slice(1).map(__webpack_require__.e))" :
360- "__webpack_require__.e(ids[1])" ;
448+ `Promise.all(ids.slice(${ chunksStartPosition } ).map(__webpack_require__.e))` :
449+ `__webpack_require__.e(ids[${ chunksStartPosition } ])` ;
450+ const returnModuleObject = `return ${ this . getModuleObjectSource ( fakeMap , "ids[1]" ) } ;` ;
361451
362452 return `var map = ${ JSON . stringify ( map , null , "\t" ) } ;
363453function webpackAsyncContext(req) {
364454 var ids = map[req];
365455 if(!ids)
366- return Promise.reject( new Error("Cannot find module '" + req + "'."));
456+ return Promise.resolve().then(function() { throw new Error("Cannot find module '" + req + "'."); } );
367457 return ${ requestPrefix } .then(function() {
368- return __webpack_require__(ids[0]);
458+ var module = __webpack_require__(ids[0]);
459+ ${ returnModuleObject }
369460 });
370- };
461+ }
371462webpackAsyncContext.keys = function webpackAsyncContextKeys() {
372463 return Object.keys(map);
373464};
0 commit comments