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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

support destructuring assignment with AwaitExpression #16995

Merged
merged 4 commits into from Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion lib/dependencies/ImportParserPlugin.js
Expand Up @@ -27,6 +27,8 @@ class ImportParserPlugin {
}

apply(parser) {
const exportsFromEnumerable = enumerable =>
Array.from(enumerable, e => [e]);
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
const param = parser.evaluateExpression(expr.source);

Expand Down Expand Up @@ -184,7 +186,7 @@ class ImportParserPlugin {
if (typeof importOptions.webpackExports === "string") {
exports = [[importOptions.webpackExports]];
} else {
exports = Array.from(importOptions.webpackExports, e => [e]);
exports = exportsFromEnumerable(importOptions.webpackExports);
}
}
}
Expand All @@ -205,6 +207,20 @@ class ImportParserPlugin {
mode = "lazy";
}

const referencedPropertiesInDestructuring =
parser.destructuringAssignmentPropertiesFor(expr);
if (referencedPropertiesInDestructuring) {
if (exports) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chenxsan I'm pinging you here for this one this will likely be a documentation note for this feature. I'll try to remember to reference the generated issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely be added to the Module Methods or magic comments section where /* webpackExports */ is used.

`\`webpackExports\` could not be used with destructuring assignment.`,
expr.loc
)
);
}
exports = exportsFromEnumerable(referencedPropertiesInDestructuring);
}

if (param.isString()) {
if (mode === "eager") {
const dep = new ImportEagerDependency(
Expand Down
14 changes: 12 additions & 2 deletions lib/javascript/JavascriptParser.js
Expand Up @@ -1927,7 +1927,12 @@ class JavascriptParser extends Parser {
for (const id of set) keys.add(id);
}

this.destructuringAssignmentProperties.set(expression.right, keys);
this.destructuringAssignmentProperties.set(
expression.right.type === "AwaitExpression"
? expression.right.argument
: expression.right,
keys
);

if (expression.right.type === "AssignmentExpression") {
this.preWalkAssignmentExpression(expression.right);
Expand Down Expand Up @@ -2183,7 +2188,12 @@ class JavascriptParser extends Parser {
const keys = this._preWalkObjectPattern(declarator.id);

if (!keys) return;
this.destructuringAssignmentProperties.set(declarator.init, keys);
this.destructuringAssignmentProperties.set(
declarator.init.type === "AwaitExpression"
? declarator.init.argument
: declarator.init,
keys
);

if (declarator.init.type === "AssignmentExpression") {
this.preWalkAssignmentExpression(declarator.init);
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Expand Up @@ -1840,9 +1840,9 @@ webpack x.x.x compiled successfully in X ms"
`;

exports[`StatsTestCases should print correct stats for output-module 1`] = `
"asset main.mjs 10 KiB [emitted] [javascript module] (name: main)
asset 52.mjs 402 bytes [emitted] [javascript module]
runtime modules 6.07 KiB 8 modules
"asset main.mjs 9.57 KiB [emitted] [javascript module] (name: main)
asset 52.mjs 358 bytes [emitted] [javascript module]
runtime modules 5.8 KiB 7 modules
orphan modules 38 bytes [orphan] 1 module
cacheable modules 263 bytes
./index.js + 1 modules 225 bytes [built] [code generated]
Expand Down
3 changes: 3 additions & 0 deletions test/cases/chunks/destructuring-assignment/dir1/a.js
@@ -0,0 +1,3 @@
export const a = 1;
export default 3;
export const usedExports = __webpack_exports_info__.usedExports;
12 changes: 12 additions & 0 deletions test/cases/chunks/destructuring-assignment/index.js
@@ -0,0 +1,12 @@
it("should load only used exports", async (done) => {
const { default: def, usedExports } = await import("./dir1/a");
expect(def).toBe(3);
expect(usedExports).toEqual(["default", "usedExports"]);
done();
});

it("should get warning on using 'webpackExports' with destructuring assignment", async (done) => {
const { default: def } = await import(/* webpackExports: ["a"] */"./dir1/a?2");
expect(def).toBe(3);
done();
});
3 changes: 3 additions & 0 deletions test/cases/chunks/destructuring-assignment/warnings.js
@@ -0,0 +1,3 @@
module.exports = [
[/`webpackExports` could not be used with destructuring assignment./]
];