ElectronTargetPlugin.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ExternalsPlugin = require("../ExternalsPlugin");
  7. /** @import { ExternalsType } from "../../declarations/WebpackOptions" */
  8. /** @import Compiler from "../Compiler" */
  9. /** @import Dependency from "../Dependency" */
  10. /** @typedef {"main" | "preload" | "renderer"} ElectronContext */
  11. const SHARED_MODULES = [
  12. "clipboard",
  13. "crash-reporter",
  14. "electron",
  15. "ipc",
  16. "native-image",
  17. "original-fs",
  18. "screen",
  19. "shell"
  20. ];
  21. const MAIN_MODULES = [
  22. "app",
  23. "auto-updater",
  24. "browser-window",
  25. "content-tracing",
  26. "dialog",
  27. "global-shortcut",
  28. "ipc-main",
  29. "menu",
  30. "menu-item",
  31. "power-monitor",
  32. "power-save-blocker",
  33. "protocol",
  34. "session",
  35. "tray",
  36. "web-contents"
  37. ];
  38. const RENDERER_MODULES = [
  39. "desktop-capturer",
  40. "ipc-renderer",
  41. "remote",
  42. "web-frame"
  43. ];
  44. class ElectronTargetPlugin {
  45. /**
  46. * @param {ElectronContext=} context in main, preload or renderer context?
  47. * @param {ExternalsType=} type default external type
  48. */
  49. constructor(context, type = "node-commonjs") {
  50. /** @type {ElectronContext | undefined} */
  51. this._context = context;
  52. /** @type {ExternalsType} */
  53. this.type = type;
  54. /**
  55. * @param {Dependency} dependency the dependency
  56. * @returns {ExternalsType} the external type
  57. */
  58. this._externalType = (dependency) =>
  59. // When `require`-ing electron built-in modules with module output
  60. // we should still emit `node-commonjs` for compatibility
  61. dependency.category === "commonjs" ? "node-commonjs" : this.type;
  62. }
  63. /**
  64. * Applies the plugin by registering its hooks on the compiler.
  65. * @param {Compiler} compiler the compiler instance
  66. * @returns {void}
  67. */
  68. apply(compiler) {
  69. const externals = [...SHARED_MODULES];
  70. switch (this._context) {
  71. case "main":
  72. externals.push(...MAIN_MODULES);
  73. break;
  74. case "preload":
  75. case "renderer":
  76. externals.push(...RENDERER_MODULES);
  77. break;
  78. }
  79. new ExternalsPlugin(this._externalType, externals).apply(compiler);
  80. }
  81. }
  82. module.exports = ElectronTargetPlugin;