Skip to content

Commit

Permalink
support destructuring assignment with AwaitExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Apr 13, 2023
1 parent ecdcc1a commit 3824bee
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
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(
`\`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
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./]
];

0 comments on commit 3824bee

Please sign in to comment.