index.js 4.7 KB

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