Skip to content

Commit 1c538f9

Browse files
committed
fixes webpack#5000
1 parent 3d65329 commit 1c538f9

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ function getSymbolsFromScope(s, untilScope) {
6262
return allUsedNames;
6363
}
6464

65+
function getAllReferences(variable) {
66+
let set = variable.references;
67+
// Look for inner scope variables too (like in class Foo { t() { Foo } })
68+
const identifiers = new Set(variable.identifiers);
69+
for(const scope of variable.scope.childScopes) {
70+
for(const innerVar of scope.variables) {
71+
if(innerVar.identifiers.some(id => identifiers.has(id))) {
72+
set = set.concat(innerVar.references);
73+
break;
74+
}
75+
}
76+
}
77+
return set;
78+
}
79+
6580
function reduceSet(a, b) {
6681
for(const item of b)
6782
a.add(item);
@@ -242,7 +257,7 @@ class ConcatenatedModule extends Module {
242257
});
243258
const globalScope = scopeManager.acquire(ast);
244259
const moduleScope = globalScope.childScopes[0];
245-
const resultSource = new ReplaceSource(m.source(innerDependencyTemplates, outputOptions, requestShortener));
260+
const resultSource = new ReplaceSource(source);
246261
info.source = resultSource;
247262
info.globalScope = globalScope;
248263
info.moduleScope = moduleScope;
@@ -272,12 +287,13 @@ class ConcatenatedModule extends Module {
272287
info.moduleScope.variables.forEach(variable => {
273288
const name = variable.name;
274289
if(allUsedNames.has(name)) {
275-
const symbolsInReferences = variable.references.map(ref => getSymbolsFromScope(ref.from, info.moduleScope)).reduce(reduceSet, new Set());
290+
const references = getAllReferences(variable);
291+
const symbolsInReferences = references.map(ref => getSymbolsFromScope(ref.from, info.moduleScope)).reduce(reduceSet, new Set());
276292
const newName = this.findNewName(name, allUsedNames, symbolsInReferences, info.module.readableIdentifier(requestShortener));
277293
allUsedNames.add(newName);
278294
info.internalNames.set(name, newName);
279295
const source = info.source;
280-
const allIdentifiers = new Set(variable.references.map(r => r.identifier).concat(variable.identifiers));
296+
const allIdentifiers = new Set(references.map(r => r.identifier).concat(variable.identifiers));
281297
for(const identifier of allIdentifiers) {
282298
const r = identifier.range;
283299
source.replace(r[0], r[1] - 1, newName);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class Foo {
2+
test() {
3+
return Foo.value;
4+
}
5+
}
6+
Foo.value = 1;
7+
8+
export class Bar {
9+
test() {
10+
return Bar.value;
11+
}
12+
}
13+
Bar.value = 1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Foo, Bar as FirstBar } from "./first"
2+
import { Foo as SecondFoo, Bar } from "./second"
3+
4+
it("should renamed class reference in inner scope", function() {
5+
var a = new Foo().test();
6+
var b = new SecondFoo().test();
7+
a.should.be.eql(1);
8+
b.should.be.eql(2);
9+
new FirstBar().test().should.be.eql(1);
10+
new Bar().test().should.be.eql(2);
11+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class Foo {
2+
test() {
3+
return Foo.value;
4+
}
5+
}
6+
Foo.value = 2;
7+
8+
export class Bar {
9+
test() {
10+
return Bar.value;
11+
}
12+
}
13+
Bar.value = 2;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var supportsES6 = require("../../../helpers/supportsES6");
2+
3+
module.exports = function(config) {
4+
return !config.minimize && supportsES6();
5+
};

0 commit comments

Comments
 (0)