question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File path info in chunkFilename function when optimization.chunkIds is deterministic

See original GitHub issue

Feature request

What is the expected behavior? I am using the output.chunkFilename function to generate friendly readable chunk filenames based on their source path. This works well if optimization.chunkIds is set to named. However, this leaves big long ids in the source code references (e.g. “src_web_app_components_home_index_js”). Ideally, I’d like to leave optimization.chunkIds set to deterministic (the default in a production build), so the chunk ids are small numbers. The issue is that the chunk info provided to the chunkFilename function no longer has any identifiable information about the file paths to generate the filenames anymore.

What is motivation or use case for adding/changing the behavior? Automatic readable filenames with smaller code.

How should this be implemented in your opinion? Include more information to the chunkFilename regardless of the optimization.chunkIds setting.

Are you willing to work on this yourself? yes

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
gpoitchcommented, Nov 7, 2021

@jamesarosen This was my final solution. You’ll have to adjust it for your directory structure but you should have all the pieces there.

class AutoChunkNamePlugin {
  apply(compiler) {
    compiler.hooks.compilation.tap('AutoChunkNamePlugin', (compilation) => {
      compilation.hooks.chunkIds.tap('AutoChunkNamePlugin', (chunks) => {
        chunks.forEach((chunk) => {
          if (!chunk.name) {
            const chunkModule = compilation.chunkGraph.getChunkRootModules(chunk)[0]
            const rootModule = (chunkModule && chunkModule.rootModule) || chunkModule
            const rootPath = rootModule && rootModule.userRequest
            const name = rootPath && rootPath.split(path.sep).slice(-2)[0]
            if (name) chunk.name = name
          }
        })
      })
    })
  }
}
1reaction
gpoitchcommented, Jun 29, 2021

I’m having some success simply applying both plugins. Not sure of any unintended side-effects yet:

const { DeterministicChunkIdsPlugin, NamedChunkIdsPlugin } = require('webpack').ids

class NamedDeterministicChunkIdsPlugin {
  apply(compiler) {
    // Apply named chunks plugin (assigns name to chunk.id)
    new NamedChunkIdsPlugin().apply(compiler)

    compiler.hooks.compilation.tap('NamedDeterministicChunkIdsPlugin', (compilation) => {
      compilation.hooks.chunkIds.tap('NamedDeterministicChunkIdsPlugin', (chunks) => {
        chunks.forEach((chunk) => {
          chunk.name = chunk.id // Store the name on another field
          chunk.id = null // null out id so the next plugin will reassign it
        })
      })
    })

    // Apply deterministic chunks plugin (reassigns chunk.id to a short numeric id)
    new DeterministicChunkIdsPlugin().apply(compiler)
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimization - webpack
chunkIds is set to 'named' , while in production it is set to 'deterministic' if none of the above, optimization.
Read more >
webpack-cli/OPTIONS.md at master - GitHub
Provide path to a webpack configuration file e.g. . ... --cache-profile Track and log detailed timing information for individual cache items.
Read more >
Webpack 5 {output: chunkFilename} not working
In my webpack config, I use single-file entry & the splitChunks option to separate the game code from the libraries I am using....
Read more >
Webpack config for better monitoring - RelativeCI
Webpack config for better monitoring · Use a common pattern for filename content hashes · Name asynchronous chunks · Use deterministic chunkIds (webpack...
Read more >
Adding Hashes to Filenames - SurviveJS
These strings are used to attach specific information to webpack output. The most valuable ones are: [id] - Returns the chunk id. [path]...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found