Webpack生产环境和开发环境的区别

3

在我的Webpack配置中,我尝试为我的生产环境和开发环境创建不同的配置。我想要为每个任务生成不同的JS文件。也就是说,当我构建代码时,需要将其发送到名为“lib.js”的生产环境,并且当我运行开发环境时,我希望编译后的文件被放置在“dist”文件夹中。 这是我的webpack.config.js文件:

    const webpack = require('webpack')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
let path = require('path');
let config = {
    entry: [

        './src/index.js'
    ],
    module: {
        rules: [{
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: [
                    'react-hot-loader',
                    'babel-loader'
                ]
            },
            {
                test: /\.(css)$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader'

                })
            },
            {
                test: /\.less$/,
                use:ExtractTextPlugin.extract({
                    use: 'less-loader'
                })

            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file-loader',
              },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader', 'eslint-loader']
            }
        ],

    },
    resolve: {
        extensions: ['*', '.js', '.jsx', '.css'],
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
            }
        })
    ],
    devServer: {
        contentBase: './dist',
        historyApiFallback: true
    },
    devtool : "cheap-module-source-map",


}
if (process.env.NODE_ENV === 'production') {
    config.output= {
        path: path.join(__dirname, '/build/'),
        filename: 'lib.js',
        library: ['MyLibrary'],
        libraryTarget: 'umd'
    };
    config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
        new ExtractTextPlugin({
            filename: 'bundle.css',
            disable: false,
            allChunks: true
        }),
        new webpack.optimize.AggressiveMergingPlugin({
            minSizeReduce: 1,
            moveToParents: true

        })
    )

} else {
    config.output= {
        path: path.join(__dirname, '/dist/'),
        filename: 'bundle.js',
        library: ['MyLibrary'],
        libraryTarget: 'umd',
        publicPath: '/dist/'

    };
    config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
        new ExtractTextPlugin({ disable: true })
    )
}

module.exports = config

这是我的脚本:

  "scripts": {
"dev": "webpack-dev-server -d --env.platform=default --progress --hot",
"build": "set NODE_ENV=production && webpack -p --progress --hot"
  }

实际情况是,只有在我进行构建时,文件才会进入dist文件夹,尽管我已将NODE_ENV参数设置为"production"。 需要帮助,感谢!


@Prakashsharma 删除 && 实际上会完全停止构建。顺便说一下,我正在使用 SET 是因为我的机器是 Windows。 - Basilf
1个回答

1
可能是在 NODE_ENV=production 后面添加了空格,导致设置 NODE_ENVproduction 的值带有一个不匹配的空格,无法匹配你的 webpack 配置。尝试在 package.json 中像这样更改:
"build": "set NODE_ENV=production&& webpack -p --progress --hot"

这在 Stack Overflow 回答中 @daw 的评论中提到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接