2019-06-05 17:15:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-06-05 19:25:15 +08:00
|
|
|
const WebpackError = require("../WebpackError");
|
2019-06-05 18:07:20 +08:00
|
|
|
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
|
|
|
|
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
|
2019-06-05 17:15:25 +08:00
|
|
|
|
2019-06-05 18:07:20 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
/** @typedef {import("../Module")} Module */
|
2019-06-05 17:15:25 +08:00
|
|
|
|
|
|
|
class InferAsyncModulesPlugin {
|
2019-06-05 17:41:53 +08:00
|
|
|
constructor(options) {
|
2019-06-05 19:25:15 +08:00
|
|
|
const { errorOnMissingAwait = false } = options || {};
|
|
|
|
this.errorOnMissingAwait = errorOnMissingAwait;
|
2019-06-05 17:41:53 +08:00
|
|
|
}
|
2019-06-05 17:15:25 +08:00
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler webpack compiler
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.compilation.tap("InferAsyncModulesPlugin", compilation => {
|
|
|
|
const { moduleGraph } = compilation;
|
|
|
|
compilation.hooks.finishModules.tap(
|
|
|
|
"InferAsyncModulesPlugin",
|
|
|
|
modules => {
|
|
|
|
/** @type {Set<Module>} */
|
|
|
|
const queue = new Set();
|
|
|
|
for (const module of modules) {
|
|
|
|
if (module.buildMeta && module.buildMeta.async) {
|
|
|
|
queue.add(module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const module of queue) {
|
|
|
|
moduleGraph.setAsync(module);
|
|
|
|
const connections = moduleGraph.getIncomingConnections(module);
|
|
|
|
for (const connection of connections) {
|
2019-06-05 17:41:53 +08:00
|
|
|
const dep = connection.dependency;
|
2019-10-30 04:37:59 +08:00
|
|
|
if (dep instanceof HarmonyImportDependency && connection.active) {
|
2019-06-05 17:41:53 +08:00
|
|
|
if (
|
2019-06-05 19:25:15 +08:00
|
|
|
this.errorOnMissingAwait &&
|
2019-06-05 17:41:53 +08:00
|
|
|
dep instanceof HarmonyImportSideEffectDependency &&
|
|
|
|
!dep.await
|
|
|
|
) {
|
|
|
|
const error = new WebpackError(
|
|
|
|
"Tried to import async module with normal import/export (must use 'import await'/'export await' instead)"
|
|
|
|
);
|
|
|
|
error.module = module;
|
|
|
|
error.loc = dep.loc;
|
|
|
|
compilation.errors.push(error);
|
|
|
|
}
|
2019-06-05 17:15:25 +08:00
|
|
|
queue.add(connection.originModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InferAsyncModulesPlugin;
|