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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
import { expect, it, describe } from "bun:test";
describe("Bun.Transpiler", () => {
const transpiler = new Bun.Transpiler({
loader: "tsx",
define: {
"process.env.NODE_ENV": JSON.stringify("development"),
},
macro: {
react: {
bacon: `${import.meta.dir}/macro-check.js`,
},
},
platform: "browser",
});
const code = `import { useParams } from "remix";
import type { LoaderFunction, ActionFunction } from "remix";
export const loader: LoaderFunction = async ({
params
}) => {
console.log(params.postId);
};
export const action: ActionFunction = async ({
params
}) => {
console.log(params.postId);
};
export default function PostRoute() {
const params = useParams();
console.log(params.postId);
}
`;
describe("scanImports", () => {
it("reports import paths, excluding types", () => {
const imports = transpiler.scanImports(code);
expect(imports.filter(({ path }) => path === "remix")).toHaveLength(1);
});
});
describe("scan", () => {
it("reports all export names", () => {
const { imports, exports } = transpiler.scan(code);
expect(exports[0]).toBe("action");
expect(exports[2]).toBe("loader");
expect(exports[1]).toBe("default");
expect(exports).toHaveLength(3);
});
});
describe("transform", () => {
it("supports macros", async () => {
const out = await transpiler.transform(`
import {keepSecondArgument} from 'macro:${
import.meta.dir
}/macro-check.js';
export default keepSecondArgument("Test failed", "Test passed");
`);
expect(out.includes("Test failed")).toBe(false);
expect(out.includes("Test passed")).toBe(true);
// ensure both the import and the macro function call are removed
expect(out.includes("keepSecondArgument")).toBe(false);
});
it("sync supports macros", () => {
const out = transpiler.transformSync(`
import {keepSecondArgument} from 'macro:${
import.meta.dir
}/macro-check.js';
export default keepSecondArgument("Test failed", "Test passed");
`);
expect(out.includes("Test failed")).toBe(false);
expect(out.includes("Test passed")).toBe(true);
expect(out.includes("keepSecondArgument")).toBe(false);
});
it("sync supports macros remap", () => {
const out = transpiler.transformSync(`
import {createElement, bacon} from 'react';
export default bacon("Test failed", "Test passed");
export function hi() { createElement("hi"); }
`);
expect(out.includes("Test failed")).toBe(false);
expect(out.includes("Test passed")).toBe(true);
expect(out.includes("bacon")).toBe(false);
expect(out.includes("createElement")).toBe(true);
});
it("macro remap removes import statement if its the only used one", () => {
const out = transpiler.transformSync(`
import {bacon} from 'react';
export default bacon("Test failed", "Test passed");
`);
expect(out.includes("Test failed")).toBe(false);
expect(out.includes("Test passed")).toBe(true);
expect(out.includes("bacon")).toBe(false);
expect(out.includes("import")).toBe(false);
});
it("removes types", () => {
expect(code.includes("ActionFunction")).toBe(true);
expect(code.includes("LoaderFunction")).toBe(true);
const out = transpiler.transformSync(code);
expect(out.includes("ActionFunction")).toBe(false);
expect(out.includes("LoaderFunction")).toBe(false);
const { exports } = transpiler.scan(out);
expect(exports[0]).toBe("action");
expect(exports[2]).toBe("loader");
expect(exports[1]).toBe("default");
expect(exports).toHaveLength(3);
});
});
});
|