1

I'm using webpack (V 1.13.2) to bundle my webapp + ReactJS.

I'd like to pass the current commit SHA to the js.

Getting the current commit SHA is straightforward, but I couldn't figure out how to pass it the to js file.
I tried to change the output js file and chain the commit SHA to it:

output:{
        path: distPath,
        publicPath: '/',
        filename: "client.min.js?" + COMMIT_SHA
      },

But the webpack won't bundle it.

I also tried to use string-replace-loader like the following:

webpack.config.js:
{
        test: /\.jsx?$/,
        loader: 'string-replace-loader',
        query: {
          search: 'COMMIT_SHA_ANCHOR',
          replace:  VERSION
        }
      },

client.js:

var commit = 'COMMIT_SHA_ANCHOR';

but I get a syntax error (it creates a new line before closing the string):

Unterminated string constant (34:13)

var commit = '4820fa5de22d3463a0ca39c1d4067a62800d1d07
     |              ^
  35 | ';

What is the best way (if exists) to pass the commit SHA to js?

Community
  • 1
  • 1
Sharon Haim Pour
  • 6,595
  • 12
  • 42
  • 64

1 Answers1

1

The DefinePlugin should do what you want.

In your webpack.config.js:

plugins: [
  new webpack.DefinePlugin({COMMIT_SHA: JSON.stringify(VERSION)})
]

This will create a global variable COMMIT_SHA that you can refer to anywhere:

console.log(COMMIT_SHA);

You should also verify that the VERSION string you're using doesn't already contain the newline at the end. Try adding console.log(JSON.stringify(VERSION)) to your webpack config and see if it includes \n.

also
  • 974
  • 7
  • 21