index.js 5.0 KB

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