webpack.prod.babel.js 4.31 KB
Newer Older
Julien Benchetrit's avatar
Julien Benchetrit committed
1
// Important modules this config uses
2
const os = require('os');
Julien Benchetrit's avatar
Julien Benchetrit committed
3 4 5
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
邓超's avatar
邓超 committed
6 7
// const OfflinePlugin = require('offline-plugin');
// const { HashedModuleIdsPlugin } = require('webpack');
Julien Benchetrit's avatar
Julien Benchetrit committed
8 9 10
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');

11 12
// cpu核数
const threads = os.cpus().length;
Julien Benchetrit's avatar
Julien Benchetrit committed
13 14 15 16
module.exports = require('./webpack.base.babel')({
  mode: 'production',

  // In production, we skip all hot-reloading stuff
17
  entry: [require.resolve('react-app-polyfill/ie11'), path.join(process.cwd(), 'src/app.js')],
Julien Benchetrit's avatar
Julien Benchetrit committed
18 19

  // Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
tianfen's avatar
tianfen committed
20
  // [chunkhash:8] [chunkhash:8]
Julien Benchetrit's avatar
Julien Benchetrit committed
21
  output: {
邓晓峰's avatar
邓晓峰 committed
22 23
    filename: 'static/[name].[chunkhash:8].js',
    chunkFilename: 'static/[name].[chunkhash:8].chunk.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
24 25 26 27
  },

  optimization: {
    minimize: true,
28 29
    minimizer: [
      new TerserPlugin({
田翔's avatar
田翔 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        // terserOptions: {
        //   warnings: false,
        //   compress: {
        //     comparisons: false,
        //     drop_console: true,
        //     drop_debugger: true,
        //     pure_funcs: ['console.log'], // 移除console
        //   },
        //   parse: {},
        //   mangle: true,
        //   output: {
        //     comments: false,
        //     ascii_only: true,
        //   },
        // },
45
        parallel: threads, // 开启多进程
46 47 48 49
        cache: true,
        sourceMap: true,
      }),
    ],
Julien Benchetrit's avatar
Julien Benchetrit committed
50 51 52 53 54 55
    nodeEnv: 'production',
    sideEffects: true,
    concatenateModules: true,
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
56 57 58 59 60 61 62 63 64 65 66
      maxInitialRequests: 10,
      minSize: 0,
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
            return `npm.${packageName.replace('@', '')}`;
          },
        },
      },
Julien Benchetrit's avatar
Julien Benchetrit committed
67 68 69 70 71 72
    },
  },

  plugins: [
    // Minify and optimize the index.html
    new HtmlWebpackPlugin({
dengxiaofeng's avatar
dengxiaofeng committed
73
      template: 'src/index.html',
Julien Benchetrit's avatar
Julien Benchetrit committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true,
      },
      inject: true,
    }),

    // Put it in the end to capture all the HtmlWebpackPlugin's
    // assets manipulations and do leak its manipulations to HtmlWebpackPlugin
邓超's avatar
邓超 committed
91 92 93 94
    // new OfflinePlugin({
    //   relativePaths: false,
    //   publicPath: '/',
    //   appShell: '/',
Julien Benchetrit's avatar
Julien Benchetrit committed
95

邓超's avatar
邓超 committed
96 97 98
    //   // No need to cache .htaccess. See http://mxs.is/googmp,
    //   // this is applied before any match in `caches` section
    //   excludes: ['.htaccess'],
Julien Benchetrit's avatar
Julien Benchetrit committed
99

邓超's avatar
邓超 committed
100 101
    //   caches: {
    //     main: [':rest:'],
Julien Benchetrit's avatar
Julien Benchetrit committed
102

邓超's avatar
邓超 committed
103 104 105 106 107
    //     // All chunks marked as `additional`, loaded after main section
    //     // and do not prevent SW to install. Change to `optional` if
    //     // do not want them to be preloaded at all (cached only when first loaded)
    //     additional: ['*.chunk.js'],
    //   },
Julien Benchetrit's avatar
Julien Benchetrit committed
108

邓超's avatar
邓超 committed
109 110 111
    //   // Removes warning for about `additional` section usage
    //   safeToUseOptionalCaches: true,
    // }),
Julien Benchetrit's avatar
Julien Benchetrit committed
112 113 114 115 116 117 118 119 120

    new CompressionPlugin({
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.8,
    }),

    new WebpackPwaManifest({
dengxiaofeng's avatar
dengxiaofeng committed
121 122 123
      name: '运维管理平台',
      short_name: '运维管理平台',
      description: '运维管理平台',
Julien Benchetrit's avatar
Julien Benchetrit committed
124 125 126 127 128 129
      background_color: '#fafafa',
      theme_color: '#b1624d',
      inject: true,
      ios: true,
      icons: [
        {
dengxiaofeng's avatar
dengxiaofeng committed
130
          src: path.resolve('src/images/icon-512x512.png'),
Julien Benchetrit's avatar
Julien Benchetrit committed
131 132 133
          sizes: [72, 96, 128, 144, 192, 384, 512],
        },
        {
dengxiaofeng's avatar
dengxiaofeng committed
134
          src: path.resolve('src/images/icon-512x512.png'),
Julien Benchetrit's avatar
Julien Benchetrit committed
135 136 137 138 139 140
          sizes: [120, 152, 167, 180],
          ios: true,
        },
      ],
    }),

邓超's avatar
邓超 committed
141 142 143 144 145
    // new HashedModuleIdsPlugin({
    //   hashFunction: 'sha256',
    //   hashDigest: 'hex',
    //   hashDigestLength: 20,
    // }),
Julien Benchetrit's avatar
Julien Benchetrit committed
146 147 148
  ],

  performance: {
149
    assetFilter: assetFilename => !/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename),
Julien Benchetrit's avatar
Julien Benchetrit committed
150 151
  },
});