webpack.prod.config.js 1.71 KB
const merge = require('webpack-merge');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJsParallelPlugin = require('webpack-uglify-parallel');
const os = require('os');
const pkg = require('../../package.json');
const path = require('path');
const distDir = path.join(__dirname, `../dist/${pkg.name}`);

let base = require('./webpack.base.config');

if (process.env.NODE_ENV === 'performance') {
    const Visualizer = require('webpack-visualizer-plugin');
    const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

    base = merge(base, {
        plugins: [
            new Visualizer({
                filename: '../../statistics.html'
            }),
            new BundleAnalyzerPlugin()
        ]
    });
}

if (process.env.npm_config_report) {
    let BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

    base.plugins.push(new BundleAnalyzerPlugin());
}

module.exports = merge(base, {
    output: {
        path: `${distDir}/${pkg.version}`
    },
    devtool: false,
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new UglifyJsParallelPlugin({
            workers: os.cpus().length,
            compress: {
                warnings: false
            },
            comments: false
        }),
        new CopyWebpackPlugin([
            { from: './public/img', to: `${distDir}/${pkg.version}/img` },
            { from: './public/font', to: `${distDir}/${pkg.version}/font` }
        ], {
            ignore: [
                'sprite/**'
            ],
            copyUnmodified: true
        })
    ]
});