ArrayPushCallbackChunkFormatPlugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource, PrefixSource, RawSource } = require("webpack-sources");
  7. const { RuntimeGlobals } = require("..");
  8. const HotUpdateChunk = require("../HotUpdateChunk");
  9. const Template = require("../Template");
  10. const { getCompilationHooks } = require("./JavascriptModulesPlugin");
  11. const {
  12. generateEntryStartup,
  13. updateHashForEntryStartup
  14. } = require("./StartupHelpers");
  15. /** @typedef {import("../Compiler")} Compiler */
  16. class ArrayPushCallbackChunkFormatPlugin {
  17. /**
  18. * Apply the plugin
  19. * @param {Compiler} compiler the compiler instance
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. compiler.hooks.thisCompilation.tap(
  24. "ArrayPushCallbackChunkFormatPlugin",
  25. compilation => {
  26. compilation.hooks.additionalChunkRuntimeRequirements.tap(
  27. "ArrayPushCallbackChunkFormatPlugin",
  28. (chunk, set, { chunkGraph }) => {
  29. if (chunk.hasRuntime()) return;
  30. if (chunkGraph.getNumberOfEntryModules(chunk) > 0) {
  31. set.add(RuntimeGlobals.onChunksLoaded);
  32. set.add(RuntimeGlobals.require);
  33. }
  34. set.add(RuntimeGlobals.chunkCallback);
  35. }
  36. );
  37. const hooks = getCompilationHooks(compilation);
  38. hooks.renderChunk.tap(
  39. "ArrayPushCallbackChunkFormatPlugin",
  40. (modules, renderContext) => {
  41. const { chunk, chunkGraph, runtimeTemplate } = renderContext;
  42. const hotUpdateChunk =
  43. chunk instanceof HotUpdateChunk ? chunk : null;
  44. const globalObject = runtimeTemplate.globalObject;
  45. const source = new ConcatSource();
  46. const runtimeModules =
  47. chunkGraph.getChunkRuntimeModulesInOrder(chunk);
  48. if (hotUpdateChunk) {
  49. const hotUpdateGlobal =
  50. runtimeTemplate.outputOptions.hotUpdateGlobal;
  51. source.add(
  52. `${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
  53. );
  54. source.add(`${JSON.stringify(chunk.id)},`);
  55. source.add(modules);
  56. if (runtimeModules.length > 0) {
  57. source.add(",\n");
  58. const runtimePart = Template.renderChunkRuntimeModules(
  59. runtimeModules,
  60. renderContext
  61. );
  62. source.add(runtimePart);
  63. }
  64. source.add(")");
  65. } else {
  66. const chunkLoadingGlobal =
  67. runtimeTemplate.outputOptions.chunkLoadingGlobal;
  68. source.add(
  69. `(${globalObject}[${JSON.stringify(
  70. chunkLoadingGlobal
  71. )}] = ${globalObject}[${JSON.stringify(
  72. chunkLoadingGlobal
  73. )}] || []).push([`
  74. );
  75. source.add(`${JSON.stringify(chunk.ids)},`);
  76. source.add(modules);
  77. const entries = Array.from(
  78. chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
  79. );
  80. if (runtimeModules.length > 0 || entries.length > 0) {
  81. const runtime = new ConcatSource(
  82. (runtimeTemplate.supportsArrowFunction()
  83. ? "__webpack_require__ =>"
  84. : "function(__webpack_require__)") +
  85. " { // webpackRuntimeModules\n"
  86. );
  87. if (runtimeModules.length > 0) {
  88. runtime.add(
  89. Template.renderRuntimeModules(runtimeModules, {
  90. ...renderContext,
  91. codeGenerationResults: compilation.codeGenerationResults
  92. })
  93. );
  94. }
  95. if (entries.length > 0) {
  96. const startupSource = new RawSource(
  97. generateEntryStartup(
  98. chunkGraph,
  99. runtimeTemplate,
  100. entries,
  101. chunk,
  102. true
  103. )
  104. );
  105. runtime.add(
  106. hooks.renderStartup.call(
  107. startupSource,
  108. entries[entries.length - 1][0],
  109. {
  110. ...renderContext,
  111. inlined: false
  112. }
  113. )
  114. );
  115. if (
  116. chunkGraph
  117. .getChunkRuntimeRequirements(chunk)
  118. .has(RuntimeGlobals.returnExportsFromRuntime)
  119. ) {
  120. runtime.add("return __webpack_exports__;\n");
  121. }
  122. }
  123. runtime.add("}\n");
  124. source.add(",\n");
  125. source.add(new PrefixSource("/******/ ", runtime));
  126. }
  127. source.add("])");
  128. }
  129. return source;
  130. }
  131. );
  132. hooks.chunkHash.tap(
  133. "ArrayPushCallbackChunkFormatPlugin",
  134. (chunk, hash, { chunkGraph, runtimeTemplate }) => {
  135. if (chunk.hasRuntime()) return;
  136. hash.update(
  137. `ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}`
  138. );
  139. const entries = Array.from(
  140. chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
  141. );
  142. updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
  143. }
  144. );
  145. }
  146. );
  147. }
  148. }
  149. module.exports = ArrayPushCallbackChunkFormatPlugin;