Skip to content

Commit ff69a4a

Browse files
authored
Merge pull request #13997 from yujunjung/main
fix: exporting async module as library `type: "module"`
2 parents bd7cb37 + f689021 commit ff69a4a

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

lib/library/ModuleLibraryPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
7878
const result = new ConcatSource(source);
7979
const exportsInfo = moduleGraph.getExportsInfo(module);
8080
const exports = [];
81+
const isAsync = moduleGraph.isAsync(module);
82+
if (isAsync) {
83+
result.add(`__webpack_exports__ = await __webpack_exports__;\n`);
84+
}
8185
for (const exportInfo of exportsInfo.orderedExports) {
8286
if (!exportInfo.provided) continue;
8387
const varName = `__webpack_exports__${Template.toIdentifier(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = await Promise.resolve(42);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.noTests = true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type {import("../../../../types").Configuration} */
2+
module.exports = {
3+
entry: "./a.js",
4+
output: {
5+
filename: "lib.js",
6+
library: {
7+
type: "module"
8+
}
9+
},
10+
target: "node14",
11+
optimization: {
12+
minimize: true
13+
},
14+
experiments: {
15+
topLevelAwait: true,
16+
outputModule: true
17+
}
18+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it("should get valid export from library", () => {
2+
return import("library").then(({ a }) => {
3+
expect(a).toBe(42);
4+
});
5+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var path = require("path");
2+
3+
/** @type {function(any, any): import("../../../../types").Configuration} */
4+
module.exports = (env, { testPath }) => ({
5+
target: "node14",
6+
output: {
7+
chunkLoading: "import"
8+
},
9+
resolve: {
10+
alias: {
11+
library: path.resolve(testPath, "../0-create-library/lib.js")
12+
}
13+
},
14+
experiments: {
15+
topLevelAwait: true,
16+
outputModule: true
17+
}
18+
});

0 commit comments

Comments
 (0)