index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.NodeGlobalsPolyfillPlugin = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const fs_1 = __importDefault(require("fs"));
  9. function NodeGlobalsPolyfillPlugin({ buffer = false, define = {}, process = true, } = {}) {
  10. return {
  11. name: 'node-globals-polyfill',
  12. setup({ initialOptions, onResolve, onLoad }) {
  13. onResolve({ filter: /_node-buffer-polyfill_\.js/ }, (arg) => {
  14. return {
  15. path: path_1.default.resolve(__dirname, '../Buffer.js'),
  16. };
  17. });
  18. onResolve({ filter: /_node-process-polyfill_\.js/ }, (arg) => {
  19. return {
  20. path: path_1.default.resolve(__dirname, '../process.js'),
  21. };
  22. });
  23. onLoad({ filter: /_virtual-process-polyfill_\.js/ }, (arg) => {
  24. const data = fs_1.default
  25. .readFileSync(path_1.default.resolve(__dirname, '../process.js'))
  26. .toString();
  27. const keys = Object.keys(define);
  28. return {
  29. loader: 'js',
  30. contents: data.replace(`const defines = {}`, 'const defines = {\n' +
  31. keys
  32. .filter((x) => x.startsWith('process.'))
  33. .sort((a, b) => a.length - b.length)
  34. .map((k) => ` ${JSON.stringify(k).replace('process.', '')}: ${define[k]},`)
  35. .join('\n') +
  36. '\n}'),
  37. };
  38. });
  39. const polyfills = [];
  40. if (process) {
  41. polyfills.push(path_1.default.resolve(__dirname, '../_virtual-process-polyfill_.js'));
  42. }
  43. if (buffer) {
  44. polyfills.push(path_1.default.resolve(__dirname, '../_buffer.js'));
  45. }
  46. if (initialOptions.inject) {
  47. initialOptions.inject.push(...polyfills);
  48. }
  49. else {
  50. initialOptions.inject = [...polyfills];
  51. }
  52. },
  53. };
  54. }
  55. exports.NodeGlobalsPolyfillPlugin = NodeGlobalsPolyfillPlugin;
  56. exports.default = NodeGlobalsPolyfillPlugin;
  57. //# sourceMappingURL=index.js.map