forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace-node-util.test.ts
More file actions
77 lines (73 loc) · 1.95 KB
/
replace-node-util.test.ts
File metadata and controls
77 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as esbuild from 'esbuild';
import {expect, test} from 'vitest';
import {replaceNodeUtil} from './replace-node-util.ts';
async function compile(contents: string) {
const res = await esbuild.build({
stdin: {
contents,
},
write: false,
plugins: [replaceNodeUtil],
platform: 'node',
format: 'esm',
target: 'esnext',
bundle: true,
minify: true,
});
return res.outputFiles[0].text;
}
test('Should remove dead code', async () => {
expect(
await compile(`import {TextEncoder} from 'util';`),
).toMatchInlineSnapshot(`""`);
expect(
await compile(`import {TextEncoder} from 'node:util';`),
).toMatchInlineSnapshot(`""`);
expect(await compile(`import * as util from 'util';`)).toMatchInlineSnapshot(
`""`,
);
expect(
await compile(`import * as util from 'node:util';`),
).toMatchInlineSnapshot(`""`);
});
test('Should allow different forms of import', async () => {
expect(
await compile(`import {TextEncoder} from 'util';
console.log(TextEncoder);`),
).toMatchInlineSnapshot(`
"var e=TextEncoder;console.log(e);
"
`);
expect(
await compile(`import {TextEncoder} from 'node:util';
console.log(TextEncoder);`),
).toMatchInlineSnapshot(`
"var e=TextEncoder;console.log(e);
"
`);
expect(
await compile(`import * as mod from 'util';
console.log(mod.TextEncoder);`),
).toMatchInlineSnapshot(`
"var o=TextEncoder;console.log(o);
"
`);
expect(
await compile(`import def from 'util';
console.log(def.TextEncoder);`),
).toMatchInlineSnapshot(`
"var o=TextEncoder,e={TextEncoder:o};console.log(e.TextEncoder);
"
`);
});
test('Should allow local reference as well', async () => {
expect(
await compile(`import {TextEncoder as UtilTextEncoder} from 'util';
console.log(UtilTextEncoder);
console.log(TextEncoder);
`),
).toMatchInlineSnapshot(`
"var e=TextEncoder;console.log(e);console.log(TextEncoder);
"
`);
});