Skip to content

Commit 3502287

Browse files
committed
emit correct code for reexport unused exports
1 parent 50fe2e7 commit 3502287

File tree

8 files changed

+77
-9
lines changed

8 files changed

+77
-9
lines changed

lib/dependencies/HarmonyExportImportedSpecifierDependency.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,10 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
602602

603603
getReexportStatement(module, key, name, valueKey) {
604604
const exportsName = module.exportsArgument;
605-
const returnValue = this.getReturnValue(valueKey);
605+
const returnValue = this.getReturnValue(name, valueKey);
606606
return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
607607
key
608-
)}, function() { return ${name}${returnValue}; });\n`;
608+
)}, function() { return ${returnValue}; });\n`;
609609
}
610610

611611
getReexportFakeNamespaceObjectStatement(module, key, name) {
@@ -617,19 +617,25 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
617617

618618
getConditionalReexportStatement(module, key, name, valueKey) {
619619
const exportsName = module.exportsArgument;
620-
const returnValue = this.getReturnValue(valueKey);
620+
const returnValue = this.getReturnValue(name, valueKey);
621621
return `if(__webpack_require__.o(${name}, ${JSON.stringify(
622622
valueKey
623623
)})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
624624
key
625-
)}, function() { return ${name}${returnValue}; });\n`;
625+
)}, function() { return ${returnValue}; });\n`;
626626
}
627627

628-
getReturnValue(valueKey) {
628+
getReturnValue(name, valueKey) {
629629
if (valueKey === null) {
630-
return "_default.a";
630+
return `${name}_default.a`;
631+
}
632+
if (valueKey === "") {
633+
return name;
634+
}
635+
if (valueKey === false) {
636+
return "/* unused export */ undefined";
631637
}
632638

633-
return valueKey && "[" + JSON.stringify(valueKey) + "]";
639+
return `${name}[${JSON.stringify(valueKey)}]`;
634640
}
635641
};

lib/optimize/ConcatenatedModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ const getFinalName = (
133133
strictHarmonyModule
134134
);
135135
}
136+
if (!info.module.isUsed(directExport)) {
137+
return "/* unused export */ undefined";
138+
}
136139
const name = info.internalNames.get(directExport);
137140
if (!name) {
138141
throw new Error(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, unused } from "./module";
2+
3+
it("should run the test", () => {
4+
expect(test()).toEqual({
5+
used: "used",
6+
unused: undefined
7+
});
8+
expect(unused).toEqual(undefined);
9+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { used, unused } from "./reference";
2+
3+
export function test() {
4+
return {
5+
used,
6+
unused
7+
};
8+
}
9+
10+
export { unused }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export var used = "used";
2+
3+
export var unused = "unused";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const DependencyReference = require("../../../../").dependencies
2+
.DependencyReference;
3+
module.exports = {
4+
optimization: {
5+
usedExports: true,
6+
concatenateModules: true
7+
},
8+
plugins: [
9+
function() {
10+
this.hooks.compilation.tap("Test", compilation => {
11+
compilation.hooks.dependencyReference.tap(
12+
"Test",
13+
(ref, dep, module) => {
14+
if (
15+
module.identifier().endsWith("module.js") &&
16+
ref.module &&
17+
ref.module.identifier().endsWith("reference.js") &&
18+
Array.isArray(ref.importedNames) &&
19+
ref.importedNames.includes("unused")
20+
) {
21+
return new DependencyReference(
22+
ref.module,
23+
ref.importedNames.filter(item => item !== "unused"),
24+
ref.weak,
25+
ref.order
26+
);
27+
}
28+
return ref;
29+
}
30+
);
31+
});
32+
}
33+
]
34+
};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { test } from "./module";
1+
import { test, unused } from "./module";
22

33
it("should run the test", () => {
44
expect(test()).toEqual({
55
used: "used",
66
unused: undefined
7-
})
7+
});
8+
expect(unused).toEqual(undefined);
89
});

test/configCases/deep-scope-analysis/remove-export/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export function test() {
66
unused
77
};
88
}
9+
10+
export { unused }

0 commit comments

Comments
 (0)