with-reg-exp.js 917 B

123456789101112131415161718192021222324252627
  1. import { normalizeModuleInfo } from "./module-info.js";
  2. import { createContents } from "./on-load.js";
  3. const PLUGIN_NAME = "global-externals";
  4. /**
  5. * Create a `Plugin` for replacing modules with corresponding global variables.
  6. *
  7. * @param globals See type declaration.
  8. */
  9. export const globalExternalsWithRegExp = (globals) => {
  10. const { modulePathFilter, getModuleInfo } = globals;
  11. return {
  12. name: PLUGIN_NAME,
  13. setup(build) {
  14. build.onResolve({ filter: modulePathFilter }, (args) => ({
  15. path: args.path,
  16. namespace: PLUGIN_NAME,
  17. }));
  18. build.onLoad({ filter: /.*/, namespace: PLUGIN_NAME }, (args) => {
  19. // eslint-disable-next-line total-functions/no-unsafe-type-assertion
  20. const modulePath = args.path;
  21. const moduleInfo = normalizeModuleInfo(getModuleInfo(modulePath));
  22. return { contents: createContents(moduleInfo) };
  23. });
  24. },
  25. };
  26. };