How to build minified and uncompressed bundle with webpack?

前端 未结 14 1129
囚心锁ツ
囚心锁ツ 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: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
    

提交回复
热议问题