webpack: output.filename not working if function

Bug report

What is the current behavior?

When I use a function for output.filename it doesn’t work for my dynamic imports: they get emitted as 1.js, 2.js etc. with identical chunk value (whereas chunk name is correct)

In addition things get worse when I set optimization.runtime to single what results in even the entries being affected (only runtime.js is correct then).

The same filename code as string works without problems and emits foo.js, bar.js etc.

// working
filename: isProd ? '[name].[chunkhash].js' : '[name].js',

// not working
filename: data => isProd ? '[name].[chunkhash].js' : '[name].js',

If the current behavior is a bug, please provide the steps to reproduce.

  1. create app.js with dynamic imports
// app.js
console.log('[entry] app');
import(/* webpackChunkName: 'foo' */ './foo').then(mod => console.log(mod.default));
import(/* webpackChunkName: 'bar' */ './bar').then(mod => console.log(mod.default));

// foo.js
export default '[dynamic] foo';

// bar.js
export default '[dynamic] bar';
  1. change output.filename to function and run webpack via node api

What is the expected behavior?

I expect no difference when filename function returns the same string I’ve used for filename string before.

Other relevant information: webpack version: 4.29.6 Node.js version: 10.15.0 Operating System: macOS Mojave (10.14) Additional tools: -

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 18 (10 by maintainers)

Most upvoted comments

@SassNinja i think we can make chunkFilename as Function

Any update on this?

@sokra would be awesome if you can clarify things a bit (why chunkFilename can’t be a function)

Only the runtime chunk is affected by output.filename. The other chunks are affected by ouput.chunkFilename.

If output.filename is a string, output.chunkFilename defaults to the same string. If it’s a function output.chunkFilename defaults to [id].js. You cannot provide a function to output.chunkFilename.