NodeTargetPlugin.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. const { builtins } = require("./nodeBuiltins");
  8. /** @import { ExternalsType } from "../../declarations/WebpackOptions" */
  9. /** @import Compiler from "../Compiler" */
  10. class NodeTargetPlugin {
  11. /**
  12. * Creates an instance of NodeTargetPlugin.
  13. * @param {ExternalsType} type default external type
  14. */
  15. constructor(type = "node-commonjs") {
  16. /** @type {ExternalsType} */
  17. this.type = type;
  18. }
  19. /**
  20. * Applies the plugin by registering its hooks on the compiler.
  21. * @param {Compiler} compiler the compiler instance
  22. * @returns {void}
  23. */
  24. apply(compiler) {
  25. new ExternalsPlugin((dependency) => {
  26. // When `require` node.js built-in modules with module output
  27. // we should still emit `createRequire` for compatibility
  28. if (dependency.category === "commonjs") {
  29. return "node-commonjs";
  30. }
  31. return this.type;
  32. }, builtins).apply(compiler);
  33. }
  34. }
  35. NodeTargetPlugin.builtins = builtins;
  36. module.exports = NodeTargetPlugin;