webpack

How can I access the hash or contenthash in my javascript code, not the html?


How can I access the [name] and [contenthash] within the my code itself.

I want my loader.js file to reference the other script's file name.

webpack.config.js:

module.exports = {
  entry: {
    app: './src/index.js',
    loader: './src/loader.js'
  },
  output: {
    filename: '[name].[contenthash].js',
    path: path.resolve(__dirname, 'dist')
  },
}

Solution

  • Add the ExtendedAPIPlugin

    new webpack.ExtendedAPIPlugin()
    

    __webpack_hash__ being a global, you can access everywhere.

    If using Webpack 5 the plug in is not required.