Skip to content

Commit ae5cb84

Browse files
authored
Merge pull request webpack#5024 from webpack/bugfix/scope-hoisting-ns-object
Bugfix scope hoisting ns object
2 parents 9b8c40b + 8c78089 commit ae5cb84

17 files changed

Lines changed: 150 additions & 12 deletions

File tree

‎examples/scope-hoisting/README.md‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ var a = "a";
315315

316316
// CONCATENATED MODULE: ./node_modules/b.js
317317
// module b
318-
function b_js_a() {
318+
function b_a() {
319319
return "b";
320320
};
321321

@@ -324,7 +324,7 @@ function b_js_a() {
324324

325325

326326
__webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, /*! ./lazy */ 3)).then(function(lazy) {
327-
console.log(a, b_js_a(), __WEBPACK_IMPORTED_MODULE_0_shared__["a"], __WEBPACK_IMPORTED_MODULE_0_shared__["b"], lazy.c, lazy.d.a, lazy.x, lazy.y);
327+
console.log(a, b_a(), __WEBPACK_IMPORTED_MODULE_0_shared__["a"], __WEBPACK_IMPORTED_MODULE_0_shared__["b"], lazy.c, lazy.d.a, lazy.x, lazy.y);
328328
});
329329

330330

@@ -375,16 +375,16 @@ var c = String.fromCharCode(__WEBPACK_IMPORTED_MODULE_0_cjs__["c"].charCodeAt(0)
375375

376376

377377
// CONCATENATED MODULE: ./node_modules/d.js
378-
var d_js_namespaceObject = {};
379-
__webpack_require__.d(d_js_namespaceObject, "a", function() { return a; });
378+
var d_namespaceObject = {};
379+
__webpack_require__.d(d_namespaceObject, "a", function() { return a; });
380380
// module d
381381
var a = "d";
382382

383383
// CONCATENATED MODULE: ./lazy.js
384384
/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "c", function() { return c; });
385385
/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "x", function() { return __WEBPACK_IMPORTED_MODULE_1_shared__["a"]; });
386386
/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "y", function() { return __WEBPACK_IMPORTED_MODULE_1_shared__["b"]; });
387-
/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "d", function() { return d_js_namespaceObject; });
387+
/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "d", function() { return d_namespaceObject; });
388388

389389

390390

@@ -408,8 +408,8 @@ webpackJsonp([0],[,,function(n,r){r.c="e"},function(n,r,t){"use strict";Object.d
408408
Hash: 26f89d6006fb6c5a1fa1
409409
Version: webpack 3.0.0-rc.0
410410
Asset Size Chunks Chunk Names
411-
0.output.js 1.96 kB 0 [emitted]
412-
output.js 7.46 kB 1 [emitted] main
411+
0.output.js 1.95 kB 0 [emitted]
412+
output.js 7.45 kB 1 [emitted] main
413413
Entrypoint main = output.js
414414
chunk {0} 0.output.js 276 bytes {1} [rendered]
415415
> [] 4:0-16
@@ -427,6 +427,18 @@ chunk {1} output.js (main) 385 bytes [entry] [rendered]
427427
harmony import shared [1] ./example.js + 2 modules 3:0-23
428428
harmony import shared [3] ./lazy.js + 2 modules 6:0-30
429429
[1] ./example.js + 2 modules 280 bytes {1} [built]
430+
[0] ./node_modules/shared.js + 1 modules 105 bytes {1} [built]
431+
[exports: x, y]
432+
[only some exports used: x, y]
433+
harmony import shared [1] ./example.js + 2 modules 3:0-23
434+
harmony import shared [3] ./lazy.js + 2 modules 6:0-30
435+
[1] ./example.js + 2 modules 280 bytes {1} [built]
436+
[2] ./node_modules/cjs.js 44 bytes {0} [built]
437+
[only some exports used: c]
438+
harmony import cjs [3] ./lazy.js + 2 modules 2:0-29
439+
[3] ./lazy.js + 2 modules 232 bytes {0} [built]
440+
[exports: d, c, x, y]
441+
import() ./lazy [] ./example.js 4:0-16
430442
```
431443

432444
## Minimized (uglify-js, no zip)
@@ -454,4 +466,16 @@ chunk {1} output.js (main) 385 bytes [entry] [rendered]
454466
harmony import shared [1] ./example.js + 2 modules 3:0-23
455467
harmony import shared [3] ./lazy.js + 2 modules 6:0-30
456468
[1] ./example.js + 2 modules 280 bytes {1} [built]
469+
[0] ./node_modules/shared.js + 1 modules 105 bytes {1} [built]
470+
[exports: x, y]
471+
[only some exports used: x, y]
472+
harmony import shared [1] ./example.js + 2 modules 3:0-23
473+
harmony import shared [3] ./lazy.js + 2 modules 6:0-30
474+
[1] ./example.js + 2 modules 280 bytes {1} [built]
475+
[2] ./node_modules/cjs.js 44 bytes {0} [built]
476+
[only some exports used: c]
477+
harmony import cjs [3] ./lazy.js + 2 modules 2:0-29
478+
[3] ./lazy.js + 2 modules 232 bytes {0} [built]
479+
[exports: d, c, x, y]
480+
import() ./lazy [] ./example.js 4:0-16
457481
```

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class ConcatenatedModule extends Module {
197197
exportMap: exportMap,
198198
reexportMap: reexportMap,
199199
needCompatibilityFlag: false,
200-
needNamespaceObject: false
200+
needNamespaceObject: false,
201+
namespaceObjectSource: null
201202
};
202203
});
203204

@@ -282,6 +283,7 @@ class ConcatenatedModule extends Module {
282283

283284
modulesWithInfo.forEach(info => {
284285
const namespaceObjectName = this.findNewName("namespaceObject", allUsedNames, null, info.module.readableIdentifier(requestShortener));
286+
allUsedNames.add(namespaceObjectName);
285287
info.internalNames.set(namespaceObjectName, namespaceObjectName);
286288
info.exportMap.set(true, namespaceObjectName);
287289
info.moduleScope.variables.forEach(variable => {
@@ -332,16 +334,27 @@ class ConcatenatedModule extends Module {
332334
if(moduleToInfoMap.get(this.rootModule).needCompatibilityFlag) {
333335
result.add(`Object.defineProperty(${this.rootModule.exportsArgument || "exports"}, "__esModule", { value: true });\n`);
334336
}
335-
modulesWithInfo.forEach(info => {
336-
result.add(`\n// CONCATENATED MODULE: ${info.module.readableIdentifier(requestShortener)}\n`);
337-
if(info.needNamespaceObject) {
337+
let generated = true;
338+
const ensureNsObjSource = info => {
339+
if(info.needNamespaceObject && !info.namespaceObjectSource) {
338340
const name = info.exportMap.get(true);
339341
const nsObj = [`var ${name} = {};`];
340342
for(const exportName of info.module.providedExports) {
341343
const finalName = getFinalName(info, exportName, moduleToInfoMap, requestShortener);
342344
nsObj.push(`__webpack_require__.d(${name}, ${JSON.stringify(exportName)}, function() { return ${finalName}; });`);
343345
}
344-
result.add(nsObj.join("\n") + "\n");
346+
info.namespaceObjectSource = nsObj.join("\n") + "\n";
347+
generated = true;
348+
}
349+
};
350+
while(generated) {
351+
generated = false;
352+
modulesWithInfo.forEach(ensureNsObjSource);
353+
}
354+
modulesWithInfo.forEach(info => {
355+
result.add(`\n// CONCATENATED MODULE: ${info.module.readableIdentifier(requestShortener)}\n`);
356+
if(info.namespaceObjectSource) {
357+
result.add(info.namespaceObjectSource);
345358
}
346359
result.add(info.source);
347360
});
@@ -355,6 +368,9 @@ class ConcatenatedModule extends Module {
355368
if(name === "__WEBPACK_MODULE_DEFAULT_EXPORT__")
356369
name = "defaultExport";
357370

371+
// Remove uncool stuff
372+
extraInfo = extraInfo.replace(/(\/index)?\.([a-zA-Z0-9]{1,4})$/, "");
373+
358374
const splittedInfo = extraInfo.split("/");
359375
while(splittedInfo.length) {
360376
name = splittedInfo.pop() + "_" + name;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var testData = require("./src/index.js");
2+
3+
it("should export the correct values", function() {
4+
testData.should.be.eql({
5+
icon: {
6+
svg: {
7+
default: 1
8+
}
9+
}
10+
});
11+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as svg from "./svg";
2+
3+
export {
4+
svg
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const svg1 = 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as icon from "./icon";
2+
3+
export {
4+
icon
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var testData = require("./src/index.js");
2+
3+
it("should export the correct values", function() {
4+
testData.should.be.eql({
5+
svg5: {
6+
svg: {
7+
clinical1: {
8+
svg1: 1
9+
},
10+
clinical2: {
11+
svg2: 2
12+
}
13+
}
14+
},
15+
svg6: {
16+
svg: {
17+
test: {
18+
svg1: 10
19+
},
20+
clinical2: {
21+
svg2: 20
22+
}
23+
}
24+
}
25+
});
26+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as svg from "./svg";
2+
3+
export {
4+
svg
5+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { svg1 } from "./svg1";
2+
import { svg2 } from "./svg2";
3+
4+
const clinical1 = {
5+
svg1
6+
};
7+
const clinical2 = {
8+
svg2
9+
};
10+
11+
export {
12+
clinical1,
13+
clinical2
14+
};

0 commit comments

Comments
 (0)