DllReferencePlugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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("./util/fs").InputFileSystem} InputFileSystem */
  19. const validate = createSchemaValidation(
  20. require("../schemas/plugins/DllReferencePlugin.check.js"),
  21. () => require("../schemas/plugins/DllReferencePlugin.json"),
  22. {
  23. name: "Dll Reference Plugin",
  24. baseDataPath: "options"
  25. }
  26. );
  27. /** @typedef {{ path: string, data: DllReferencePluginOptionsManifest | undefined, error: Error | undefined }} CompilationDataItem */
  28. class DllReferencePlugin {
  29. /**
  30. * @param {DllReferencePluginOptions} options options object
  31. */
  32. constructor(options) {
  33. validate(options);
  34. this.options = options;
  35. /** @type {WeakMap<object, CompilationDataItem>} */
  36. this._compilationData = new WeakMap();
  37. }
  38. /**
  39. * Apply the plugin
  40. * @param {Compiler} compiler the compiler instance
  41. * @returns {void}
  42. */
  43. apply(compiler) {
  44. compiler.hooks.compilation.tap(
  45. "DllReferencePlugin",
  46. (compilation, { normalModuleFactory }) => {
  47. compilation.dependencyFactories.set(
  48. DelegatedSourceDependency,
  49. normalModuleFactory
  50. );
  51. }
  52. );
  53. compiler.hooks.beforeCompile.tapAsync(
  54. "DllReferencePlugin",
  55. (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. );
  96. compiler.hooks.compile.tap("DllReferencePlugin", params => {
  97. let name = this.options.name;
  98. let sourceType = this.options.sourceType;
  99. let resolvedContent =
  100. "content" in this.options ? this.options.content : undefined;
  101. if ("manifest" in this.options) {
  102. const manifestParameter = this.options.manifest;
  103. let manifest;
  104. if (typeof manifestParameter === "string") {
  105. const data =
  106. /** @type {CompilationDataItem} */
  107. (this._compilationData.get(params));
  108. // If there was an error parsing the manifest
  109. // file, exit now because the error will be added
  110. // as a compilation error in the "compilation" hook.
  111. if (data.error) {
  112. return;
  113. }
  114. manifest = data.data;
  115. } else {
  116. manifest = manifestParameter;
  117. }
  118. if (manifest) {
  119. if (!name) name = manifest.name;
  120. if (!sourceType) sourceType = manifest.type;
  121. if (!resolvedContent) resolvedContent = manifest.content;
  122. }
  123. }
  124. /** @type {Externals} */
  125. const externals = {};
  126. const source = `dll-reference ${name}`;
  127. externals[source] = /** @type {string} */ (name);
  128. const normalModuleFactory = params.normalModuleFactory;
  129. new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
  130. normalModuleFactory
  131. );
  132. new DelegatedModuleFactoryPlugin({
  133. source,
  134. type: this.options.type,
  135. scope: this.options.scope,
  136. context:
  137. /** @type {string} */
  138. (this.options.context || compiler.options.context),
  139. content:
  140. /** @type {DllReferencePluginOptionsContent} */
  141. (resolvedContent),
  142. extensions: this.options.extensions,
  143. associatedObjectForCache: compiler.root
  144. }).apply(normalModuleFactory);
  145. });
  146. compiler.hooks.compilation.tap(
  147. "DllReferencePlugin",
  148. (compilation, params) => {
  149. if ("manifest" in this.options) {
  150. const manifest = this.options.manifest;
  151. if (typeof manifest === "string") {
  152. const data = /** @type {CompilationDataItem} */ (
  153. this._compilationData.get(params)
  154. );
  155. // If there was an error parsing the manifest file, add the
  156. // error as a compilation error to make the compilation fail.
  157. if (data.error) {
  158. compilation.errors.push(
  159. /** @type {DllManifestError} */ (data.error)
  160. );
  161. }
  162. compilation.fileDependencies.add(manifest);
  163. }
  164. }
  165. }
  166. );
  167. }
  168. }
  169. class DllManifestError extends WebpackError {
  170. /**
  171. * @param {string} filename filename of the manifest
  172. * @param {string} message error message
  173. */
  174. constructor(filename, message) {
  175. super();
  176. this.name = "DllManifestError";
  177. this.message = `Dll manifest ${filename}\n${message}`;
  178. }
  179. }
  180. module.exports = DllReferencePlugin;