How can I inject a build number with webpack?

后端 未结 7 962
野的像风
野的像风 2021-01-30 10:38

I\'d like to inject a build number and version information to my project as it\'s built with webpack. For example, so that my code can do something like:

var bui         


        
7条回答
  •  借酒劲吻你
    2021-01-30 10:48

    I would do more simpler, just use npm version patch (npm-version) (no plugin required)

    package.json (Example path version before building)

    {
      "version": "1.0.0",
      "scripts": {
        "build": "npm version patch && node build/build.js"
      }
    }
    

    So when you run npm run build this will patch the version (1.0.0 to 1.0.1 in your package.json)


    Bonus: You can also add this to your config (example config/prod.env.js)

    'use strict'
    const pkg = require('./package.json')
    module.exports = {
      NODE_ENV: '"production"',
      VERSION: pkg.version
    }
    

    Then you can use process.env.VERSION anywhere in your our JS

    Updated: Or just use process.env.npm_package_version without required to include package.json

提交回复
热议问题