index.test.js 6.8 KB

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