My package.json file includes a version for my module, which ultimately get's compiled into an app.bundle.js file that I include in my web project. I would REALLY like to have the version number from the package.json file written to the app.bundle.js file as a comment right at the beginning of the file.
Is there a WebPack plugin to do this or a setting with WebPack itself?
The syntax is in JSON format where the key is the name of the package and the value is the version of the package to be used. npm uses the package. json file to specify the version of a package that your app depends on. The version number is in semver syntax which designates each section with different meaning.
We can use webpack as a value of one of the commands in our package. json file — without any flag. This way, webpack will assume your project's entry point file lives in the src directory. It will bundle the entry file and output it to the dist directory.
Webpack comes with a BannerPlugin that adds a banner to the top of each generated chunk.
You can require your package.json
and use it as any regular JavaScript object to get the version
field.
var PACKAGE = require('./package.json'); var version = PACKAGE.version;
Then use it to generate the desired banner string that will be used in the BannerPlugin
.
var PACKAGE = require('./package.json'); var banner = PACKAGE.name + ' - ' + PACKAGE.version; module.exports = { // Other stuff plugins: [ new webpack.BannerPlugin(banner) ] };
I have used it to add the version from the package.json
file and other info to the top of a library of my own. Check the webpack.config.js of this project for a working example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With