webpack.production.config.js 3.8 KB
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import HappyPack from 'happypack';
import os from 'os';

const rootPath = path.resolve(__dirname, '..');

const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });

module.exports = {
    entry: {
        app: [path.resolve(rootPath, 'src', 'index.js')]
    },
    output: {
        filename: '[name].[chunkhash].bundle.js',
        path: path.resolve(rootPath, 'build'),
        publicPath: '/'
    },
    module: {
        rules: [{
            test: /\.(js|jsx?)$/,
            use: 'happypack/loader?id=jsx',
            exclude: [
                path.resolve(rootPath, 'node_modules')
            ]
        }, {
            test: /\.less$/,
            use: 'happypack/loader?id=style'
        }, {
            test: /\.css$/,
            use: [
                MiniCssExtractPlugin.loader,
                {
                    loader: "css-loader"
                }]
        }]
    },
    devtool: false,//'source-map',//
    cache: true,
    mode: 'production',
    stats: {
        assets: true,
        assetsSort: 'chunks',
        children: false,
        colors: true,
        reasons: false,
        hash: false,
        env: true,
        version: true,
        timings: true,
        chunks: false,
        chunkGroups: false,
        chunkModules: false,
        chunkOrigins: false,
        modules: false,
        cached: false,
        moduleTrace: false,
        performance: true,
        warnings: false,
        cachedAssets: false
    },
    resolve: {
        alias: {
            src: path.resolve(rootPath, 'src')
        }
    },
    optimization: {
        minimize: false,
        minimizer: [
            new UglifyJsPlugin({
                parallel: true,
                uglifyOptions: {
                    compress: {
                        warnings: true,
                        drop_debugger: true,
                        drop_console: true
                    }
                }
            }),
            new OptimizeCSSAssetsPlugin({})
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            "window.evn": JSON.stringify('production')
        }),
        new HappyPack({
            id: 'jsx',
            threadPool: happyThreadPool,
            loaders: [{
                loader: "babel-loader",
                options: {
                    presets: ["env", "react", "stage-0"],
                    plugins: [["import", { "libraryName": "antd" }, "ant"]]
                }
            }]
        }),
        new HappyPack({
            id: 'style',
            threadPool: happyThreadPool,
            loaders: [{
                loader: "style-loader"
            }, {
                loader: "css-loader"
            }, {
                loader: "less-loader",
                options: {
                    paths: [
                        path.resolve(rootPath, "node_modules")
                    ]
                }
            }]
        }),
        new HtmlWebpackPlugin({
            title: '小爱科技',
            "filename": path.resolve(rootPath, 'build', 'index.html'),
            "inject": 'body',
            "template": path.resolve(rootPath, 'temp_build', 'index-production.html')
        }),
        new webpack.HashedModuleIdsPlugin({
            hashFunction: 'sha256',
            hashDigest: 'hex',
            hashDigestLength: 20
        }),
        new MiniCssExtractPlugin({
            filename: '[name].[contenthash].css'
        }),
        new webpack.DllReferencePlugin({
            context: '.',
            manifest: require('../temp_build/vendor.manifest.json')
        })
    ]
};