get-module-chunk.js 904 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Find nearest module chunk (not sure that is reliable method, but who cares).
  3. * @see http://stackoverflow.com/questions/43202761/how-to-determine-all-module-chunks-in-webpack
  4. * @param {NormalModule} module
  5. * @param {NormalModule[]} modules - webpack 1 compat
  6. * @return {Chunk|null}
  7. */
  8. function getModuleChunk(module, modules) {
  9. let chunks;
  10. if (module.chunksIterable) {
  11. chunks = Array.from(module.chunksIterable);
  12. } else if (module.mapChunks) {
  13. chunks = module.mapChunks();
  14. } else {
  15. chunks = module.chunks;
  16. }
  17. // webpack 1 compat
  18. const issuer = typeof module.issuer === 'string'
  19. ? modules.find(m => m.request === module.issuer)
  20. : module.issuer;
  21. if (Array.isArray(chunks) && chunks.length > 0) {
  22. return chunks[chunks.length - 1];
  23. } else if (issuer) {
  24. return getModuleChunk(issuer, modules);
  25. }
  26. return null;
  27. }
  28. module.exports = getModuleChunk;