index.test.js 5.0 KB

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