How to get terser webpack plugin works properly

夙愿已清 提交于 2019-12-12 09:13:15

问题


I am trying to understand how to get terser webpack plugin to work in the most simple possible case.

This is my code:

src/math.js, exports 2 functions.

export const double = (x) =>
  x + x;

export const triple = (x) =>
  x + x + x;

scr/index.js, imports only double function.

import { double } from './math.js';
console.log(double(10));

I understood from webpack documentation that I must use ModuleConcatenationPlugin if I want activate tree shaking.

This is my webpack config:

webpack/index.js

const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  mode: 'none',
  module: {
    rules: [
        {
            test: /\.js$/,
            loader: 'babel-loader',
            options: {
                presets: [
                    ['@babel/preset-env', {
                        modules: false
                    }]
                ],
                babelrc: false
            }
        }
    ]
  },
  plugins: [
      new webpack.optimize.ModuleConcatenationPlugin()
  ],
  optimization: {
      minimizer: [new TerserPlugin({})],
  }
};

I don't understand what I missed in my webpack config. The webpack output is without minification or tree shaking...


node: "v10.10.0"

webpack: "^4.28.1"

webpack-cli: "^3.2.1"

@babel/core: "^7.2.2"

@babel/preset-env: "^7.1.5"

babel-loader: "^8.0.5"

terser-webpack-plugin: "^1.2.1"


回答1:


Seeing that you have mode set to none, optimization.minimize is set to false, so your custom instance of Terser is never instantiated.

Add minimize: true to the optimization object and it should work.



来源:https://stackoverflow.com/questions/54131749/how-to-get-terser-webpack-plugin-works-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!