npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-version-file

v0.1.7

Published

Generates a file with your package name, version and build date

Downloads

43,540

Readme

webpack-version-file

Build Status Downloads Version License

This is a simple Webpack plugin which generates a file with your package name, version number, build date and any other details you might need. This is particularly useful as a way to know which version of your project is deployed at any given time.

Here's an example of an automatically generated version.txt file, which you can deploy next to your bundle file:

[email protected]
Build date: Mon Nov 28 2016 08:12:34 GMT+1100 (AEDT)

Installation

# npm
npm install --save-dev webpack-version-file

# yarn
yarn add --dev webpack-version-file

Setting up the plugin in your Webpack config file

Just include the module at the top of your webpack.config.js file and add a new entry to your plugins array:

const VersionFile = require('webpack-version-file');

module.exports = {
  entry: './src',
  ...
  plugins: [
    new VersionFile()
  ]
};

You can also pass in additional options:

const VersionFile = require('webpack-version-file');

module.exports = {
  entry: './src',
  ...
  plugins: [
    new VersionFile({
      output: './build/version.txt',
      package: './package.json'
    })
  ]
};

Available options are:

| Option | Description | |--------|-------------| | output | Path to the output file the plugin will generate. It defaults to ./version.txt. | | package | Path to the package.json file. It defaults to ./package.json. | | template | Path to the template file, e.g.: ./version.ejs. Has no default value. | | templateString | Defaults to <%= name %>@<%= version %>\nBuild date: <%= buildDate %> | | data | Object with additional data to be passed in to the template | | verbose | Log a success message to the terminal once the version file has been generated. false by default. |

Custom Data

By default, within your template you have access to all of the fields in your package.json with no extra configuration, e.g.:

  • version
  • name
  • license
  • author
  • repository.url
  • etc.
<%= name %>@<%= version %>
License: <%= license %>
Author: <%= author.name %> (<%= author.email %>)

However you can also pass in custom data when you add webpack-version-file to your list of plugins:

const VersionFile = require('webpack-version-file');

module.exports = {
  entry: './src',
  ...
  plugins: [
    new VersionFile({
      data: {
        date: new Date(),
        environment: process.env.NODE_ENV || 'development'
      }
    })
  ]
};

Once you've set your custom chunks of data, you can reference them in your template by using the same name you've given them:

<%= name %>@<%= version %>
Build date: <%= date %>
Environment: <%= environment %>

Note that in this example, the only two variables coming from your package.json file are name and version. date and environment are defined in your data object.

Predefined Variables

There's a single predefined variable you can make use of: buildDate (which is also part of the default template). The plugin itself is in charge of putting this variable into scope, and its value is generated using new Date().

<%= name %>@<%= version %>
Build date: <%= buildDate %>

Custom Templates

There are two ways in which you can define your own template:

  • using a template string
  • creating a template file

In either case, the template must be written using EJS which is a JavaScript templating language. Here's a sample template:

<%= name %>@<%= version %>
Build date: <%= buildDate %>
Comments: <%= comments %>

where name and version both come from the package.json file, buildTime is a variable injected by this library and comments is a custom variable set on the webpack.config.js file as part of the data object on your plugin definition.

This template can also be written inline in case you don't want an extra file on your project. The only difference is that you need to use the \n character instead of line breaks:

<%= name %>@<%= version %>\nBuild date: <%= buildTime %>\nComments: <%= comments %>

If you don't define a template altogether, it will default to:

<%= name %>@<%= version %>
Build date: <%= buildDate %>

Running the Example

Clone this repo, move to the example folder and download the dependencies:

cd example
npm install

To check how the plugin behaves with Webpack Dev Server, run:

npm start

To trigger the plugin when building the bundle with Webpack, run:

npm run build

Credits

This plugin was inspired by morficus/version-file.

License

MIT