index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.NodeModulesPolyfillPlugin = void 0;
  16. const escape_string_regexp_1 = __importDefault(require("escape-string-regexp"));
  17. const fs_1 = __importDefault(require("fs"));
  18. const path_1 = __importDefault(require("path"));
  19. const polyfills_1 = require("./polyfills");
  20. // import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve'
  21. const NAME = 'node-modules-polyfills';
  22. const NAMESPACE = NAME;
  23. function removeEndingSlash(importee) {
  24. if (importee && importee.slice(-1) === '/') {
  25. importee = importee.slice(0, -1);
  26. }
  27. return importee;
  28. }
  29. function NodeModulesPolyfillPlugin(options = {}) {
  30. const { namespace = NAMESPACE, name = NAME } = options;
  31. if (namespace.endsWith('commonjs')) {
  32. throw new Error(`namespace ${namespace} must not end with commonjs`);
  33. }
  34. // this namespace is needed to make ES modules expose their default export to require: require('assert') will give you import('assert').default
  35. const commonjsNamespace = namespace + '-commonjs';
  36. const polyfilledBuiltins = polyfills_1.builtinsPolyfills();
  37. const polyfilledBuiltinsNames = [...polyfilledBuiltins.keys()];
  38. return {
  39. name,
  40. setup: function setup({ onLoad, onResolve, initialOptions }) {
  41. var _a;
  42. // polyfills contain global keyword, it must be defined
  43. if ((initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define) && !((_a = initialOptions.define) === null || _a === void 0 ? void 0 : _a.global)) {
  44. initialOptions.define['global'] = 'globalThis';
  45. }
  46. else if (!(initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define)) {
  47. initialOptions.define = { global: 'globalThis' };
  48. }
  49. // TODO these polyfill module cannot import anything, is that ok?
  50. function loader(args) {
  51. return __awaiter(this, void 0, void 0, function* () {
  52. try {
  53. const isCommonjs = args.namespace.endsWith('commonjs');
  54. const resolved = polyfilledBuiltins.get(removeEndingSlash(args.path));
  55. const contents = yield (yield fs_1.default.promises.readFile(resolved)).toString();
  56. let resolveDir = path_1.default.dirname(resolved);
  57. if (isCommonjs) {
  58. return {
  59. loader: 'js',
  60. contents: commonJsTemplate({
  61. importPath: args.path,
  62. }),
  63. resolveDir,
  64. };
  65. }
  66. return {
  67. loader: 'js',
  68. contents,
  69. resolveDir,
  70. };
  71. }
  72. catch (e) {
  73. console.error('node-modules-polyfill', e);
  74. return {
  75. contents: `export {}`,
  76. loader: 'js',
  77. };
  78. }
  79. });
  80. }
  81. onLoad({ filter: /.*/, namespace }, loader);
  82. onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader);
  83. const filter = new RegExp(polyfilledBuiltinsNames.map(escape_string_regexp_1.default).join('|'));
  84. function resolver(args) {
  85. return __awaiter(this, void 0, void 0, function* () {
  86. const ignoreRequire = args.namespace === commonjsNamespace;
  87. if (!polyfilledBuiltins.has(args.path)) {
  88. return;
  89. }
  90. const isCommonjs = !ignoreRequire && args.kind === 'require-call';
  91. return {
  92. namespace: isCommonjs ? commonjsNamespace : namespace,
  93. path: args.path,
  94. };
  95. });
  96. }
  97. onResolve({ filter }, resolver);
  98. // onResolve({ filter: /.*/, namespace }, resolver)
  99. },
  100. };
  101. }
  102. exports.NodeModulesPolyfillPlugin = NodeModulesPolyfillPlugin;
  103. function commonJsTemplate({ importPath }) {
  104. return `
  105. const polyfill = require('${importPath}')
  106. if (polyfill && polyfill.default) {
  107. module.exports = polyfill.default
  108. for (let k in polyfill) {
  109. module.exports[k] = polyfill[k]
  110. }
  111. } else if (polyfill) {
  112. module.exports = polyfill
  113. }
  114. `;
  115. }
  116. exports.default = NodeModulesPolyfillPlugin;
  117. //# sourceMappingURL=index.js.map