How to build minified and uncompressed bundle with webpack?

前端 未结 14 1055
囚心锁ツ
囚心锁ツ 2020-11-29 14:52

Here\'s my webpack.config.js

var webpack = require(\"webpack\");

module.exports = {

  entry: \"./entry.js\",
  devtool: \"source-map\",
  outp         


        
相关标签:
14条回答
  • 2020-11-29 15:25

    You can create two configs for webpack, one that minifies the code and one that doesn't (just remove the optimize.UglifyJSPlugin line) and then run both configurations at the same time $ webpack && webpack --config webpack.config.min.js

    0 讨论(0)
  • 2020-11-29 15:26

    webpack entry.jsx ./output.js -p

    works for me, with -p flag.

    0 讨论(0)
  • 2020-11-29 15:27

    You should export an array like this:

    const path = require('path');
    const webpack = require('webpack');
    
    const libName = 'YourLibraryName';
    
    function getConfig(env) {
      const config = {
        mode: env,
        output: {
          path: path.resolve('dist'),
          library: libName,
          libraryTarget: 'umd',
          filename: env === 'production' ? `${libName}.min.js` : `${libName}.js`
        },
        target: 'web',
        .... your shared options ...
      };
    
      return config;
    }
    
    module.exports = [
      getConfig('development'),
      getConfig('production'),
    ];
    
    0 讨论(0)
  • 2020-11-29 15:27

    You can define two entry points in your webpack configuration, one for your normal js and the other one for minified js. Then you should output your bundle with its name, and configure UglifyJS plugin to include min.js files. See the example webpack configuration for more details:

    module.exports = {
     entry: {
       'bundle': './src/index.js',
       'bundle.min': './src/index.js',
     },
    
     output: {
       path: path.resolve(__dirname, 'dist'),
       filename: "[name].js"
     },
    
     plugins: [
       new webpack.optimize.UglifyJsPlugin({
          include: /\.min\.js$/,
          minimize: true
       })
     ]
    };
    

    After running webpack, you will get bundle.js and bundle.min.js in your dist folder, no need for extra plugin.

    0 讨论(0)
  • 2020-11-29 15:32

    You can use a single config file, and include the UglifyJS plugin conditionally using an environment variable:

    const webpack = require('webpack');
    const TerserPlugin = require('terser-webpack-plugin');
    
    const PROD = JSON.parse(process.env.PROD_ENV || '0');
    
    module.exports = {
    
      entry: './entry.js',
      devtool: 'source-map',
      output: {
        path: './dist',
        filename: PROD ? 'bundle.min.js' : 'bundle.js'
      },
      optimization: {
        minimize: PROD,
        minimizer: [
          new TerserPlugin({ parallel: true })
      ]
    };
    

    and then just set this variable when you want to minify it:

    $ PROD_ENV=1 webpack
    

    Edit:

    As mentioned in the comments, NODE_ENV is generally used (by convention) to state whether a particular environment is a production or a development environment. To check it, you can also set const PROD = (process.env.NODE_ENV === 'production'), and continue normally.

    0 讨论(0)
  • 2020-11-29 15:32

    I had the same issue, and had to satisfy all these requirements:

    • Minified + Non minified version (as in the question)
    • ES6
    • Cross platform (Windows + Linux).

    I finally solved it as follows:

    webpack.config.js:

    const path = require('path');
    const MinifyPlugin = require("babel-minify-webpack-plugin");
    
    module.exports = getConfiguration;
    
    function getConfiguration(env) {
        var outFile;
        var plugins = [];
        if (env === 'prod') {
            outFile = 'mylib.dev';
            plugins.push(new MinifyPlugin());
        } else {
            if (env !== 'dev') {
                console.log('Unknown env ' + env + '. Defaults to dev');
            }
            outFile = 'mylib.dev.debug';
        }
    
        var entry = {};
        entry[outFile] = './src/mylib-entry.js';
    
        return {
            entry: entry,
            plugins: plugins,
            output: {
                filename: '[name].js',
                path: __dirname
            }
        };
    }
    

    package.json:

    {
        "name": "mylib.js",
        ...
        "scripts": {
            "build": "npm-run-all webpack-prod webpack-dev",
            "webpack-prod": "npx webpack --env=prod",
            "webpack-dev": "npx webpack --env=dev"
        },
        "devDependencies": {
            ...
            "babel-minify-webpack-plugin": "^0.2.0",
            "npm-run-all": "^4.1.2",
            "webpack": "^3.10.0"
        }
    }
    

    Then I can build by (Don't forget to npm install before):

    npm run-script build
    
    0 讨论(0)
提交回复
热议问题