How can I get the entry file name in the loaders?

If I have a entry hello.js like this:

require('./images/test.png');

and I use file-loader to handle this, then I want the built image named as “hello.test.png”,how can I make it?

The following example can not fill my need:

{
  test: /\.(jpe?g|png|gif|svg)$/i,
  loaders: ['file?hash=sha512&digest=hex&name=[path][hash].[ext]']
}

Author: Fantashit

5 thoughts on “How can I get the entry file name in the loaders?

  1. The only way, I found was to explore the reasons of the module directly in the loader.

    var findEntry = function( mod ) {
      if (mod.reasons.length > 0) {
        return findEntry( mod.reasons[0].module )
      }
      return mod.resource
    }
    
    module.exports = function MyLoader() {
      var entry = findEntry( this._module )
      ...
    }
    
  2. Thanks FuriouZz, here’s a small modification to make it work with HMR as well:

    function findEntry(mod) {
        if (mod.reasons.length > 0 && mod.reasons[0].module.resource) {
            return findEntry(mod.reasons[0].module)
        }
        return mod.resource;
    }
  3. This issue had no activity for at least half a year.

    It’s subject to automatic issue closing if there is no activity in the next 15 days.

Comments are closed.