DenoTargetPlugin.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const ExternalsPlugin = require("../ExternalsPlugin");
  7. const { coreModules } = require("../node/nodeBuiltins");
  8. /** @import Compiler from "../Compiler" */
  9. // Deno only resolves node.js core modules through the `node:` specifier, so the
  10. // prefix is baked into the external request for both `import` and `require`.
  11. // Deno's own import protocols are resolved by the runtime, never bundled.
  12. const DENO_PROTOCOLS = /^(?:npm|jsr|https?):/;
  13. class DenoTargetPlugin {
  14. /**
  15. * Applies the plugin by registering its hooks on the compiler.
  16. * @param {Compiler} compiler the compiler instance
  17. * @returns {void}
  18. */
  19. apply(compiler) {
  20. new ExternalsPlugin(
  21. (dependency) =>
  22. dependency.category === "commonjs" ? "node-commonjs" : "module-import",
  23. ({ request }, callback) => {
  24. if (!request) return callback();
  25. if (request.startsWith("node:") || DENO_PROTOCOLS.test(request)) {
  26. return callback(null, request);
  27. }
  28. if (coreModules.has(request)) return callback(null, `node:${request}`);
  29. callback();
  30. }
  31. ).apply(compiler);
  32. }
  33. }
  34. module.exports = DenoTargetPlugin;