有些插件和loader引入了webpack的模块(比如monaco-editor-webpack-plugin、worker-loader),
会有The 'compilation' argument must be an instance of Compilation
错误。
这个错误是由存在多个webpack引起的。
这个库导出umi@3.4 webpack5模式下@umijs/deps的webpack模块,重定向到webpack
模块。
用于解决使用webpack插件引起的多个webpack冲突的问题。
安装时覆盖webpack。
{
"devDependencies": {
"webpack": "npm:@bbkkbkk/umi-webpack5-export"
}
}
或者使用module-alias
,在config.js文件头部加载如下alias.js
文件。
// alias.js
import moduleAlias from 'module-alias';
moduleAlias.addAlias('webpack', function(fromPath, request, alias) {
if (/@umijs[\\/]/.test(fromPath)) {
return 'webpack';
}
// umi的某个版本在使用worker-loader时会提示错误,添加如下代码
// 没有错误的话不要加这个
if (/worker-loader/.test(fromPath)) {
if (/ExternalsPlugin|FetchCompileWasmPlugin|FetchCompileAsyncWasmPlugin/i.test(request)) {
return '@bbkkbkk/umi-webpack5-export';
}
return 'webpack';
}
return '@bbkkbkk/umi-webpack5-export';
});
导入webpack。
import webpack from 'webpack';
// or
const webpack = require('webpack');
导入@umi/deps的bundle。
import { bundle } from '@bbkkbkk/umi-webpack5-export/bundle';
npm run build
编译文件npm run test
测试