index.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { NodeGlobalsPolyfillPlugin } from '.';
  13. require('debug').enable(require('../package.json').name);
  14. test('process works', () => __awaiter(void 0, void 0, void 0, function* () {
  15. const { unlink, paths: [ENTRY], } = yield writeFiles({
  16. 'entry.ts': `process.version`,
  17. });
  18. const res = yield build({
  19. entryPoints: [ENTRY],
  20. write: false,
  21. format: 'esm',
  22. target: 'es2017',
  23. bundle: true,
  24. inject: [require.resolve('../process')],
  25. });
  26. const output = res.outputFiles[0].text;
  27. // console.log(output)
  28. eval(output);
  29. unlink();
  30. }));
  31. test('process is tree shaken', () => __awaiter(void 0, void 0, void 0, function* () {
  32. const { unlink, paths: [ENTRY], } = yield writeFiles({
  33. 'entry.ts': `console.log('hei')`,
  34. });
  35. const res = yield build({
  36. entryPoints: [ENTRY],
  37. write: false,
  38. format: 'esm',
  39. target: 'es2017',
  40. bundle: true,
  41. inject: [require.resolve('../process')],
  42. });
  43. const output = res.outputFiles[0].text;
  44. expect(output).not.toContain('process');
  45. unlink();
  46. }));
  47. test('process env vars are replaced with ones from define', () => __awaiter(void 0, void 0, void 0, function* () {
  48. const { unlink, paths: [ENTRY], } = yield writeFiles({
  49. 'entry.ts': `if (process.env.VAR !== 'hello') { throw new Error('process.env.VAR not right: ' + process.env.VAR) }`,
  50. });
  51. const res = yield build({
  52. entryPoints: [ENTRY],
  53. write: false,
  54. format: 'esm',
  55. target: 'es2017',
  56. bundle: true,
  57. plugins: [
  58. NodeGlobalsPolyfillPlugin({
  59. define: {
  60. 'process.env.VAR': '"hello"',
  61. },
  62. }),
  63. ],
  64. });
  65. const output = res.outputFiles[0].text;
  66. eval(output);
  67. unlink();
  68. }));
  69. test('Buffer works', () => __awaiter(void 0, void 0, void 0, function* () {
  70. const { unlink, paths: [ENTRY], } = yield writeFiles({
  71. 'entry.ts': `console.log(Buffer.from('xxx').toString())`,
  72. });
  73. const res = yield build({
  74. entryPoints: [ENTRY],
  75. write: false,
  76. format: 'esm',
  77. target: 'es2017',
  78. bundle: true,
  79. inject: [require.resolve('../Buffer')],
  80. });
  81. const output = res.outputFiles[0].text;
  82. // console.log(output)
  83. eval(output);
  84. unlink();
  85. }));
  86. test('Buffer is tree shaken', () => __awaiter(void 0, void 0, void 0, function* () {
  87. const { unlink, paths: [ENTRY], } = yield writeFiles({
  88. 'entry.ts': `console.log('hei')`,
  89. });
  90. const res = yield build({
  91. entryPoints: [ENTRY],
  92. write: false,
  93. format: 'esm',
  94. target: 'es2017',
  95. bundle: true,
  96. inject: [require.resolve('../Buffer')],
  97. });
  98. const output = res.outputFiles[0].text;
  99. expect(output).not.toContain('Buffer');
  100. unlink();
  101. }));
  102. test('Buffer works using plugin', () => __awaiter(void 0, void 0, void 0, function* () {
  103. const { unlink, paths: [ENTRY], } = yield writeFiles({
  104. 'entry.ts': `
  105. let buf = new Buffer(256);
  106. let len = buf.write("Simply Easy Learning");
  107. console.log("Octets written : "+ len);`,
  108. });
  109. const res = yield build({
  110. entryPoints: [ENTRY],
  111. write: false,
  112. format: 'esm',
  113. target: 'es2017',
  114. bundle: true,
  115. plugins: [NodeGlobalsPolyfillPlugin({ buffer: true })],
  116. });
  117. const output = res.outputFiles[0].text;
  118. // console.log(output)
  119. eval(output);
  120. unlink();
  121. }));
  122. test('process works using plugin', () => __awaiter(void 0, void 0, void 0, function* () {
  123. const { unlink, paths: [ENTRY], } = yield writeFiles({
  124. 'entry.ts': `console.log(process.cwd())`,
  125. });
  126. const res = yield build({
  127. entryPoints: [ENTRY],
  128. write: false,
  129. format: 'esm',
  130. target: 'es2017',
  131. bundle: true,
  132. plugins: [NodeGlobalsPolyfillPlugin({ process: true })],
  133. });
  134. const output = res.outputFiles[0].text;
  135. // console.log(output)
  136. eval(output);
  137. unlink();
  138. }));
  139. //# sourceMappingURL=index.test.js.map