const LINKING_ERROR_TAG = 'was not found in'; // build 时会出现 css modules 的引用警告,但这应该是需要忽略的 const CSS_NO_EXPORTS = /\.(css|sass|scss|styl|less)' \(module has no exports\)/; class HarmonyLinkingErrorPlugin { apply(compiler) { compiler.hooks.afterCompile.tap('HarmonyLinkingErrorPlugin', (compilation) => { if (!compilation.warnings.length) { return; } const harmonyLinkingErrors = compilation.warnings.filter((w) => { return ( w.name === 'ModuleDependencyWarning' && !w.module.resource.includes('node_modules') && w.message.includes(LINKING_ERROR_TAG) && !CSS_NO_EXPORTS.test(w.message) ); }); if (!harmonyLinkingErrors.length) { return; } compilation.errors.push(...harmonyLinkingErrors); }); } } module.exports = async function addHarmonyLinkingErrorPlugin(opts) { const { config } = opts; config.plugin('harmony-linking-error-plugin').use(HarmonyLinkingErrorPlugin); };