How to fix deprecation warning for Chunk.modulesIterable?
Asked Answered
J

3

7

I am the maintainer of external-svg-sprite-loader and I noticed that when using it with webpack 5 I get the following warning:

[DEP_WEBPACK_CHUNK_MODULES_ITERABLE] DeprecationWarning: Chunk.modulesIterable: Use new ChunkGraph API

The build passes but I would like to able to fix this deprecation warning. However, I can't find any documentation about modulesIterable or the ChunkGraph API. Where should I look for it and what would be a potential solution for this issue?

Jodoin answered 19/10, 2020 at 11:40 Comment(0)
J
1

I had this issue because I was using compilation.hooks.optimizeChunks to iterate through all modules in the compilation. After some research, I found that I could use compilation.hooks.optimizeModules to achieve the same result. Therefore, I switched to the latter and stopped getting the deprecation warning mentioned in my question. I would say that this is a better solution than iterating through the modules in a chunk since webpack seems to already be doing that internally anyway.

Jodoin answered 14/11, 2020 at 13:35 Comment(2)
Care to share where you found this information? There doesn't appear to be much documentation available for the ChunkGraph API, yet.Roebuck
No, there isn't much documention. AFAIK all there is available is this page: webpack.js.org/api/compilation-hooks.Jodoin
G
6

If you want to iterate through modules in chunk, you could do it like this:

compilation.chunks.forEach(chunk => {
  compilation.chunkGraph.getChunkModules(chunk).forEach((module) => {
    // module is available here
  })
});

You could find other useful methods of this class in its source code. If you want to manipulate modules in chunk, probably it's better to use ModuleGraph class. For example:

compilation.chunks.forEach(chunk => {
  compilation.chunkGraph.getChunkModules(chunk).forEach((module) => {
    const exportInfo = compilation.chunkGraph.moduleGraph.getExportInfo(module);
  })
});
Germanous answered 12/11, 2020 at 18:38 Comment(0)
J
1

I had this issue because I was using compilation.hooks.optimizeChunks to iterate through all modules in the compilation. After some research, I found that I could use compilation.hooks.optimizeModules to achieve the same result. Therefore, I switched to the latter and stopped getting the deprecation warning mentioned in my question. I would say that this is a better solution than iterating through the modules in a chunk since webpack seems to already be doing that internally anyway.

Jodoin answered 14/11, 2020 at 13:35 Comment(2)
Care to share where you found this information? There doesn't appear to be much documentation available for the ChunkGraph API, yet.Roebuck
No, there isn't much documention. AFAIK all there is available is this page: webpack.js.org/api/compilation-hooks.Jodoin
C
0

external-svg-sprite-loader is using Chunk.modulesIterable (ref) which is now deprecated and is being replaced with ChunkGraph

The package has a peer dependency of webpack^4.1.0 (ref), so you can either downgrade your Webpack to 4.* to remove the warning or ask the package maintainer to add support for Webpack 5.

You can find the official deprecation note here

Cannes answered 28/10, 2020 at 9:51 Comment(1)
sorry I guess I didn't explain myself correctly in my question. I am the package maintainer and would like to know how to fix this issue since I can't find any documentation for ChunkGraph .Jodoin

© 2022 - 2024 — McMap. All rights reserved.