Passing environment-dependent variables in webpack

后端 未结 15 2240
醉酒成梦
醉酒成梦 2020-11-22 12:46

I\'m trying to convert an angular app from gulp to webpack. in gulp I use gulp-preprocess to replace some variables in the html page (e.g. database name) depending on the NO

15条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 13:29

    Just another answer that is similar to @zer0chain's answer. However, with one distinction.

    Setting webpack -p is sufficient.

    It is the same as:

    --define process.env.NODE_ENV="production"
    

    And this is the same as

    // webpack.config.js
    const webpack = require('webpack');
    
    module.exports = {
      //...
    
      plugins:[
        new webpack.DefinePlugin({
          'process.env.NODE_ENV': JSON.stringify('production')
        })
      ]
    };
    

    So you may only need something like this in package.json Node file:

    {
      "name": "projectname",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "debug": "webpack -d",
        "production": "webpack -p"
      },
      "author": "prosti",
      "license": "ISC",
      "dependencies": {    
        "webpack": "^2.2.1",
        ...
      }
    }
    

    Just a few tips from the DefinePlugin:

    The DefinePlugin allows you to create global constants which can be configured at compile time. This can be useful for allowing different behavior between development builds and release builds. For example, you might use a global constant to determine whether logging takes place; perhaps you perform logging in your development build but not in the release build. That's the sort of scenario the DefinePlugin facilitates.


    That this is so you can check if you type webpack --help

    Config options:
      --config  Path to the config file
                             [string] [default: webpack.config.js or webpackfile.js]
      --env     Enviroment passed to the config, when it is a function
    
    Basic options:
      --context    The root directory for resolving entry point and stats
                                           [string] [default: The current directory]
      --entry      The entry point                                          [string]
      --watch, -w  Watch the filesystem for changes                        [boolean]
      --debug      Switch loaders to debug mode                            [boolean]
      --devtool    Enable devtool for better debugging experience (Example:
                   --devtool eval-cheap-module-source-map)                  [string]
      -d           shortcut for --debug --devtool eval-cheap-module-source-map
                   --output-pathinfo                                       [boolean]
      -p           shortcut for --optimize-minimize --define
                   process.env.NODE_ENV="production" 
    
                          [boolean]
      --progress   Print compilation progress in percentage                [boolean]
    

提交回复
热议问题