0

I am using webpack 2 and added preprocessor in it. now I am passing NODE_ENV variable value from command line. in java script I am able to access that value but my preprocessor condition is getting failed please anyone let me know how to set argument value in preprocessor?

here is my config file

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/app.js",
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader!preprocess-loader?+DEBUG',
        // query: {
        //   presets: ['react', 'es2015'],
        //   plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
        // }
      },
      {
       test: /\.css$/,
       loader: "style-loader!css-loader!sass-loader"

    }, {
        test: /\.htm?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'preprocess-loader?+DEBUG',
     }

    ] 

    },
  output: {
    path: __dirname + "/src/",
    filename: "demo.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),

  ],
};

my app.js

//@if process.env.NODE_ENV !=='production'
console.log("hi this dev")
console.log("environment",process.env.NODE_ENV)
//@endif
console.log("this is working")

when I saw console it is showing production as value of process.env.NODE_ENV) but if is getting failed I checked with exclude condition it is working fine

command I used to run is npm start --env.NODE_ENV='production' please let me know how to access variable in pre processor?

EDIT 1: I mentioned I can get value but it is not being set in my preprocessor

EDIT 2: Now I am getting value as undefined I added from that answer like this way

"dev": "set NODE_ENV=dev && webpack-dev-server",
LowCool
  • 1,187
  • 5
  • 25
  • 51
  • Possible duplicate of [How to set Environment variables from within package.json \[Node.js\]](https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json-node-js) – Fahad Nisar Jul 25 '17 at 13:02
  • @FahadNisar: I tried that way m not able to access it in if condition of preprocessor. – LowCool Jul 25 '17 at 13:04
  • and in that way my console is showing value undefined – LowCool Jul 25 '17 at 13:09
  • 2
    hi finally I got my answer from this [SO](https://stackoverflow.com/questions/41705888/passing-the-node-env-value-using-webpack-via-defineplugin) post – LowCool Jul 26 '17 at 07:08

0 Answers0