2017-05-15 61 views
0

我旁邊Webpack.config.js:Webpack.config.js process.env.NODE_ENV not workinkg。 - ReactJS -

'use strict'; 

const WEBPACK = require('webpack'); 
const PATH = require('path'); 
const CopyFiles = require('copy-webpack-plugin'); 
const BaseName = "/upct"; 

const CONFIG = { 
    resolve: { 
     extensions: ['', '.js', '.jsx'] 
    }, 
    context: __dirname, 
    entry: { 
     app: ['./src/index.jsx'] 
    }, 
    /* 
    output: { 
     path: PATH.join(__dirname, '/public'), 
     /*path: './public',*//* 
     publicPath: BaseName+'/', 
     filename: 'index.js' 
    }, 
    /* 
    devServer: { 
     host: 'localhost', 
     port: 3000, 
     contentBase: PATH.join(__dirname, '/public'), 
     inline: true, 
     historyApiFallback: true, 
     headers: { 
      "Access-Control-Allow-Origin": "*" 
     } 
    }, 
    */ 
    module: { 
     loaders: [ 
      { 
       test: /(\.js|.jsx)$/, 
       loader: 'babel', 
       query: { 
        "presets": [ 
         "es2015", 
         "react", 
         "stage-0" 
        ], 
        "plugins": [ 
         "react-html-attrs", 
         "transform-decorators-legacy", 
         "transform-class-properties" 
        ] 
       } 
      } 
     ] 
    }, 
    plugins: [ 
     new WEBPACK.DefinePlugin({ 
      BASENAME: JSON.stringify(BaseName), 
      'process.env': { 
       /*'NODE_ENV': JSON.stringify(process.env.NODE_ENV)*/ 
       'NODE_ENV': JSON.stringify('production') 
      } 
     }) 
    ] 
} 

if (process.env.NODE_ENV === 'production') { 
    CONFIG.output = { 
     path: PATH.join(__dirname, '/publicProduction'), 
     publicPath: BaseName+'/', 
     filename: 'index.js' 
    }; 

    CONFIG.plugins.push(
     new WEBPACK.optimize.UglifyJsPlugin({ 
      beautify: false, 
      mangle: { 
       screw_ie8: true, 
       keep_fnames: true 
      }, 
      compress: { 
       screw_ie8: true, 
       warnings: false 
      }, 
      comments: false 
     }) 
    ); 
    //babelSettings.plugins.push("transform-react-inline-elements"); 
    //babelSettings.plugins.push("transform-react-constant-elements"); 

} else { 
    //CONFIG.devtool = "#cheap-module-source-map" 
    CONFIG.output = { 
     path: PATH.join(__dirname, '/publicDeveloping'), 
     publicPath: BaseName+'/', 
     filename: 'index.js' 
    }; 

    CONFIG.devServer = { 
     host: 'localhost', 
     port: 3000, 
     contentBase: PATH.join(__dirname, '/src'), 
     inline: true, 
     historyApiFallback: true, 
     headers: { 
      "Access-Control-Allow-Origin": "*" 
     } 
    } 
    /* 
    CONFIG.plugins.push(
     new webpack.HotModuleReplacementPlugin() 
    );*/ 
} 

module.exports = CONFIG; 

和明年腳本:

"scripts": { 
    "build": "SET NODE_ENV='building' && webpack --progress --watch --colors", 
    "serve": "SET NODE_ENV='testing' && webpack-dev-server --progress --colors", 
    "production": "SET NODE_ENV='production' && webpack --progress -p" 
    }, 

但從來沒有到IF(當我運行npm run production),一直到ELSE(運行我運行的腳本)。爲什麼會這樣? (上面我宣佈NODE_ENV ='生產',我不明白爲什麼它不工作...)。

謝謝。

+1

哪個操作系統是你的在工作? –

+0

的Windows 10.版本:' 「的WebPack」: 「^ 1.13.2」,'和' 「的WebPack-dev的服務器」: 「^ 1.15.2」' – JuMoGar

+0

嘗試' 「生產」:「SET NODE_ENV =生產& webpack --progress -p「' –

回答

1

我曾經有過同樣的問題。先嚐試修剪字符串,然後再比較它。

if (process.env.NODE_ENV.trim() === 'production') { 
    // Do Something 
} 

1分更多的東西,-p標誌webpackSET NODE_ENV=production基本上是相同的事情,所以我不認爲你需要他們。

+0

謝謝。但對我來說,'修剪()'沒有解決問題:( – JuMoGar

+0

嘗試刪除'-p'標誌,只是使用下面的腳本: '「生產」:「SET NODE_ENV =生產&&的WebPack」' –

+0

呀,我將其刪除我下一運行:'「生產」:「SET NODE_ENV =‘生產’&&的WebPack --progress」' – JuMoGar

1

你可能也想嘗試:如果你關心跨平臺 SET NODE_ENV=production webpack --progress

,你可能想嘗試npmcross-env

一旦你安裝包cross-env,你會改變你的腳本看起來像:

你的腳本將更改到cross-env NODE_ENV=production webpack --progress

+0

它也可以工作! :d – JuMoGar

0

只是另一個想法。 你可以試試這個,

"production": "webpack -p" 

和的WebPack配置文件

const PROD_ENV = process.argv.indexOf('-p'); 

,並使用三元條件

...PROD_ENV ? [prod settings] : [other settings] 

或使用,如果條件

if(PROD_ENV > 0) { 
    ...prod settings 
} else { 
    ...other settings 
} 
相關問題