AbstractLibraryPlugin.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
  8. /** @typedef {import("webpack-sources").Source} Source */
  9. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  10. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  11. /** @typedef {import("../Chunk")} Chunk */
  12. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  13. /** @typedef {import("../Compilation")} Compilation */
  14. /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
  15. /** @typedef {import("../Compiler")} Compiler */
  16. /** @typedef {import("../Module")} Module */
  17. /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
  18. /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
  19. /** @typedef {import("../util/Hash")} Hash */
  20. const COMMON_LIBRARY_NAME_MESSAGE =
  21. "Common configuration options that specific library names are 'output.library[.name]', 'entry.xyz.library[.name]', 'ModuleFederationPlugin.name' and 'ModuleFederationPlugin.library[.name]'.";
  22. /**
  23. * @template T
  24. * @typedef {Object} LibraryContext
  25. * @property {Compilation} compilation
  26. * @property {ChunkGraph} chunkGraph
  27. * @property {T} options
  28. */
  29. /**
  30. * @template T
  31. */
  32. class AbstractLibraryPlugin {
  33. /**
  34. * @param {Object} options options
  35. * @param {string} options.pluginName name of the plugin
  36. * @param {LibraryType} options.type used library type
  37. */
  38. constructor({ pluginName, type }) {
  39. this._pluginName = pluginName;
  40. this._type = type;
  41. this._parseCache = new WeakMap();
  42. }
  43. /**
  44. * Apply the plugin
  45. * @param {Compiler} compiler the compiler instance
  46. * @returns {void}
  47. */
  48. apply(compiler) {
  49. const { _pluginName } = this;
  50. compiler.hooks.thisCompilation.tap(_pluginName, compilation => {
  51. compilation.hooks.finishModules.tap(
  52. { name: _pluginName, stage: 10 },
  53. () => {
  54. for (const [
  55. name,
  56. {
  57. dependencies: deps,
  58. options: { library }
  59. }
  60. ] of compilation.entries) {
  61. const options = this._parseOptionsCached(
  62. library !== undefined
  63. ? library
  64. : compilation.outputOptions.library
  65. );
  66. if (options !== false) {
  67. const dep = deps[deps.length - 1];
  68. if (dep) {
  69. const module = compilation.moduleGraph.getModule(dep);
  70. if (module) {
  71. this.finishEntryModule(module, name, {
  72. options,
  73. compilation,
  74. chunkGraph: compilation.chunkGraph
  75. });
  76. }
  77. }
  78. }
  79. }
  80. }
  81. );
  82. const getOptionsForChunk = chunk => {
  83. if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0)
  84. return false;
  85. const options = chunk.getEntryOptions();
  86. const library = options && options.library;
  87. return this._parseOptionsCached(
  88. library !== undefined ? library : compilation.outputOptions.library
  89. );
  90. };
  91. if (
  92. this.render !== AbstractLibraryPlugin.prototype.render ||
  93. this.runtimeRequirements !==
  94. AbstractLibraryPlugin.prototype.runtimeRequirements
  95. ) {
  96. compilation.hooks.additionalChunkRuntimeRequirements.tap(
  97. _pluginName,
  98. (chunk, set, { chunkGraph }) => {
  99. const options = getOptionsForChunk(chunk);
  100. if (options !== false) {
  101. this.runtimeRequirements(chunk, set, {
  102. options,
  103. compilation,
  104. chunkGraph
  105. });
  106. }
  107. }
  108. );
  109. }
  110. const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
  111. if (this.render !== AbstractLibraryPlugin.prototype.render) {
  112. hooks.render.tap(_pluginName, (source, renderContext) => {
  113. const options = getOptionsForChunk(renderContext.chunk);
  114. if (options === false) return source;
  115. return this.render(source, renderContext, {
  116. options,
  117. compilation,
  118. chunkGraph: compilation.chunkGraph
  119. });
  120. });
  121. }
  122. if (
  123. this.embedInRuntimeBailout !==
  124. AbstractLibraryPlugin.prototype.embedInRuntimeBailout
  125. ) {
  126. hooks.embedInRuntimeBailout.tap(
  127. _pluginName,
  128. (module, renderContext) => {
  129. const options = getOptionsForChunk(renderContext.chunk);
  130. if (options === false) return;
  131. return this.embedInRuntimeBailout(module, renderContext, {
  132. options,
  133. compilation,
  134. chunkGraph: compilation.chunkGraph
  135. });
  136. }
  137. );
  138. }
  139. if (
  140. this.strictRuntimeBailout !==
  141. AbstractLibraryPlugin.prototype.strictRuntimeBailout
  142. ) {
  143. hooks.strictRuntimeBailout.tap(_pluginName, renderContext => {
  144. const options = getOptionsForChunk(renderContext.chunk);
  145. if (options === false) return;
  146. return this.strictRuntimeBailout(renderContext, {
  147. options,
  148. compilation,
  149. chunkGraph: compilation.chunkGraph
  150. });
  151. });
  152. }
  153. if (
  154. this.renderStartup !== AbstractLibraryPlugin.prototype.renderStartup
  155. ) {
  156. hooks.renderStartup.tap(
  157. _pluginName,
  158. (source, module, renderContext) => {
  159. const options = getOptionsForChunk(renderContext.chunk);
  160. if (options === false) return source;
  161. return this.renderStartup(source, module, renderContext, {
  162. options,
  163. compilation,
  164. chunkGraph: compilation.chunkGraph
  165. });
  166. }
  167. );
  168. }
  169. hooks.chunkHash.tap(_pluginName, (chunk, hash, context) => {
  170. const options = getOptionsForChunk(chunk);
  171. if (options === false) return;
  172. this.chunkHash(chunk, hash, context, {
  173. options,
  174. compilation,
  175. chunkGraph: compilation.chunkGraph
  176. });
  177. });
  178. });
  179. }
  180. /**
  181. * @param {LibraryOptions=} library normalized library option
  182. * @returns {T | false} preprocess as needed by overriding
  183. */
  184. _parseOptionsCached(library) {
  185. if (!library) return false;
  186. if (library.type !== this._type) return false;
  187. const cacheEntry = this._parseCache.get(library);
  188. if (cacheEntry !== undefined) return cacheEntry;
  189. const result = this.parseOptions(library);
  190. this._parseCache.set(library, result);
  191. return result;
  192. }
  193. /* istanbul ignore next */
  194. /**
  195. * @abstract
  196. * @param {LibraryOptions} library normalized library option
  197. * @returns {T | false} preprocess as needed by overriding
  198. */
  199. parseOptions(library) {
  200. const AbstractMethodError = require("../AbstractMethodError");
  201. throw new AbstractMethodError();
  202. }
  203. /**
  204. * @param {Module} module the exporting entry module
  205. * @param {string} entryName the name of the entrypoint
  206. * @param {LibraryContext<T>} libraryContext context
  207. * @returns {void}
  208. */
  209. finishEntryModule(module, entryName, libraryContext) {}
  210. /**
  211. * @param {Module} module the exporting entry module
  212. * @param {RenderContext} renderContext render context
  213. * @param {LibraryContext<T>} libraryContext context
  214. * @returns {string | undefined} bailout reason
  215. */
  216. embedInRuntimeBailout(module, renderContext, libraryContext) {
  217. return undefined;
  218. }
  219. /**
  220. * @param {RenderContext} renderContext render context
  221. * @param {LibraryContext<T>} libraryContext context
  222. * @returns {string | undefined} bailout reason
  223. */
  224. strictRuntimeBailout(renderContext, libraryContext) {
  225. return undefined;
  226. }
  227. /**
  228. * @param {Chunk} chunk the chunk
  229. * @param {Set<string>} set runtime requirements
  230. * @param {LibraryContext<T>} libraryContext context
  231. * @returns {void}
  232. */
  233. runtimeRequirements(chunk, set, libraryContext) {
  234. if (this.render !== AbstractLibraryPlugin.prototype.render)
  235. set.add(RuntimeGlobals.returnExportsFromRuntime);
  236. }
  237. /**
  238. * @param {Source} source source
  239. * @param {RenderContext} renderContext render context
  240. * @param {LibraryContext<T>} libraryContext context
  241. * @returns {Source} source with library export
  242. */
  243. render(source, renderContext, libraryContext) {
  244. return source;
  245. }
  246. /**
  247. * @param {Source} source source
  248. * @param {Module} module module
  249. * @param {StartupRenderContext} renderContext render context
  250. * @param {LibraryContext<T>} libraryContext context
  251. * @returns {Source} source with library export
  252. */
  253. renderStartup(source, module, renderContext, libraryContext) {
  254. return source;
  255. }
  256. /**
  257. * @param {Chunk} chunk the chunk
  258. * @param {Hash} hash hash
  259. * @param {ChunkHashContext} chunkHashContext chunk hash context
  260. * @param {LibraryContext<T>} libraryContext context
  261. * @returns {void}
  262. */
  263. chunkHash(chunk, hash, chunkHashContext, libraryContext) {
  264. const options = this._parseOptionsCached(
  265. libraryContext.compilation.outputOptions.library
  266. );
  267. hash.update(this._pluginName);
  268. hash.update(JSON.stringify(options));
  269. }
  270. }
  271. AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE = COMMON_LIBRARY_NAME_MESSAGE;
  272. module.exports = AbstractLibraryPlugin;