Skip to content

Commit 9b58b7a

Browse files
committed
generate correct namespace objects
fixes webpack#5020
1 parent 9b8c40b commit 9b58b7a

File tree

16 files changed

+119
-9
lines changed

16 files changed

+119
-9
lines changed

lib/optimize/ConcatenatedModule.js

Lines changed: 21 additions & 9 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,26 @@ 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
}
337+
let generated;
338+
do {
339+
generated = false;
340+
modulesWithInfo.forEach(info => {
341+
if(info.needNamespaceObject && !info.namespaceObjectSource) {
342+
const name = info.exportMap.get(true);
343+
const nsObj = [`var ${name} = {};`];
344+
for(const exportName of info.module.providedExports) {
345+
const finalName = getFinalName(info, exportName, moduleToInfoMap, requestShortener);
346+
nsObj.push(`__webpack_require__.d(${name}, ${JSON.stringify(exportName)}, function() { return ${finalName}; });`);
347+
}
348+
info.namespaceObjectSource = nsObj.join("\n") + "\n";
349+
generated = true;
350+
}
351+
});
352+
} while(generated);
335353
modulesWithInfo.forEach(info => {
336354
result.add(`\n// CONCATENATED MODULE: ${info.module.readableIdentifier(requestShortener)}\n`);
337-
if(info.needNamespaceObject) {
338-
const name = info.exportMap.get(true);
339-
const nsObj = [`var ${name} = {};`];
340-
for(const exportName of info.module.providedExports) {
341-
const finalName = getFinalName(info, exportName, moduleToInfoMap, requestShortener);
342-
nsObj.push(`__webpack_require__.d(${name}, ${JSON.stringify(exportName)}, function() { return ${finalName}; });`);
343-
}
344-
result.add(nsObj.join("\n") + "\n");
355+
if(info.namespaceObjectSource) {
356+
result.add(info.namespaceObjectSource);
345357
}
346358
result.add(info.source);
347359
});
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+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const svg1 = 1;

0 commit comments

Comments
 (0)