AbstractLibraryPlugin.js 8.7 KB

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