DllReferencePlugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const parseJson = require("json-parse-even-better-errors");
  7. const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
  8. const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
  9. const WebpackError = require("./WebpackError");
  10. const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
  11. const createSchemaValidation = require("./util/create-schema-validation");
  12. const makePathsRelative = require("./util/identifier").makePathsRelative;
  13. /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
  14. /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
  15. /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */
  16. /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */
  17. /** @typedef {import("./Compiler")} Compiler */
  18. /** @typedef {import("./Compiler").CompilationParams} CompilationParams */
  19. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  20. const validate = createSchemaValidation(
  21. require("../schemas/plugins/DllReferencePlugin.check.js"),
  22. () => require("../schemas/plugins/DllReferencePlugin.json"),
  23. {
  24. name: "Dll Reference Plugin",
  25. baseDataPath: "options"
  26. }
  27. );
  28. /** @typedef {{ path: string, data: DllReferencePluginOptionsManifest | undefined, error: Error | undefined }} CompilationDataItem */
  29. const PLUGIN_NAME = "DllReferencePlugin";
  30. class DllReferencePlugin {
  31. /**
  32. * @param {DllReferencePluginOptions} options options object
  33. */
  34. constructor(options) {
  35. validate(options);
  36. this.options = options;
  37. /** @type {WeakMap<CompilationParams, CompilationDataItem>} */
  38. this._compilationData = new WeakMap();
  39. }
  40. /**
  41. * Apply the plugin
  42. * @param {Compiler} compiler the compiler instance
  43. * @returns {void}
  44. */
  45. apply(compiler) {
  46. compiler.hooks.compilation.tap(
  47. PLUGIN_NAME,
  48. (compilation, { normalModuleFactory }) => {
  49. compilation.dependencyFactories.set(
  50. DelegatedSourceDependency,
  51. normalModuleFactory
  52. );
  53. }
  54. );
  55. compiler.hooks.beforeCompile.tapAsync(PLUGIN_NAME, (params, callback) => {
  56. if ("manifest" in this.options) {
  57. const manifest = this.options.manifest;
  58. if (typeof manifest === "string") {
  59. /** @type {InputFileSystem} */
  60. (compiler.inputFileSystem).readFile(manifest, (err, result) => {
  61. if (err) return callback(err);
  62. /** @type {CompilationDataItem} */
  63. const data = {
  64. path: manifest,
  65. data: undefined,
  66. error: undefined
  67. };
  68. // Catch errors parsing the manifest so that blank
  69. // or malformed manifest files don't kill the process.
  70. try {
  71. data.data = parseJson(
  72. /** @type {Buffer} */ (result).toString("utf-8")
  73. );
  74. } catch (parseErr) {
  75. // Store the error in the params so that it can
  76. // be added as a compilation error later on.
  77. const manifestPath = makePathsRelative(
  78. /** @type {string} */ (compiler.options.context),
  79. manifest,
  80. compiler.root
  81. );
  82. data.error = new DllManifestError(
  83. manifestPath,
  84. /** @type {Error} */ (parseErr).message
  85. );
  86. }
  87. this._compilationData.set(params, data);
  88. return callback();
  89. });
  90. return;
  91. }
  92. }
  93. return callback();
  94. });
  95. compiler.hooks.compile.tap(PLUGIN_NAME, params => {
  96. let name = this.options.name;
  97. let sourceType = this.options.sourceType;
  98. let resolvedContent =
  99. "content" in this.options ? this.options.content : undefined;
  100. if ("manifest" in this.options) {
  101. const manifestParameter = this.options.manifest;
  102. let manifest;
  103. if (typeof manifestParameter === "string") {
  104. const data =
  105. /** @type {CompilationDataItem} */
  106. (this._compilationData.get(params));
  107. // If there was an error parsing the manifest
  108. // file, exit now because the error will be added
  109. // as a compilation error in the "compilation" hook.
  110. if (data.error) {
  111. return;
  112. }
  113. manifest = data.data;
  114. } else {
  115. manifest = manifestParameter;
  116. }
  117. if (manifest) {
  118. if (!name) name = manifest.name;
  119. if (!sourceType) sourceType = manifest.type;
  120. if (!resolvedContent) resolvedContent = manifest.content;
  121. }
  122. }
  123. /** @type {Externals} */
  124. const externals = {};
  125. const source = `dll-reference ${name}`;
  126. externals[source] = /** @type {string} */ (name);
  127. const normalModuleFactory = params.normalModuleFactory;
  128. new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
  129. normalModuleFactory
  130. );
  131. new DelegatedModuleFactoryPlugin({
  132. source,
  133. type: this.options.type,
  134. scope: this.options.scope,
  135. context:
  136. /** @type {string} */
  137. (this.options.context || compiler.options.context),
  138. content:
  139. /** @type {DllReferencePluginOptionsContent} */
  140. (resolvedContent),
  141. extensions: this.options.extensions,
  142. associatedObjectForCache: compiler.root
  143. }).apply(normalModuleFactory);
  144. });
  145. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation, params) => {
  146. if ("manifest" in this.options) {
  147. const manifest = this.options.manifest;
  148. if (typeof manifest === "string") {
  149. const data = /** @type {CompilationDataItem} */ (
  150. this._compilationData.get(params)
  151. );
  152. // If there was an error parsing the manifest file, add the
  153. // error as a compilation error to make the compilation fail.
  154. if (data.error) {
  155. compilation.errors.push(
  156. /** @type {DllManifestError} */ (data.error)
  157. );
  158. }
  159. compilation.fileDependencies.add(manifest);
  160. }
  161. }
  162. });
  163. }
  164. }
  165. class DllManifestError extends WebpackError {
  166. /**
  167. * @param {string} filename filename of the manifest
  168. * @param {string} message error message
  169. */
  170. constructor(filename, message) {
  171. super();
  172. this.name = "DllManifestError";
  173. this.message = `Dll manifest ${filename}\n${message}`;
  174. }
  175. }
  176. module.exports = DllReferencePlugin;