How can I inject a build number with webpack?

后端 未结 7 1032
野的像风
野的像风 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:53

    There is a plugin to auto inject version from package.json. It can inject it into HTML, CSS, JS as a comment, but also as a value by special tag webpack-auto-inject-version.

    How to:

    First of all, you have to add it to your project:

    npm i webpack-auto-inject-version
    

    Then you need to set up your webpack config:

    var WebpackAutoInject = require('webpack-auto-inject-version');
    
    module.exports = {
        plugins: [
            new WebpackAutoInject()
        ]
    }
    

    As you want to inject it into javascript, you should add a tag inside your javascript file ( which will be changed to version during the webpack compilation )

    var version = '[AIV]{version}[/AIV]';
    console.log(version);
    

    Auto increasing:

    You can set it up to auto increase the version directly from webpack by:

    webpack --other-webpack-settings --major
    
    webpack --other-webpack-settings -- minor
    
    webpack --other-webpack-settings --patch
    

    Where --other-webpack-settings is equal to your custom line args. Simplifying - you need to att --major, --minor or --patch whenever you want to auto increase a version.

提交回复
热议问题