with-object.js 805 B

12345678910111213141516171819202122232425
  1. import { globalExternalsWithRegExp } from "./with-reg-exp.js";
  2. /**
  3. * Create a `Plugin` for replacing modules with corresponding global variables.
  4. *
  5. * @param globals Object that maps between the two below:
  6. *
  7. * - From: Module path used in any `import` statements that should be replaced
  8. * with a global variable.
  9. * - To: String for a global variable name, or any `ModuleInfo` object
  10. * which also includes the global variable name.
  11. *
  12. * @example
  13. *
  14. * ```
  15. * const plugins = [globalExternals({ jquery: "$" })];
  16. * ```
  17. */
  18. export const globalExternals = (globals) => {
  19. const normalizedGlobals = {
  20. modulePathFilter: new RegExp(`^(?:${Object.keys(globals).join("|")})$`),
  21. getModuleInfo: (modulePath) => globals[modulePath],
  22. };
  23. return globalExternalsWithRegExp(normalizedGlobals);
  24. };