Skip to content

Commit d7f553c

Browse files
committed
fixes tree shaking for export *
fixes webpack#2850 fixes webpack#2847
1 parent cca2cee commit d7f553c

10 files changed

Lines changed: 58 additions & 6 deletions

File tree

lib/FunctionModuleTemplatePlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ FunctionModuleTemplatePlugin.prototype.apply = function(moduleTemplate) {
2525
if(this.outputOptions.pathinfo) {
2626
var source = new ConcatSource();
2727
var req = module.readableIdentifier(this.requestShortener);
28+
if(Array.isArray(module.usedExports))
29+
source.add("/* exports used: " + module.usedExports.join(", ") + " */\n");
30+
else if(module.usedExports)
31+
source.add("/* all exports used */\n");
2832
source.add("/*!****" + req.replace(/./g, "*") + "****!*\\\n");
2933
source.add(" !*** " + req.replace(/\*\//g, "*_/") + " ***!\n");
3034
source.add(" \\****" + req.replace(/./g, "*") + "****/\n");

lib/dependencies/HarmonyExportImportedSpecifierDependency.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,41 @@ HarmonyExportImportedSpecifierDependency.prototype.getReference = function() {
2424
var used = this.originModule.isUsed(this.name);
2525
var active = HarmonyModulesHelpers.isActive(this.originModule, this);
2626
if(!this.importDependency.module || !used || !active) return null;
27-
return {
28-
module: this.importDependency.module,
29-
importedNames: this.id ? [this.id] : true,
30-
precedence: this.name ? 2 : 3
31-
};
27+
if(!this.originModule.usedExports) return null;
28+
var m = this.importDependency.module;
29+
if(!this.name) {
30+
// export *
31+
if(Array.isArray(this.originModule.usedExports)) {
32+
// reexport * with known used exports
33+
var activeExports = HarmonyModulesHelpers.getActiveExports(this.originModule);
34+
return {
35+
module: m,
36+
importedNames: this.originModule.usedExports.filter(function(id) {
37+
return activeExports.indexOf(id) < 0;
38+
})
39+
}
40+
} else {
41+
return {
42+
module: m,
43+
importedNames: true
44+
}
45+
}
46+
} else {
47+
if(Array.isArray(this.originModule.usedExports) && this.originModule.usedExports.indexOf(this.name) < 0) return null;
48+
if(this.id) {
49+
// export { name as name }
50+
return {
51+
module: m,
52+
importedNames: [this.id]
53+
};
54+
} else {
55+
// export { * as name }
56+
return {
57+
module: m,
58+
importedNames: true
59+
};
60+
}
61+
}
3262
};
3363

3464
HarmonyExportImportedSpecifierDependency.prototype.describeHarmonyExport = function() {
@@ -77,7 +107,7 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
77107
var items = dep.originModule.usedExports.map(function(id) {
78108
if(id === "default") return;
79109
if(activeExports.indexOf(id) >= 0) return;
80-
if(importIsHarmony && importActiveExports.indexOf(id) < 0) return;
110+
if(importIsHarmony && !HarmonyModulesHelpers.isExportedByHarmony(importedModule, id)) return;
81111
var exportUsed = dep.originModule.isUsed(id);
82112
var idUsed = importedModule && importedModule.isUsed(id);
83113
return [exportUsed, idUsed];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./b";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./c";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./d";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var test = 123;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { test } from "./a";
2+
import { func1, func3 } from "./x";
3+
4+
it("should correctly tree shake star exports", function() {
5+
test.should.be.eql(123);
6+
func1().should.be.eql("func1");
7+
func3().should.be.eql("func3");
8+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./x1";
2+
export * from "./x2";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export function func1() { return "func1"; }
2+
export function func2() { return "func2"; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export function func3() { return "func3"; }
2+
export function func4() { return "func4"; }

0 commit comments

Comments
 (0)