valueInjectionLoader.js 1.1 KB

123456789101112131415161718192021222324252627
  1. /**
  2. * Set values on the global/window object at the start of a module.
  3. *
  4. * Options:
  5. * - `values`: An object where the keys correspond to the keys of the global values to set and the values
  6. * correspond to the values of the values on the global object. Values must be JSON serializable.
  7. */
  8. function valueInjectionLoader( userCode) {
  9. // We know one or the other will be defined, depending on the version of webpack being used
  10. const { values } = 'getOptions' in this ? this.getOptions() : this.query;
  11. // We do not want to cache injected values across builds
  12. this.cacheable(false);
  13. // Define some global proxy that works on server and on the browser.
  14. let injectedCode =
  15. 'var _sentryCollisionFreeGlobalObject = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : {};\n';
  16. Object.entries(values).forEach(([key, value]) => {
  17. injectedCode += `_sentryCollisionFreeGlobalObject["${key}"] = ${JSON.stringify(value)};\n`;
  18. });
  19. return `${injectedCode}\n${userCode}`;
  20. }
  21. export { valueInjectionLoader as default };
  22. //# sourceMappingURL=valueInjectionLoader.js.map