How to use ChunkGraph API in webpack 5. splitchunks.cacheGroup?

2.7k views Asked by At

In webpack4:

splitChunks: {  
  cacheGroups: {
    a: {
      test: (module, chunks)=>{
        chunks.every(( chunk ) => { chunk.name === 'xxx' })
      }
    }
  },
}

In webpack 5:

splitChunks: {
  cacheGroups: {
    a: {
      test: (module, {moduleGraph, chunkGraph})=>{
        // how to use chunkGraph? 
        // Which is the equivalent of `chunks.eveny(chunk => chunk.name === 'xxx')`
      }
    }
  },
}

How to use ChunkGraph API? I try to use in this way,

return chunkGraph.getChunkModules(module).every((chunk) => {
  return new RegExp(`^${root}\\/`).test(chunk.name);
});

But chunkGraph.getChunkModules(module).length === 0 is true.

I got this link

1

There are 1 answers

0
Cauê Thenório On

The correct chunkGraph method name is getModuleChunks:

{
  splitChunks: {
    cacheGroups: {
      a: {
        test: (module, {chunkGraph})=> {
          return chunkGraph.getModuleChunks(module).every(chunk => chunk.name 
  === 'xxx');
        }
      }
    }
  }
}

The method source-code is available in the link you provided: https://github.com/webpack/webpack/blob/a16909c5fd/lib/ChunkGraph.js#L474