index.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import inject from 'rollup-plugin-inject';
  2. import { dirname, relative } from 'path';
  3. import { randomBytes } from 'crypto';
  4. const EMPTY_PATH = require.resolve('../polyfills/empty.js');
  5. function builtinsResolver(opts) {
  6. const libs = new Map();
  7. libs.set('process', require.resolve('../polyfills/process-es6'));
  8. libs.set('buffer', require.resolve('../polyfills/buffer-es6'));
  9. libs.set('util', require.resolve('../polyfills/util'));
  10. libs.set('sys', libs.get('util'));
  11. libs.set('events', require.resolve('../polyfills/events'));
  12. libs.set('stream', require.resolve('../polyfills/stream'));
  13. libs.set('path', require.resolve('../polyfills/path'));
  14. libs.set('querystring', require.resolve('../polyfills/qs'));
  15. libs.set('punycode', require.resolve('../polyfills/punycode'));
  16. libs.set('url', require.resolve('../polyfills/url'));
  17. libs.set('string_decoder', require.resolve('../polyfills/string-decoder'));
  18. libs.set('http', require.resolve('../polyfills/http'));
  19. libs.set('https', require.resolve('../polyfills/http'));
  20. libs.set('os', require.resolve('../polyfills/os'));
  21. libs.set('assert', require.resolve('../polyfills/assert'));
  22. libs.set('constants', require.resolve('../polyfills/constants'));
  23. libs.set('_stream_duplex', require.resolve('../polyfills/readable-stream/duplex'));
  24. libs.set('_stream_passthrough', require.resolve('../polyfills/readable-stream/passthrough'));
  25. libs.set('_stream_readable', require.resolve('../polyfills/readable-stream/readable'));
  26. libs.set('_stream_writable', require.resolve('../polyfills/readable-stream/writable'));
  27. libs.set('_stream_transform', require.resolve('../polyfills/readable-stream/transform'));
  28. libs.set('timers', require.resolve('../polyfills/timers'));
  29. libs.set('console', require.resolve('../polyfills/console'));
  30. libs.set('vm', require.resolve('../polyfills/vm'));
  31. libs.set('zlib', require.resolve('../polyfills/zlib'));
  32. libs.set('tty', require.resolve('../polyfills/tty'));
  33. libs.set('domain', require.resolve('../polyfills/domain'));
  34. // not shimmed
  35. libs.set('dns', EMPTY_PATH);
  36. libs.set('dgram', EMPTY_PATH);
  37. libs.set('child_process', EMPTY_PATH);
  38. libs.set('cluster', EMPTY_PATH);
  39. libs.set('module', EMPTY_PATH);
  40. libs.set('net', EMPTY_PATH);
  41. libs.set('readline', EMPTY_PATH);
  42. libs.set('repl', EMPTY_PATH);
  43. libs.set('tls', EMPTY_PATH);
  44. libs.set('fs', EMPTY_PATH);
  45. libs.set('crypto', EMPTY_PATH);
  46. if (opts.fs) {
  47. libs.set('fs', require.resolve('../polyfills/browserify-fs'));
  48. }
  49. if (opts.crypto) {
  50. libs.set('crypto', require.resolve('../polyfills/crypto-browserify'));
  51. }
  52. return (importee) => {
  53. if (importee && importee.slice(-1) === '/') {
  54. importee === importee.slice(0, -1);
  55. }
  56. if (libs.has(importee)) {
  57. return { id: libs.get(importee), moduleSideEffects: false };
  58. }
  59. return null;
  60. };
  61. }
  62. // @ts-ignore
  63. function index (opts = {}) {
  64. const injectPlugin = inject({
  65. include: opts.include === undefined ? 'node_modules/**/*.js' : undefined,
  66. exclude: opts.exclude,
  67. sourceMap: opts.sourceMap,
  68. modules: {
  69. 'process': 'process',
  70. 'Buffer': ['buffer', 'Buffer'],
  71. 'global': GLOBAL_PATH,
  72. '__filename': FILENAME_PATH,
  73. '__dirname': DIRNAME_PATH,
  74. }
  75. });
  76. const basedir = opts.baseDir || '/';
  77. const dirs = new Map();
  78. const resolver = builtinsResolver(opts);
  79. return {
  80. name: 'node-polyfills',
  81. resolveId(importee, importer) {
  82. if (importee === DIRNAME_PATH) {
  83. const id = getRandomId();
  84. dirs.set(id, dirname('/' + relative(basedir, importer)));
  85. return { id, moduleSideEffects: false };
  86. }
  87. if (importee === FILENAME_PATH) {
  88. const id = getRandomId();
  89. dirs.set(id, dirname('/' + relative(basedir, importer)));
  90. return { id, moduleSideEffects: false };
  91. }
  92. return resolver(importee);
  93. },
  94. load(id) {
  95. if (dirs.has(id)) {
  96. return `export default '${dirs.get(id)}'`;
  97. }
  98. },
  99. transform(code, id) {
  100. return injectPlugin.transform.call(this, code, id);
  101. }
  102. };
  103. }
  104. function getRandomId() {
  105. return randomBytes(15).toString('hex');
  106. }
  107. const GLOBAL_PATH = require.resolve('../polyfills/global.js');
  108. const DIRNAME_PATH = '\0node-polyfills:dirname';
  109. const FILENAME_PATH = '\0node-polyfills:filename';
  110. export default index;