feat: 前端打包增加分包策略

This commit is contained in:
GraceWalk
2022-09-01 19:44:37 +08:00
parent 28e8540c78
commit 500eaace82
2 changed files with 19 additions and 23 deletions

View File

@@ -86,12 +86,12 @@ class CoverHtmlWebpackPlugin {
assetJson.reverse().forEach((item) => { assetJson.reverse().forEach((item) => {
if (/\.js$/.test(item)) { if (/\.js$/.test(item)) {
// if (item.includes('vendor~')) { if (item.includes('vendor~')) {
// vendors += `<script async src="${item}"></script>`; vendors += `<script async src="${item}"></script>`;
// } else { } else {
// TODO: entry 只有一个 // TODO: entry 只有一个
portalMap['@portal/layout'] = item; portalMap['@portal/layout'] = item;
// } }
} else if (/\.css$/.test(item)) { } else if (/\.css$/.test(item)) {
links += `<link href="${item}" rel="stylesheet">`; links += `<link href="${item}" rel="stylesheet">`;
} }

View File

@@ -9,7 +9,6 @@ const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const theme = require('./theme'); const theme = require('./theme');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production'; const isProd = process.env.NODE_ENV === 'production';
const babelOptions = { const babelOptions = {
@@ -43,7 +42,6 @@ const babelOptions = {
module.exports = () => { module.exports = () => {
const cssFileName = isProd ? '[name]-[chunkhash].css' : '[name].css'; const cssFileName = isProd ? '[name]-[chunkhash].css' : '[name].css';
const plugins = [ const plugins = [
// !isProd && new HardSourceWebpackPlugin(),
new CoverHtmlWebpackPlugin(), new CoverHtmlWebpackPlugin(),
new ProgressBarPlugin(), new ProgressBarPlugin(),
new CaseSensitivePathsPlugin(), new CaseSensitivePathsPlugin(),
@@ -150,23 +148,21 @@ module.exports = () => {
], ],
}, },
optimization: Object.assign( optimization: Object.assign(
// {
// splitChunks: {
// cacheGroups: {
// vendor: {
// test: /[\\/]node_modules[\\/]/,
// chunks: 'all',
// name: 'vendor',
// priority: 10,
// enforce: true,
// minChunks: 1,
// maxSize: 3500000,
// },
// },
// },
// },
isProd isProd
? { ? {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
chunks: 'all',
name: 'vendor',
priority: 10,
enforce: true,
minChunks: 1,
maxSize: 3000000,
},
},
},
minimizer: [ minimizer: [
new TerserJSPlugin({ new TerserJSPlugin({
cache: true, cache: true,