Skip to content

Commit

Permalink
emit correct code for reexport unused exports
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jun 23, 2018
1 parent 50fe2e7 commit 3502287
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Expand Up @@ -602,10 +602,10 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS

getReexportStatement(module, key, name, valueKey) {
const exportsName = module.exportsArgument;
const returnValue = this.getReturnValue(valueKey);
const returnValue = this.getReturnValue(name, valueKey);
return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
key
)}, function() { return ${name}${returnValue}; });\n`;
)}, function() { return ${returnValue}; });\n`;
}

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

getConditionalReexportStatement(module, key, name, valueKey) {
const exportsName = module.exportsArgument;
const returnValue = this.getReturnValue(valueKey);
const returnValue = this.getReturnValue(name, valueKey);
return `if(__webpack_require__.o(${name}, ${JSON.stringify(
valueKey
)})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
key
)}, function() { return ${name}${returnValue}; });\n`;
)}, function() { return ${returnValue}; });\n`;
}

getReturnValue(valueKey) {
getReturnValue(name, valueKey) {
if (valueKey === null) {
return "_default.a";
return `${name}_default.a`;
}
if (valueKey === "") {
return name;
}
if (valueKey === false) {
return "/* unused export */ undefined";
}

return valueKey && "[" + JSON.stringify(valueKey) + "]";
return `${name}[${JSON.stringify(valueKey)}]`;
}
};
3 changes: 3 additions & 0 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -133,6 +133,9 @@ const getFinalName = (
strictHarmonyModule
);
}
if (!info.module.isUsed(directExport)) {
return "/* unused export */ undefined";
}
const name = info.internalNames.get(directExport);
if (!name) {
throw new Error(
Expand Down
@@ -0,0 +1,9 @@
import { test, unused } from "./module";

it("should run the test", () => {
expect(test()).toEqual({
used: "used",
unused: undefined
});
expect(unused).toEqual(undefined);
});
@@ -0,0 +1,10 @@
import { used, unused } from "./reference";

export function test() {
return {
used,
unused
};
}

export { unused }
@@ -0,0 +1,3 @@
export var used = "used";

export var unused = "unused";
@@ -0,0 +1,34 @@
const DependencyReference = require("../../../../").dependencies
.DependencyReference;
module.exports = {
optimization: {
usedExports: true,
concatenateModules: true
},
plugins: [
function() {
this.hooks.compilation.tap("Test", compilation => {
compilation.hooks.dependencyReference.tap(
"Test",
(ref, dep, module) => {
if (
module.identifier().endsWith("module.js") &&
ref.module &&
ref.module.identifier().endsWith("reference.js") &&
Array.isArray(ref.importedNames) &&
ref.importedNames.includes("unused")
) {
return new DependencyReference(
ref.module,
ref.importedNames.filter(item => item !== "unused"),
ref.weak,
ref.order
);
}
return ref;
}
);
});
}
]
};
5 changes: 3 additions & 2 deletions test/configCases/deep-scope-analysis/remove-export/index.js
@@ -1,8 +1,9 @@
import { test } from "./module";
import { test, unused } from "./module";

it("should run the test", () => {
expect(test()).toEqual({
used: "used",
unused: undefined
})
});
expect(unused).toEqual(undefined);
});
2 changes: 2 additions & 0 deletions test/configCases/deep-scope-analysis/remove-export/module.js
Expand Up @@ -6,3 +6,5 @@ export function test() {
unused
};
}

export { unused }

0 comments on commit 3502287

Please sign in to comment.