123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- const webpack = require('webpack');
- const path = require('path');
-
- const CopyWebpackPlugin = require('copy-webpack-plugin');
- const HtmlWebpackPlugin = require('html-webpack-plugin');
- const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
- const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
-
- const baseConfig = require('./webpack.config.base');
-
- const { buildEnv } = baseConfig.extra;
-
- const config = {
- ...baseConfig,
- devtool: false,
- mode: 'production',
- output: {
- ...baseConfig.output,
- filename: '[name].js',
- },
- module: {
- rules: [
- ...baseConfig.module.rules.filter(
- (rule) => !['/\\.css$/', '/\\.less$/', '/\\.(scss|sass)$/'].includes(rule.test.toString())
- ),
- {
- test: /\.css$/,
- use: ['style-loader', 'css-loader', 'postcss-loader'],
- },
- {
- test: /\.less$/,
- exclude: /node_modules/,
- use: ['style-loader', 'css-loader', 'postcss-loader', baseConfig.extra.lessLoader],
- },
- {
- test: /\.less$/,
- include: /node_modules/,
- use: ['style-loader', 'css-loader', 'postcss-loader', baseConfig.extra.lessLoader],
- },
- ],
- },
- plugins: [
- ...baseConfig.plugins,
- new webpack.DefinePlugin({
- isProd: JSON.stringify(true),
- }),
-
- // 使用 Prepack 优化包体大小
- // 暂时存在 Bug,等待修复
- // 使用前 21 - 425
- // 使用后 21 - 433
- // new PrepackWebpackPlugin({
- // mathRandomSeed: '0'
- // }),
-
- // 必须将 CopyWebpackPlugin 与 HtmlWebpackPlugin 添加到末尾
- new CopyWebpackPlugin({ patterns: [{ from: buildEnv.public, to: buildEnv.build }] }),
- new HtmlWebpackPlugin({
- template: path.join(__dirname, '../template/template.ejs'),
- title: 'Webpack React',
- favicon: path.join(baseConfig.extra.buildEnv.public, 'favicon.ico'),
- manifest: path.join(buildEnv.public, 'manifest.json'),
- meta: [
- { name: 'robots', content: 'noindex,nofollow' },
- {
- name: 'viewport',
- content:
- 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no',
- },
- ],
- appMountIds: ['root'],
- inject: false,
- minify: {
- html5: true,
- useShortDoctype: true,
- collapseWhitespace: true,
- conservativeCollapse: true,
- preserveLineBreaks: true,
- removeComments: true,
- keepClosingSlash: true,
- removeRedundantAttributes: true,
- removeEmptyAttributes: true,
- removeStyleLinkTypeAttributes: true,
- },
- mobile: true,
- scripts: ['./static.js'],
- }),
- ],
- optimization: {
- runtimeChunk: false,
- minimizer: [
- new UglifyJsPlugin({
- exclude: /.*ts-worker.*/,
- }),
- new OptimizeCSSAssetsPlugin({}),
- ],
- },
- };
-
- delete config.extra;
-
- module.exports = config;
|