DllPlugin.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. const validate = createSchemaValidation(
  13. require("../schemas/plugins/DllPlugin.check.js"),
  14. () => require("../schemas/plugins/DllPlugin.json"),
  15. {
  16. name: "Dll Plugin",
  17. baseDataPath: "options"
  18. }
  19. );
  20. class DllPlugin {
  21. /**
  22. * @param {DllPluginOptions} options options object
  23. */
  24. constructor(options) {
  25. validate(options);
  26. this.options = {
  27. ...options,
  28. entryOnly: options.entryOnly !== false
  29. };
  30. }
  31. /**
  32. * Apply the plugin
  33. * @param {Compiler} compiler the compiler instance
  34. * @returns {void}
  35. */
  36. apply(compiler) {
  37. compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => {
  38. if (typeof entry !== "function") {
  39. for (const name of Object.keys(entry)) {
  40. const options = {
  41. name,
  42. filename: entry.filename
  43. };
  44. new DllEntryPlugin(context, entry[name].import, options).apply(
  45. compiler
  46. );
  47. }
  48. } else {
  49. throw new Error(
  50. "DllPlugin doesn't support dynamic entry (function) yet"
  51. );
  52. }
  53. return true;
  54. });
  55. new LibManifestPlugin(this.options).apply(compiler);
  56. if (!this.options.entryOnly) {
  57. new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler);
  58. }
  59. }
  60. }
  61. module.exports = DllPlugin;