Webpack definePlugin 未设置/读取 node_env 变量 [英] Webpack definePlugin not setting/reading node_env variable

查看:46
本文介绍了Webpack definePlugin 未设置/读取 node_env 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 Webpack 的 definePlugin 将 node_env var 注入我的代码时遇到了真正的问题,在阅读了大量帖子后,似乎没有任何效果.我有一种感觉,我错过了一些东西...

I'm having real issues injecting the node_env var into my code through Webpack's definePlugin and after reading numerous posts nothing seems to work. I have a feeling I'm missing something...

所以我的生产 webpack 配置是 -

So my production webpack config is -

// Config
import path from 'path';
import webpack from 'webpack';
import config from './config';


/**
 * Webpack config for compiling the
 * React/Redux/ES6 scripts.
 *
 * ENV = production
 *
 * @type {Object}
 */
module.exports = {
    entry: path.resolve(__dirname, '../', config.assets.scripts.src_dir, config.assets.scripts.entry),
    devtool: false,
    output: {
        path: path.resolve(__dirname, config.assets.scripts.dist_dir),
        filename: config.assets.scripts.dist_min
    },
    module: {
        loaders: [
            {
                test: /.js?$/,
                loader: 'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0',
                exclude: /node_modules/
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        }),
        new webpack.optimize.UglifyJsPlugin({})
    ]
};

我已经尝试为 react、react-dom 和 react-redux 设置别名,但完全没有用,React 仍然警告我我正在使用 node_env=production 之外的缩小版本(而且 redux 仍然抛出这个错误).

I've tried setting alias's for react, react-dom and react-redux with absolutely no avail, React still warns me i'm using a minified version outside of node_env=production (Also redux still throws me this error).

仅供参考,我使用的是 webpack 2.2.1 版.

FYI, i'm using webpack version 2.2.1.

这与 babel-loader 以某种方式冲突有关吗?如果有人能指出我正确的方向,那就太好了.

Is it something to do with the babel-loader conflicting in some way? If anyone can point me in the right direction that would be great.

推荐答案

尝试将 NODE_ENVDefinePlugin 更改为这个 -

Try changing your DefinePlugin for NODE_ENV to this one -

plugins: [
    new webpack.DefinePlugin({
        'process.env.NODE_ENV' : JSON.stringify('production')
    }),
    new webpack.optimize.UglifyJsPlugin({
        minimize : true,
        compress : {
            warnings : false
        }
    })
]

这篇关于Webpack definePlugin 未设置/读取 node_env 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆