index.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { build } from 'esbuild';
  11. import { writeFiles } from 'test-support';
  12. import fs from 'fs';
  13. import NodeModulesPolyfillsPlugin from '.';
  14. require('debug').enable(require('../package.json').name);
  15. test('works', () => __awaiter(void 0, void 0, void 0, function* () {
  16. const { unlink, paths: [ENTRY], } = yield writeFiles({
  17. 'entry.ts': `import {x} from './utils'; console.log(x);`,
  18. 'utils.ts': `import path from 'path'; import { Buffer } from 'buffer'; export const x = path.resolve(Buffer.from('x').toString());`,
  19. });
  20. // const outfile = randomOutputFile()
  21. const res = yield build({
  22. entryPoints: [ENTRY],
  23. write: false,
  24. format: 'esm',
  25. target: 'es2017',
  26. bundle: true,
  27. plugins: [NodeModulesPolyfillsPlugin()],
  28. });
  29. eval(res.outputFiles[0].text);
  30. // console.log(res.outputFiles[0].text)
  31. unlink();
  32. }));
  33. test('works with SafeBuffer and other package consumers', () => __awaiter(void 0, void 0, void 0, function* () {
  34. const { unlink, paths: [ENTRY], } = yield writeFiles({
  35. 'entry.ts': `import {Buffer as SafeBuffer} from './safe-buffer'; console.log(SafeBuffer);`,
  36. 'safe-buffer.ts': fs
  37. .readFileSync(require.resolve('safe-buffer'))
  38. .toString(),
  39. });
  40. // const outfile = randomOutputFile()
  41. const res = yield build({
  42. entryPoints: [ENTRY],
  43. write: false,
  44. format: 'esm',
  45. target: 'es2017',
  46. bundle: true,
  47. plugins: [NodeModulesPolyfillsPlugin()],
  48. });
  49. // console.log(
  50. // res.outputFiles[0].text
  51. // .split('\n')
  52. // .map((x, i) => i + ' ' + x)
  53. // .join('\n'),
  54. // )
  55. eval(res.outputFiles[0].text);
  56. unlink();
  57. }));
  58. test('events works', () => __awaiter(void 0, void 0, void 0, function* () {
  59. const { unlink, paths: [ENTRY], } = yield writeFiles({
  60. 'entry.ts': `
  61. import EventEmitter from 'events';
  62. class Test extends EventEmitter {
  63. constructor() { };
  64. }
  65. console.log(Test)
  66. `,
  67. });
  68. // const outfile = randomOutputFile()
  69. const res = yield build({
  70. entryPoints: [ENTRY],
  71. write: false,
  72. format: 'esm',
  73. target: 'es2017',
  74. bundle: true,
  75. plugins: [NodeModulesPolyfillsPlugin()],
  76. });
  77. // console.log(res.outputFiles[0].text)
  78. eval(res.outputFiles[0].text);
  79. unlink();
  80. }));
  81. test('require can use default export', () => __awaiter(void 0, void 0, void 0, function* () {
  82. const { unlink, paths: [ENTRY], } = yield writeFiles({
  83. 'entry.ts': `
  84. const assert = require('assert')
  85. // console.log(assert)
  86. assert('ok')
  87. `,
  88. });
  89. // const outfile = randomOutputFile()
  90. const res = yield build({
  91. entryPoints: [ENTRY],
  92. write: false,
  93. format: 'esm',
  94. target: 'es2017',
  95. bundle: true,
  96. plugins: [NodeModulesPolyfillsPlugin()],
  97. });
  98. // console.log(res.outputFiles[0].text)
  99. eval(res.outputFiles[0].text);
  100. unlink();
  101. }));
  102. test.skip('crypto', () => __awaiter(void 0, void 0, void 0, function* () {
  103. const { unlink, paths: [ENTRY], } = yield writeFiles({
  104. 'entry.ts': `import { randomBytes } from 'crypto'; console.log(randomBytes(20).toString('hex'))`,
  105. });
  106. // const outfile = randomOutputFile()
  107. const res = yield build({
  108. entryPoints: [ENTRY],
  109. write: false,
  110. format: 'esm',
  111. target: 'es2017',
  112. bundle: true,
  113. plugins: [NodeModulesPolyfillsPlugin()],
  114. });
  115. eval(res.outputFiles[0].text);
  116. // console.log(res.outputFiles[0].text)
  117. unlink();
  118. }));
  119. test.skip('fs', () => __awaiter(void 0, void 0, void 0, function* () {
  120. const { unlink, paths: [ENTRY], } = yield writeFiles({
  121. 'entry.ts': `import { readFile } from 'fs'; console.log(readFile(''))`,
  122. });
  123. // const outfile = randomOutputFile()
  124. const res = yield build({
  125. entryPoints: [ENTRY],
  126. write: false,
  127. format: 'esm',
  128. target: 'es2017',
  129. bundle: true,
  130. plugins: [NodeModulesPolyfillsPlugin()],
  131. });
  132. eval(res.outputFiles[0].text);
  133. // console.log(res.outputFiles[0].text)
  134. unlink();
  135. }));
  136. test('does not include global keyword', () => __awaiter(void 0, void 0, void 0, function* () {
  137. const { unlink, paths: [ENTRY], } = yield writeFiles({
  138. 'entry.ts': `import {x} from './utils'; console.log(x);`,
  139. 'utils.ts': `import path from 'path'; import { Buffer } from 'buffer'; export const x = path.resolve(Buffer.from('x').toString());`,
  140. });
  141. // const outfile = randomOutputFile()
  142. const res = yield build({
  143. entryPoints: [ENTRY],
  144. write: false,
  145. format: 'esm',
  146. target: 'es2017',
  147. bundle: true,
  148. plugins: [NodeModulesPolyfillsPlugin()],
  149. });
  150. const text = res.outputFiles[0].text;
  151. eval(text);
  152. console.log(text);
  153. expect(text).not.toContain(/\bglobal\b/);
  154. // console.log(res.outputFiles[0].text)
  155. unlink();
  156. }));
  157. //# sourceMappingURL=index.test.js.map