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-git-hash-updater

v1.0.2

Published

Webpack v5 plugin for complex git hash update

Downloads

346

Readme

webpack-git-hash-updater

image

It requires webpack version 5!

If you need to use webpack v4 please use this script here.

This is a Webpack v5 for solving problem of caching the same content served by the webpack bundle, by altering the githash of the js files from your HTML files.

How to install

npm install webpack-git-hash-updater

Simple example

Your HTML files are in the /static/ folder


const
  fs = require("fs"),
  colors = require("cli-color"),
  webpack = require("webpack"),

  WebpackGitHashUpdater = require("webpack-git-hash-updater"),

  entries = [
    {
      name : "game",
      path : "Game/entryPoint",
    },
    {
      name : "console",
      path : "Console/entryPoint",
    },
  ],

  gitHashPlugin = new WebpackGitHashUpdater({
    cleanup  : true,
    callback : (hash) => entries.map(({ name }) => {

      const filename = `${indexFolderPath}/${name}.html`;

      return fs.readFile(filename, "utf8", (errRead, data) => {

        if (errRead) {
          return console.log(errRead);
        }

        const
          reg = new RegExp(`src="\/static\/${name}-\\w+.js"`, "u"),
          newData = data.replace(
            reg,
            `src="/static/${name}-${hash}.js"`,
          );

        return fs.writeFile(filename, newData, (errWrite) => {
          if (errWrite) {
            return console.log(errWrite);
          }

          WebpackGitHash5.log(
            `File ${colors.cyanBright(filename)} updated. hash: ${colors.cyanBright(hash)}`,
          );
          return null;
        });
      });
    }),
  });

 // ... bla bla bla other things
  
  webpackConfiguration = {
    // .... other things
    plugins: isDevelopmentMode ? [
		gitHashPlugin,
	] : []
	// ... other things
  };

Why?

Webpack already gives you the [hash] placeholder, which serves exactly this purpose. This plugin has a couple advantages:

  1. Using the Git hash in the filename allows you to very quickly pinpoint where in the commit history to look for any issues you encounter on a production site.
  2. The plugin can automatically delete old versions of the files it affects. Depending on how you manage static assets, this might be useful.
  3. You can specify the length of the hash. If you have some crazy restriction on the number of characters in your file names, maybe this will help.

How it works ?

Use the placeholder [githash] in your config.

var WebpackGitHash = require('webpack-git-hash-updater');

module.exports = {
	output: {
		filename: 'bundle.[githash].js',
		chunkFilename: '[name]-chunk.[githash].js'
	},
	plugins: [
		new WebpackGitHash()
	]
}

Options

You can pass these options when you instantiate the plugin in your plugins array:

placeholder

Defaults to [githash]. Pass another string to use as the placeholder in filenames.

cleanup

Defaults to false. Pass true to delete old versions after Webpack is finished. This works by searching for files in the output directory that match the same pattern as the files that were just compiled, except for the hash. Note the the number of characters in the hash must match. For example:

bundle.gha3k8d.js -> the newly compiled bundle
bundle.lk8adsm.js -> would be deleted
bundle.987aas880m.js -> would *NOT* be deleted

callback

Optional callback function that runs on Webpack's done step. It receives three arguments:

  1. hash The hash used as the latest version by the plugin (not Webpack's [hash]).
  2. deletedFiles Array of any filenames (without path) deleted during cleanup; this might be an empty array.
  3. stats Webpack stats object from the latest compilation

skipHash

Defaults to hash of most recent Git commit on the current branch. This is the unique string that will be used as the version identifier, and will be "skipped" if the plugin is set to delete old versions when Webpack is finished. See cleanup above.

hashLength

Defaults to 7 characters, which is Git's default for a "short" hash. hashLength can be specified only when skipHash is not specified. If skipHash is specified, then hashLength is set to the number of characters in skipHash.

outputPath

Defaults to output.path in your Webpack config. You can change that here though; an absolute path is recommended since that's what Webpack tends to use.

regex

If you use the cleanup option to delete old verions, the plugin attempts to create regular expressions to match the filenames, based on the original Webpack config. For instance:

[name]-chunk.[githash].min.js -> the config's output.chunkFilename
abcd123 -> the latest Git hash
/\w+-chunk\.(?!abcd123)\w{7}\.min\.js/ -> the default regex
global-chunk.1234abc.min.js -> this would be deleted

If the default regex isn't working for you, you can specify a new RegExp in regex.filename and/or regex.chunkFilename. Note that there's not (yet) a way to dynamically skip the current Git hash (the (?!abcd123) part in the example). So if you use this option, you'll need to use the skipHash option also.

Post-compilation updates

Here's a simple example of how to use the callback option to edit a <script> tag to load the load the latest versionof a file.

module.exports = {
	plugins: [
		new WebpackGitHash({
			cleanup: true,
			callback: function(versionHash) {
				var indexHtml = fs.readFileSync('./index.html', 'utf8');
				indexHtml = indexHtml.replace(/src="\/static\/app-bundle\.\w+\.js/, 'src="/static/app-bundle.' + versionHash + '.js');
				fs.writeFileSync('./index.html', indexHtml);
			}
		})
	]
}

Acknowledgment

This script was inspired by v4 original script