DllPlugin.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const PLUGIN_NAME = "DllPlugin";
  23. class DllPlugin {
  24. /**
  25. * @param {DllPluginOptions} options options object
  26. */
  27. constructor(options) {
  28. validate(options);
  29. this.options = {
  30. ...options,
  31. entryOnly: options.entryOnly !== false
  32. };
  33. }
  34. /**
  35. * Apply the plugin
  36. * @param {Compiler} compiler the compiler instance
  37. * @returns {void}
  38. */
  39. apply(compiler) {
  40. compiler.hooks.entryOption.tap(PLUGIN_NAME, (context, entry) => {
  41. if (typeof entry !== "function") {
  42. for (const name of Object.keys(entry)) {
  43. /** @type {Options} */
  44. const options = { name, filename: entry.filename };
  45. new DllEntryPlugin(
  46. context,
  47. /** @type {Entries} */ (entry[name].import),
  48. options
  49. ).apply(compiler);
  50. }
  51. } else {
  52. throw new Error(
  53. `${PLUGIN_NAME} doesn't support dynamic entry (function) yet`
  54. );
  55. }
  56. return true;
  57. });
  58. new LibManifestPlugin(this.options).apply(compiler);
  59. if (!this.options.entryOnly) {
  60. new FlagAllModulesAsUsedPlugin(PLUGIN_NAME).apply(compiler);
  61. }
  62. }
  63. }
  64. module.exports = DllPlugin;