DllPlugin.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DllEntryPlugin = require("./DllEntryPlugin");
  7. const FlagAllModulesAsUsedPlugin = require("./FlagAllModulesAsUsedPlugin");
  8. const LibManifestPlugin = require("./LibManifestPlugin");
  9. const createSchemaValidation = require("./util/create-schema-validation");
  10. /** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
  11. /** @typedef {import("./Compiler")} Compiler */
  12. /** @typedef {import("./DllEntryPlugin").Entries} Entries */
  13. /** @typedef {import("./DllEntryPlugin").Options} Options */
  14. const validate = createSchemaValidation(
  15. require("../schemas/plugins/DllPlugin.check.js"),
  16. () => require("../schemas/plugins/DllPlugin.json"),
  17. {
  18. name: "Dll Plugin",
  19. baseDataPath: "options"
  20. }
  21. );
  22. class DllPlugin {
  23. /**
  24. * @param {DllPluginOptions} options options object
  25. */
  26. constructor(options) {
  27. validate(options);
  28. this.options = {
  29. ...options,
  30. entryOnly: options.entryOnly !== false
  31. };
  32. }
  33. /**
  34. * Apply the plugin
  35. * @param {Compiler} compiler the compiler instance
  36. * @returns {void}
  37. */
  38. apply(compiler) {
  39. compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => {
  40. if (typeof entry !== "function") {
  41. for (const name of Object.keys(entry)) {
  42. /** @type {Options} */
  43. const options = { name, filename: entry.filename };
  44. new DllEntryPlugin(
  45. context,
  46. /** @type {Entries} */ (entry[name].import),
  47. options
  48. ).apply(compiler);
  49. }
  50. } else {
  51. throw new Error(
  52. "DllPlugin doesn't support dynamic entry (function) yet"
  53. );
  54. }
  55. return true;
  56. });
  57. new LibManifestPlugin(this.options).apply(compiler);
  58. if (!this.options.entryOnly) {
  59. new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler);
  60. }
  61. }
  62. }
  63. module.exports = DllPlugin;