AbstractLibraryPlugin.js 9.7 KB

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