BunTargetPlugin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author sheo13666q @sheo13666q
  4. */
  5. "use strict";
  6. const ExternalsPlugin = require("../ExternalsPlugin");
  7. const { coreModules } = require("../node/nodeBuiltins");
  8. /** @import Compiler from "../Compiler" */
  9. // Bun exposes the node.js core modules; externalize them with the `node:`
  10. // specifier (Bun resolves both forms) like the deno target.
  11. // Bun's own built-in modules (`bun`, `bun:sqlite`, ...) are provided by the
  12. // runtime, never bundled.
  13. const BUN_BUILTINS = /^bun(?::|$)/;
  14. class BunTargetPlugin {
  15. /**
  16. * Applies the plugin by registering its hooks on the compiler.
  17. * @param {Compiler} compiler the compiler instance
  18. * @returns {void}
  19. */
  20. apply(compiler) {
  21. new ExternalsPlugin(
  22. (dependency) =>
  23. dependency.category === "commonjs" ? "node-commonjs" : "module-import",
  24. ({ request }, callback) => {
  25. if (!request) return callback();
  26. if (request.startsWith("node:") || BUN_BUILTINS.test(request)) {
  27. return callback(null, request);
  28. }
  29. if (coreModules.has(request)) return callback(null, `node:${request}`);
  30. callback();
  31. }
  32. ).apply(compiler);
  33. }
  34. }
  35. module.exports = BunTargetPlugin;