a087eb486a47ee0f90237388197c83800bdc6619ee6ff9168bcc0770c60374c8d87d3f3924cd9206d1de2a2ed212c4f255c37b1d71192f66c6eb4d3f65dbb4 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license
  3. * Copyright 2018 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. function isAsync (chunk) {
  18. if ('canBeInitial' in chunk) {
  19. return !chunk.canBeInitial()
  20. } else {
  21. return !chunk.isInitial()
  22. }
  23. }
  24. function getChunkEntryNames (chunk) {
  25. if ('groupsIterable' in chunk) {
  26. return Array.from(new Set(getNames(chunk.groupsIterable)))
  27. } else {
  28. return chunk.entrypoints.map(e => e.options.name)
  29. }
  30. }
  31. function getNames (groups, processed = new Set()) {
  32. const Entrypoint = require('webpack/lib/Entrypoint')
  33. const names = []
  34. for (const group of groups) {
  35. if (group instanceof Entrypoint) {
  36. // entrypoint
  37. if (group.options.name) {
  38. names.push(group.options.name)
  39. }
  40. } else if (!processed.has(group)) {
  41. processed.add(group)
  42. names.push(...getNames(group.parentsIterable, processed))
  43. }
  44. }
  45. return names
  46. }
  47. function extractChunks ({ compilation, optionsInclude }) {
  48. let includeChunks
  49. let includeType
  50. let includeEntryPoints
  51. if (optionsInclude && typeof optionsInclude === 'object') {
  52. includeType = optionsInclude.type
  53. includeChunks = optionsInclude.chunks
  54. includeEntryPoints = optionsInclude.entries
  55. } else {
  56. if (Array.isArray(optionsInclude)) {
  57. includeChunks = optionsInclude
  58. } else {
  59. includeType = optionsInclude
  60. }
  61. }
  62. let chunks = compilation.chunks
  63. if (Array.isArray(includeChunks)) {
  64. chunks = chunks.filter((chunk) => {
  65. return chunk.name && includeChunks.includes(chunk.name)
  66. })
  67. }
  68. if (Array.isArray(includeEntryPoints)) {
  69. chunks = chunks.filter(chunk => {
  70. const names = getChunkEntryNames(chunk)
  71. return names.some(name => includeEntryPoints.includes(name))
  72. })
  73. }
  74. // 'asyncChunks' are chunks intended for lazy/async loading usually generated as
  75. // part of code-splitting with import() or require.ensure(). By default, asyncChunks
  76. // get wired up using link rel=preload when using this plugin. This behaviour can be
  77. // configured to preload all types of chunks or just prefetch chunks as needed.
  78. if (includeType === undefined || includeType === 'asyncChunks') {
  79. return chunks.filter(isAsync)
  80. }
  81. if (includeType === 'initial') {
  82. return chunks.filter(chunk => !isAsync(chunk))
  83. }
  84. if (includeType === 'allChunks') {
  85. // Async chunks, vendor chunks, normal chunks.
  86. return chunks
  87. }
  88. if (includeType === 'allAssets') {
  89. // Every asset, regardless of which chunk it's in.
  90. // Wrap it in a single, "psuedo-chunk" return value.
  91. return [{ files: Object.keys(compilation.assets) }]
  92. }
  93. return chunks
  94. }
  95. module.exports = extractChunks