Merge pull request #13997 from yujunjung/main

fix: exporting async module as library `type:  "module"`
This commit is contained in:
Tobias Koppers 2021-08-19 13:20:38 +02:00 committed by GitHub
commit ff69a4a631
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 0 deletions

View File

@ -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(

View File

@ -0,0 +1 @@
export const a = await Promise.resolve(42);

View File

@ -0,0 +1 @@
exports.noTests = true;

View File

@ -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
}
};

View File

@ -0,0 +1,5 @@
it("should get valid export from library", () => {
return import("library").then(({ a }) => {
expect(a).toBe(42);
});
});

View File

@ -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
}
});