DllReferencePlugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. class DllReferencePlugin {
  30. /**
  31. * @param {DllReferencePluginOptions} options options object
  32. */
  33. constructor(options) {
  34. validate(options);
  35. this.options = options;
  36. /** @type {WeakMap<CompilationParams, CompilationDataItem>} */
  37. this._compilationData = new WeakMap();
  38. }
  39. /**
  40. * Apply the plugin
  41. * @param {Compiler} compiler the compiler instance
  42. * @returns {void}
  43. */
  44. apply(compiler) {
  45. compiler.hooks.compilation.tap(
  46. "DllReferencePlugin",
  47. (compilation, { normalModuleFactory }) => {
  48. compilation.dependencyFactories.set(
  49. DelegatedSourceDependency,
  50. normalModuleFactory
  51. );
  52. }
  53. );
  54. compiler.hooks.beforeCompile.tapAsync(
  55. "DllReferencePlugin",
  56. (params, callback) => {
  57. if ("manifest" in this.options) {
  58. const manifest = this.options.manifest;
  59. if (typeof manifest === "string") {
  60. /** @type {InputFileSystem} */
  61. (compiler.inputFileSystem).readFile(manifest, (err, result) => {
  62. if (err) return callback(err);
  63. /** @type {CompilationDataItem} */
  64. const data = {
  65. path: manifest,
  66. data: undefined,
  67. error: undefined
  68. };
  69. // Catch errors parsing the manifest so that blank
  70. // or malformed manifest files don't kill the process.
  71. try {
  72. data.data = parseJson(
  73. /** @type {Buffer} */ (result).toString("utf-8")
  74. );
  75. } catch (parseErr) {
  76. // Store the error in the params so that it can
  77. // be added as a compilation error later on.
  78. const manifestPath = makePathsRelative(
  79. /** @type {string} */ (compiler.options.context),
  80. manifest,
  81. compiler.root
  82. );
  83. data.error = new DllManifestError(
  84. manifestPath,
  85. /** @type {Error} */ (parseErr).message
  86. );
  87. }
  88. this._compilationData.set(params, data);
  89. return callback();
  90. });
  91. return;
  92. }
  93. }
  94. return callback();
  95. }
  96. );
  97. compiler.hooks.compile.tap("DllReferencePlugin", params => {
  98. let name = this.options.name;
  99. let sourceType = this.options.sourceType;
  100. let resolvedContent =
  101. "content" in this.options ? this.options.content : undefined;
  102. if ("manifest" in this.options) {
  103. const manifestParameter = this.options.manifest;
  104. let manifest;
  105. if (typeof manifestParameter === "string") {
  106. const data =
  107. /** @type {CompilationDataItem} */
  108. (this._compilationData.get(params));
  109. // If there was an error parsing the manifest
  110. // file, exit now because the error will be added
  111. // as a compilation error in the "compilation" hook.
  112. if (data.error) {
  113. return;
  114. }
  115. manifest = data.data;
  116. } else {
  117. manifest = manifestParameter;
  118. }
  119. if (manifest) {
  120. if (!name) name = manifest.name;
  121. if (!sourceType) sourceType = manifest.type;
  122. if (!resolvedContent) resolvedContent = manifest.content;
  123. }
  124. }
  125. /** @type {Externals} */
  126. const externals = {};
  127. const source = `dll-reference ${name}`;
  128. externals[source] = /** @type {string} */ (name);
  129. const normalModuleFactory = params.normalModuleFactory;
  130. new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
  131. normalModuleFactory
  132. );
  133. new DelegatedModuleFactoryPlugin({
  134. source,
  135. type: this.options.type,
  136. scope: this.options.scope,
  137. context:
  138. /** @type {string} */
  139. (this.options.context || compiler.options.context),
  140. content:
  141. /** @type {DllReferencePluginOptionsContent} */
  142. (resolvedContent),
  143. extensions: this.options.extensions,
  144. associatedObjectForCache: compiler.root
  145. }).apply(normalModuleFactory);
  146. });
  147. compiler.hooks.compilation.tap(
  148. "DllReferencePlugin",
  149. (compilation, params) => {
  150. if ("manifest" in this.options) {
  151. const manifest = this.options.manifest;
  152. if (typeof manifest === "string") {
  153. const data = /** @type {CompilationDataItem} */ (
  154. this._compilationData.get(params)
  155. );
  156. // If there was an error parsing the manifest file, add the
  157. // error as a compilation error to make the compilation fail.
  158. if (data.error) {
  159. compilation.errors.push(
  160. /** @type {DllManifestError} */ (data.error)
  161. );
  162. }
  163. compilation.fileDependencies.add(manifest);
  164. }
  165. }
  166. }
  167. );
  168. }
  169. }
  170. class DllManifestError extends WebpackError {
  171. /**
  172. * @param {string} filename filename of the manifest
  173. * @param {string} message error message
  174. */
  175. constructor(filename, message) {
  176. super();
  177. this.name = "DllManifestError";
  178. this.message = `Dll manifest ${filename}\n${message}`;
  179. }
  180. }
  181. module.exports = DllReferencePlugin;