美文网首页
webpack 自动过滤console配置参数

webpack 自动过滤console配置参数

作者: 无名程序猿 | 来源:发表于2019-09-26 14:30 被阅读0次
    • 代码
    // 关键代码
    const TerserPlugin = require('terser-webpack-plugin');
    compress: {   
      drop_console: true
    }
    // webpack.prod.js
    const path = require('path');
    const webpack = require('webpack');
    const merge = require('webpack-merge');
    const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
    const TerserPlugin = require('terser-webpack-plugin');
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
    const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
      .BundleAnalyzerPlugin;
    const common = require('./webpack.common.js');
    const themes = require('./themes.js');
    const publicPath = 'newweb/';
    module.exports = merge(common, {
      mode: 'production',
      devtool: 'cheap-module-source-map',
      output: {
        publicPath: publicPath
      },
      optimization: {
        minimizer: [
          new TerserPlugin({
            cache: true,
            parallel: true,
            sourceMap: true,
            terserOptions: {
              compress: {
              // 关键代码
                drop_console: true
              }
            }
          })
        ],
        splitChunks: {
          cacheGroups: {
            styles: {
              name: 'styles',
              test: /\.(less|css)$/,
              chunks: 'all',
              enforce: true
            }
          }
        }
      },
      module: {
        rules: [
          {
            test: /\.(sa|sc|le)ss$/,
            use: [
              {
                loader: MiniCssExtractPlugin.loader,
                options: {
                  publicPath: '../'
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: true
                }
              },
              {
                loader: 'less-loader',
                options: {
                  sourceMap: true,
                  modifyVars: themes,
                  javascriptEnabled: true
                }
              }
            ]
          },
          {
            test: /\.css$/,
            use: [
              {
                loader: MiniCssExtractPlugin.loader,
                options: {
                  publicPath: '../'
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: true
                }
              }
            ]
          }
        ]
      },
      plugins: [
        new CleanWebpackPlugin(['dist']),
        // new BundleAnalyzerPlugin(),
        new webpack.optimize.MinChunkSizePlugin({
          minChunkSize: 10000 // Minimum number of characters
        }),
        new webpack.HashedModuleIdsPlugin(),
        new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new AddAssetHtmlPlugin({
          filepath: path.resolve(__dirname, './dll/vendors.*.js'),
          outputPath: 'js',
          publicPath: publicPath + 'js',
          includeSourcemap: false
        }),
        new MiniCssExtractPlugin({
          filename: 'css/[name].[hash:5].css',
          chunkFilename: 'css/[id].[hash:5].css'
        }),
        // new webpack.DllReferencePlugin({
        //   context: __dirname,
        //   manifest: require('./dll/manifest.json')
        // }),
        new webpack.DefinePlugin({
          'process.env.NODE_ENV': JSON.stringify('production')
        })
      ]
    });
    
    
    image.png

    相关文章

      网友评论

          本文标题:webpack 自动过滤console配置参数

          本文链接:https://www.haomeiwen.com/subject/txzlyctx.html