Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I write the package.json version to my WebPack bundle?

Tags:

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?

like image 700
SomethingOn Avatar asked Jan 14 '16 19:01

SomethingOn


People also ask

What is version in package json?

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.

Which package is used to add the initial packages of webpack?

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.


1 Answers

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.

webpack.config.js

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.

like image 147
dreyescat Avatar answered Sep 27 '22 22:09

dreyescat