Skip to content

Commit

Permalink
Merge pull request #16995 from webpack/feat/destructuring-assignment-…
Browse files Browse the repository at this point in the history
…await

support destructuring assignment with AwaitExpression
  • Loading branch information
TheLarkInn committed Apr 15, 2023
2 parents ee2fe97 + 185d6d6 commit 7b42d98
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 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
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();
});
4 changes: 4 additions & 0 deletions test/cases/chunks/destructuring-assignment/test.filter.js
@@ -0,0 +1,4 @@
module.exports = function (config) {
// This test can't run in development mode
return config.mode !== "development";
};
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 7b42d98

Please sign in to comment.