WebpackCompilerHandler.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.WebpackCompilerHandler = void 0;
  15. var WebpackCompilerHandler = /** @class */ (function () {
  16. function WebpackCompilerHandler(chunkIncludeTester, chunkHandler, assetManager, moduleCache, addBanner, perChunkOutput, additionalChunkModules, additionalModules, skipChildCompilers) {
  17. this.chunkIncludeTester = chunkIncludeTester;
  18. this.chunkHandler = chunkHandler;
  19. this.assetManager = assetManager;
  20. this.moduleCache = moduleCache;
  21. this.addBanner = addBanner;
  22. this.perChunkOutput = perChunkOutput;
  23. this.additionalChunkModules = additionalChunkModules;
  24. this.additionalModules = additionalModules;
  25. this.skipChildCompilers = skipChildCompilers;
  26. }
  27. WebpackCompilerHandler.prototype.handleCompiler = function (compiler) {
  28. var _this = this;
  29. if (typeof compiler.hooks !== 'undefined') {
  30. var hookType = this.skipChildCompilers
  31. ? 'thisCompilation'
  32. : 'compilation';
  33. compiler.hooks[hookType].tap('LicenseWebpackPlugin', function (compilation) {
  34. if (typeof compilation.hooks.processAssets !== 'undefined') {
  35. // webpack v5
  36. compilation.hooks.processAssets.tap({
  37. name: 'LicenseWebpackPlugin',
  38. stage: WebpackCompilerHandler.PROCESS_ASSETS_STAGE_REPORT
  39. }, function () {
  40. // the chunk graph does not contain ES modules
  41. // use stats instead to find the ES module imports
  42. var stats = compilation.getStats().toJson({
  43. all: false,
  44. chunks: true,
  45. chunkModules: true,
  46. nestedModules: true,
  47. dependentModules: true,
  48. cachedModules: true
  49. });
  50. _this.iterateChunks(compilation, compilation.chunks, stats);
  51. });
  52. }
  53. else {
  54. // webpack v4
  55. compilation.hooks.optimizeChunkAssets.tap('LicenseWebpackPlugin', function (chunks) {
  56. _this.iterateChunks(compilation, chunks);
  57. });
  58. }
  59. if (_this.addBanner) {
  60. _this.iterateChunksForBanner(compilation);
  61. }
  62. });
  63. if (!this.perChunkOutput) {
  64. compiler.hooks[hookType].tap('LicenseWebpackPlugin', function (compilation) {
  65. if (!compilation.compiler.isChild()) {
  66. // Select only root compiler to avoid writing license file multiple times per compilation
  67. if (typeof compilation.hooks.processAssets !== 'undefined') {
  68. // webpack v5
  69. compilation.hooks.processAssets.tap({
  70. name: 'LicenseWebpackPlugin',
  71. stage: WebpackCompilerHandler.PROCESS_ASSETS_STAGE_REPORT + 1
  72. }, function () {
  73. _this.assetManager.writeAllLicenses(_this.moduleCache.getAllModules(), compilation);
  74. });
  75. }
  76. else {
  77. // webpack v4
  78. compilation.hooks.optimizeChunkAssets.tap('LicenseWebpackPlugin', function () {
  79. _this.assetManager.writeAllLicenses(_this.moduleCache.getAllModules(), compilation);
  80. });
  81. }
  82. }
  83. });
  84. }
  85. }
  86. else if (typeof compiler.plugin !== 'undefined') {
  87. compiler.plugin('compilation', function (compilation) {
  88. if (typeof compilation.plugin !== 'undefined') {
  89. compilation.plugin('optimize-chunk-assets', function (chunks, callback) {
  90. _this.iterateChunks(compilation, chunks);
  91. callback();
  92. });
  93. }
  94. });
  95. }
  96. };
  97. WebpackCompilerHandler.prototype.iterateChunksForBanner = function (compilation) {
  98. var _this = this;
  99. // for webpack v4 we write banners in iterateChunks.
  100. // because of plugin hook ordering issues, it is done separately here for webpack v5.
  101. // it is important to note that renderBanner will not receive any modules in the second
  102. // argument due to plugin hook ordering issues in webpack v5.
  103. // For the banner to work in webpack v5 production mode, TerserPlugin must be configured in a specific way.
  104. // Please check the documentation of License Webpack Plugin for more details.
  105. if (typeof compilation.hooks.processAssets !== 'undefined') {
  106. // webpack v5
  107. compilation.hooks.processAssets.tap({
  108. name: 'LicenseWebpackPlugin',
  109. stage: WebpackCompilerHandler.PROCESS_ASSETS_STAGE_ADDITIONS
  110. }, function () {
  111. var e_1, _a;
  112. try {
  113. for (var _b = __values(compilation.chunks), _c = _b.next(); !_c.done; _c = _b.next()) {
  114. var chunk = _c.value;
  115. if (_this.chunkIncludeTester.isIncluded(chunk.name)) {
  116. _this.assetManager.writeChunkBanners(_this.moduleCache.getAllModulesForChunk(chunk.name), compilation, chunk);
  117. }
  118. }
  119. }
  120. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  121. finally {
  122. try {
  123. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  124. }
  125. finally { if (e_1) throw e_1.error; }
  126. }
  127. });
  128. }
  129. };
  130. WebpackCompilerHandler.prototype.iterateChunks = function (compilation, chunks, stats) {
  131. var e_2, _a;
  132. var _this = this;
  133. var _loop_1 = function (chunk) {
  134. if (this_1.chunkIncludeTester.isIncluded(chunk.name)) {
  135. this_1.chunkHandler.processChunk(compilation, chunk, this_1.moduleCache, stats);
  136. if (this_1.additionalChunkModules[chunk.name]) {
  137. this_1.additionalChunkModules[chunk.name].forEach(function (module) {
  138. return _this.chunkHandler.processModule(compilation, chunk, _this.moduleCache, module);
  139. });
  140. }
  141. if (this_1.additionalModules.length > 0) {
  142. this_1.additionalModules.forEach(function (module) {
  143. return _this.chunkHandler.processModule(compilation, chunk, _this.moduleCache, module);
  144. });
  145. }
  146. if (this_1.perChunkOutput) {
  147. this_1.assetManager.writeChunkLicenses(this_1.moduleCache.getAllModulesForChunk(chunk.name), compilation, chunk);
  148. }
  149. if (this_1.addBanner &&
  150. typeof compilation.hooks.processAssets === 'undefined') {
  151. // webpack v4
  152. this_1.assetManager.writeChunkBanners(this_1.moduleCache.getAllModulesForChunk(chunk.name), compilation, chunk);
  153. }
  154. }
  155. };
  156. var this_1 = this;
  157. try {
  158. for (var chunks_1 = __values(chunks), chunks_1_1 = chunks_1.next(); !chunks_1_1.done; chunks_1_1 = chunks_1.next()) {
  159. var chunk = chunks_1_1.value;
  160. _loop_1(chunk);
  161. }
  162. }
  163. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  164. finally {
  165. try {
  166. if (chunks_1_1 && !chunks_1_1.done && (_a = chunks_1.return)) _a.call(chunks_1);
  167. }
  168. finally { if (e_2) throw e_2.error; }
  169. }
  170. };
  171. // copied from webpack/lib/Compilation.js
  172. WebpackCompilerHandler.PROCESS_ASSETS_STAGE_ADDITIONS = -100;
  173. WebpackCompilerHandler.PROCESS_ASSETS_STAGE_REPORT = 5000;
  174. return WebpackCompilerHandler;
  175. }());
  176. exports.WebpackCompilerHandler = WebpackCompilerHandler;