DllReferencePlugin.js 5.3 KB

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