采用@babel/polyfill兼容
步骤一:先安装
npm i @babel/polyfill -D
npx webpack打包后最终效果
原因:import @babel/polyfill,这样是全局引入整个polyfill包,包体积大,会污染全局环境
采取这个方式
npm i exports-loader -D
最后页面上删除import @babel/polyfill即可,npx webpack即可
const HtmlWebpackPlugin = require('html-webpack-plugin');
//导入模块联邦
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
mode: 'production',
entry: './src/index.js',
plugins: [
new HtmlWebpackPlugin(),
//实例化一下
new ModuleFederationPlugin({
name: 'nav', //标识联邦模块的名字,别的应用访问这个组件就要通过这个名字
filename: 'remoteEntry.js', //这个远端使用这个模块的名字
remotes: {}, //引用其他的联邦模块
exposes: {
//暴露组件
'./Header': './src/Header.js', //./Header代表到时候别人用的时候基于这个路径拼接url
},
shared: {},
}),
],
};
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'production',
entry: {
jquery: ['jquery'],
},
output: {
filename: '[name.js]',
path: path.resolve(__dirname, 'dll'),
library: '[name]_[hash]',
},
plugins: [
new webpack.DllPlugin({
name: '[name]_[hash]',
path: path.resolve(__dirname, 'dll/manifest.json'),
}),
],
};
module.exports = {
mode: 'development',
entry: './src/index.js',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
//使用worker池分流
{
loader: 'thread-loader',
options: {
workers: 2,
},
},
{
//目的,解析一些es6的代码
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
],
},
],
},
};