【问题标题】:Webpack prod vs devWebpack 产品与开发
【发布时间】:2018-06-10 17:50:36
【问题描述】:

在我的 webpack 配置中,我尝试对我的 prod 和 dev 环境进行单独的配置。我正在尝试为每个任务实现不同的 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 参数设置为“生产”。 很高兴得到帮助。 谢谢!

【问题讨论】:

  • @Prakashsharma 删除 && 实际上完全停止了它的构建。顺便说一句,我正在使用 SET 因为 windows 机器

标签: node.js reactjs webpack


【解决方案1】:

它可能是NODE_ENV=production 之后的尾随空格,它可能将NODE_ENV 设置为production,后面的空格与webpack 配置中的空格不匹配。尝试像这样在 package.json 中更改它:

"build": "set NODE_ENV=production&& webpack -p --progress --hot"

@daw 在this SO answer 的评论中提到了这一点。

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2018-08-02
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多