Skip to content

Commit

Permalink
Merge pull request #13997 from yujunjung/main
Browse files Browse the repository at this point in the history
fix: exporting async module as library `type:  "module"`
  • Loading branch information
sokra committed Aug 19, 2021
2 parents bd7cb37 + f689021 commit ff69a4a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/library/ModuleLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
const result = new ConcatSource(source);
const exportsInfo = moduleGraph.getExportsInfo(module);
const exports = [];
const isAsync = moduleGraph.isAsync(module);
if (isAsync) {
result.add(`__webpack_exports__ = await __webpack_exports__;\n`);
}
for (const exportInfo of exportsInfo.orderedExports) {
if (!exportInfo.provided) continue;
const varName = `__webpack_exports__${Template.toIdentifier(
Expand Down
1 change: 1 addition & 0 deletions test/configCases/async-library/0-create-library/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = await Promise.resolve(42);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.noTests = true;
18 changes: 18 additions & 0 deletions test/configCases/async-library/0-create-library/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import("../../../../types").Configuration} */
module.exports = {
entry: "./a.js",
output: {
filename: "lib.js",
library: {
type: "module"
}
},
target: "node14",
optimization: {
minimize: true
},
experiments: {
topLevelAwait: true,
outputModule: true
}
};
5 changes: 5 additions & 0 deletions test/configCases/async-library/1-use-library/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
it("should get valid export from library", () => {
return import("library").then(({ a }) => {
expect(a).toBe(42);
});
});
18 changes: 18 additions & 0 deletions test/configCases/async-library/1-use-library/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var path = require("path");

/** @type {function(any, any): import("../../../../types").Configuration} */
module.exports = (env, { testPath }) => ({
target: "node14",
output: {
chunkLoading: "import"
},
resolve: {
alias: {
library: path.resolve(testPath, "../0-create-library/lib.js")
}
},
experiments: {
topLevelAwait: true,
outputModule: true
}
});

0 comments on commit ff69a4a

Please sign in to comment.