Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep/restore comments in export default #8042

Merged
merged 1 commit into from Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/dependencies/HarmonyExportDependencyParserPlugin.js
Expand Up @@ -54,10 +54,25 @@ module.exports = class HarmonyExportDependencyParserPlugin {
parser.hooks.exportExpression.tap(
"HarmonyExportDependencyParserPlugin",
(statement, expr) => {
const comments = parser.getComments([
statement.range[0],
expr.range[0]
]);
const dep = new HarmonyExportExpressionDependency(
parser.state.module,
expr.range,
statement.range
statement.range,
comments
.map(c => {
switch (c.type) {
case "Block":
return `/*${c.value}*/`;
case "Line":
return `//${c.value}\n`;
}
return "";
})
.join("")
);
dep.loc = Object.create(statement.loc);
dep.loc.index = -1;
Expand Down
9 changes: 7 additions & 2 deletions lib/dependencies/HarmonyExportExpressionDependency.js
Expand Up @@ -6,11 +6,12 @@
const NullDependency = require("./NullDependency");

class HarmonyExportExpressionDependency extends NullDependency {
constructor(originModule, range, rangeStatement) {
constructor(originModule, range, rangeStatement, prefix) {
super();
this.originModule = originModule;
this.range = range;
this.rangeStatement = rangeStatement;
this.prefix = prefix;
}

get type() {
Expand All @@ -31,7 +32,11 @@ HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTempla
const content = this.getContent(dep.originModule, used);

if (dep.range) {
source.replace(dep.rangeStatement[0], dep.range[0] - 1, content + "(");
source.replace(
dep.rangeStatement[0],
dep.range[0] - 1,
content + "(" + dep.prefix
);
source.replace(dep.range[1], dep.rangeStatement[1] - 1, ");");
return;
}
Expand Down
11 changes: 11 additions & 0 deletions test/configCases/code-generation/harmony-pure-default/index.js
@@ -0,0 +1,11 @@
import { value as v1 } from "./module1";
import { value as v2 } from "./module2";
import { value as v3 } from "./module3";
import { value as v4 } from "./module4";

it("should not execute exports when annotated with pure comment", () => {
expect(v1).toBe(42);
expect(v2).toBe(42);
expect(v3).toBe(42);
expect(v4).toBe(42);
});
@@ -0,0 +1,9 @@
let value = 42;

const inc = () => {
value++;
};

export default /*#__PURE__*/inc();

export { value };
@@ -0,0 +1,9 @@
let value = 42;

const inc = () => {
value++;
};

export default (/*#__PURE__*/inc());

export { value };
@@ -0,0 +1,9 @@
let value = 42;

const inc = () => {
value++;
};

export default /*#__PURE__*/(inc());

export { value };
15 changes: 15 additions & 0 deletions test/configCases/code-generation/harmony-pure-default/module4.js
@@ -0,0 +1,15 @@
let value = 42;

const inc = () => {
value++;
};

export
// hello
default
// world
/*#__PURE__*/
inc()
;

export { value };
@@ -0,0 +1,7 @@
module.exports = {
mode: "production",
optimization: {
minimize: true,
concatenateModules: false
}
};