提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.4 KiB

  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const EntryDependency = require("./dependencies/EntryDependency");
  8. const { someInIterable } = require("./util/IterableHelpers");
  9. const { compareModulesById } = require("./util/comparators");
  10. const { dirname, mkdirp } = require("./util/fs");
  11. /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
  12. /** @typedef {import("./Compiler")} Compiler */
  13. /** @typedef {import("./Compiler").IntermediateFileSystem} IntermediateFileSystem */
  14. /** @typedef {import("./Module").BuildMeta} BuildMeta */
  15. /** @typedef {import("./ExportsInfo").ExportInfoName} ExportInfoName */
  16. /**
  17. * @typedef {object} ManifestModuleData
  18. * @property {ModuleId} id
  19. * @property {BuildMeta=} buildMeta
  20. * @property {ExportInfoName[]=} exports
  21. */
  22. /**
  23. * @typedef {object} LibManifestPluginOptions
  24. * @property {string=} context Context of requests in the manifest file (defaults to the webpack context).
  25. * @property {boolean=} entryOnly If true, only entry points will be exposed (default: true).
  26. * @property {boolean=} format If true, manifest json file (output) will be formatted.
  27. * @property {string=} name Name of the exposed dll function (external name, use value of 'output.library').
  28. * @property {string} path Absolute path to the manifest json file (output).
  29. * @property {string=} type Type of the dll bundle (external type, use value of 'output.libraryTarget').
  30. */
  31. const PLUGIN_NAME = "LibManifestPlugin";
  32. class LibManifestPlugin {
  33. /**
  34. * @param {LibManifestPluginOptions} options the options
  35. */
  36. constructor(options) {
  37. this.options = options;
  38. }
  39. /**
  40. * Apply the plugin
  41. * @param {Compiler} compiler the compiler instance
  42. * @returns {void}
  43. */
  44. apply(compiler) {
  45. compiler.hooks.emit.tapAsync(
  46. { name: PLUGIN_NAME, stage: 110 },
  47. (compilation, callback) => {
  48. const moduleGraph = compilation.moduleGraph;
  49. // store used paths to detect issue and output an error. #18200
  50. const usedPaths = new Set();
  51. asyncLib.each(
  52. [...compilation.chunks],
  53. (chunk, callback) => {
  54. if (!chunk.canBeInitial()) {
  55. callback();
  56. return;
  57. }
  58. const chunkGraph = compilation.chunkGraph;
  59. const targetPath = compilation.getPath(this.options.path, {
  60. chunk
  61. });
  62. if (usedPaths.has(targetPath)) {
  63. callback(new Error("each chunk must have a unique path"));
  64. return;
  65. }
  66. usedPaths.add(targetPath);
  67. const name =
  68. this.options.name &&
  69. compilation.getPath(this.options.name, {
  70. chunk,
  71. contentHashType: "javascript"
  72. });
  73. const content = Object.create(null);
  74. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  75. chunk,
  76. compareModulesById(chunkGraph)
  77. )) {
  78. if (
  79. this.options.entryOnly &&
  80. !someInIterable(
  81. moduleGraph.getIncomingConnections(module),
  82. (c) => c.dependency instanceof EntryDependency
  83. )
  84. ) {
  85. continue;
  86. }
  87. const ident = module.libIdent({
  88. context: this.options.context || compiler.context,
  89. associatedObjectForCache: compiler.root
  90. });
  91. if (ident) {
  92. const exportsInfo = moduleGraph.getExportsInfo(module);
  93. const providedExports = exportsInfo.getProvidedExports();
  94. /** @type {ManifestModuleData} */
  95. const data = {
  96. id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
  97. buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
  98. exports: Array.isArray(providedExports)
  99. ? providedExports
  100. : undefined
  101. };
  102. content[ident] = data;
  103. }
  104. }
  105. const manifest = {
  106. name,
  107. type: this.options.type,
  108. content
  109. };
  110. // Apply formatting to content if format flag is true;
  111. const manifestContent = this.options.format
  112. ? JSON.stringify(manifest, null, 2)
  113. : JSON.stringify(manifest);
  114. const buffer = Buffer.from(manifestContent, "utf8");
  115. const intermediateFileSystem =
  116. /** @type {IntermediateFileSystem} */ (
  117. compiler.intermediateFileSystem
  118. );
  119. mkdirp(
  120. intermediateFileSystem,
  121. dirname(intermediateFileSystem, targetPath),
  122. (err) => {
  123. if (err) return callback(err);
  124. intermediateFileSystem.writeFile(targetPath, buffer, callback);
  125. }
  126. );
  127. },
  128. callback
  129. );
  130. }
  131. );
  132. }
  133. }
  134. module.exports = LibManifestPlugin;