ContainerPlugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if (!compiler.options.output.enabledLibraryTypes.includes(library.type)) {
  59. compiler.options.output.enabledLibraryTypes.push(library.type);
  60. }
  61. compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
  62. const dep = new ContainerEntryDependency(name, exposes, shareScope);
  63. dep.loc = { name };
  64. compilation.addEntry(
  65. compilation.options.context,
  66. dep,
  67. {
  68. name,
  69. filename,
  70. runtime,
  71. library
  72. },
  73. error => {
  74. if (error) return callback(error);
  75. callback();
  76. }
  77. );
  78. });
  79. compiler.hooks.thisCompilation.tap(
  80. PLUGIN_NAME,
  81. (compilation, { normalModuleFactory }) => {
  82. compilation.dependencyFactories.set(
  83. ContainerEntryDependency,
  84. new ContainerEntryModuleFactory()
  85. );
  86. compilation.dependencyFactories.set(
  87. ContainerExposedDependency,
  88. normalModuleFactory
  89. );
  90. }
  91. );
  92. }
  93. }
  94. module.exports = ContainerPlugin;