get-module-chunk.js 979 B

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