ContainerPlugin.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
  4. */
  5. "use strict";
  6. const createSchemaValidation = require("../util/create-schema-validation");
  7. const ContainerEntryDependency = require("./ContainerEntryDependency");
  8. const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory");
  9. const ContainerExposedDependency = require("./ContainerExposedDependency");
  10. const { parseOptions } = require("./options");
  11. /** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */
  12. /** @typedef {import("../Compiler")} Compiler */
  13. const validate = createSchemaValidation(
  14. require("../../schemas/plugins/container/ContainerPlugin.check.js"),
  15. () => require("../../schemas/plugins/container/ContainerPlugin.json"),
  16. {
  17. name: "Container Plugin",
  18. baseDataPath: "options"
  19. }
  20. );
  21. const PLUGIN_NAME = "ContainerPlugin";
  22. class ContainerPlugin {
  23. /**
  24. * @param {ContainerPluginOptions} options options
  25. */
  26. constructor(options) {
  27. validate(options);
  28. this._options = {
  29. name: options.name,
  30. shareScope: options.shareScope || "default",
  31. library: options.library || {
  32. type: "var",
  33. name: options.name
  34. },
  35. runtime: options.runtime,
  36. filename: options.filename || undefined,
  37. exposes: parseOptions(
  38. options.exposes,
  39. item => ({
  40. import: Array.isArray(item) ? item : [item],
  41. name: undefined
  42. }),
  43. item => ({
  44. import: Array.isArray(item.import) ? item.import : [item.import],
  45. name: item.name || undefined
  46. })
  47. )
  48. };
  49. }
  50. /**
  51. * Apply the plugin
  52. * @param {Compiler} compiler the compiler instance
  53. * @returns {void}
  54. */
  55. apply(compiler) {
  56. const { name, exposes, shareScope, filename, library, runtime } =
  57. this._options;
  58. compiler.options.output.enabledLibraryTypes.push(library.type);
  59. compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
  60. const dep = new ContainerEntryDependency(name, exposes, shareScope);
  61. dep.loc = { name };
  62. compilation.addEntry(
  63. compilation.options.context,
  64. dep,
  65. {
  66. name,
  67. filename,
  68. runtime,
  69. library
  70. },
  71. error => {
  72. if (error) return callback(error);
  73. callback();
  74. }
  75. );
  76. });
  77. compiler.hooks.thisCompilation.tap(
  78. PLUGIN_NAME,
  79. (compilation, { normalModuleFactory }) => {
  80. compilation.dependencyFactories.set(
  81. ContainerEntryDependency,
  82. new ContainerEntryModuleFactory()
  83. );
  84. compilation.dependencyFactories.set(
  85. ContainerExposedDependency,
  86. normalModuleFactory
  87. );
  88. }
  89. );
  90. }
  91. }
  92. module.exports = ContainerPlugin;