DllReferencePlugin.js 5.0 KB

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