-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ts
177 lines (143 loc) · 4.41 KB
/
build.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { cp, mkdir, rmdirSync } from "fs";
import { readdir } from "fs/promises";
import path from "path";
import { clientImportReplacer } from "./client-import-replacer";
const tempFolderPath = "temp";
mkdir("./" + tempFolderPath, { recursive: true }, (err) => {});
rmdirSync("./app", { recursive: true });
mkdir("./app", { recursive: true }, (err) => {});
const entrypoints: string[] = [];
type File = {
path: string;
isClient: boolean;
isPage: boolean;
};
const entryFiles: File[] = [];
const clientEntryFiles: Map<string, File> = new Map();
const readFolderAndCheck = async (dir: string) => {
dir = "./" + dir;
const files = await readdir(dir);
for (let file of files) {
if (path.extname(file) === ".js") {
continue;
}
if (path.extname(file) === ".purs") {
const fullPath = path.join(dir, file);
const text = await Bun.file(fullPath).text();
const isClient = text.includes("-- use client");
const isPage =
fullPath.endsWith("Page.purs") || fullPath.endsWith("Layout.purs");
const entry = {
path: fullPath.replace("src/", "").replace(".purs", ""),
isClient,
isPage,
};
if (entry.isClient) {
clientEntryFiles.set(entry.path, entry);
}
if (entry.isPage) {
entryFiles.push(entry);
}
} else {
await readFolderAndCheck(path.join(dir, file));
}
}
};
const generateTempPath = (chunks: string[]) => {
let cs = chunks.slice(0, -1);
if (chunks[0] === "app") {
cs = cs.slice(1);
}
return `${tempFolderPath}/${cs.length ? cs.join("/") : ""}`;
};
const readAndCheckJs = async (dir: string) => {
const files = await readdir(dir);
for (let file of files) {
if (path.extname(file) === ".jsx") {
const fullPath = path.join(dir, file);
entrypoints.push(fullPath);
}
if (!path.extname(file)) {
await readAndCheckJs(path.join(dir, file));
}
}
};
const markUseClient = async (dir: string) => {
const files = await readdir(dir);
for (let f of files) {
if (path.extname(f) === ".jsx") {
const fullPath = path.join(dir, f);
let file = await Bun.file(fullPath).text();
if (fullPath.includes("temp/layout.jsx")) {
file = `import '@/globals.css';\n` + file;
}
// if (f.includes("chunk")) {
// file = file.replace("~/chunk-", "~/chunks/chunk-");
// await Bun.write(fullPath, file);
// continue;
// }
file = file.replaceAll("/temp/", "/app/");
await Bun.write(fullPath, file);
}
if (!path.extname(f)) {
await markUseClient(path.join(dir, f));
}
}
};
const modifyMainFiles = async () => {
for (let { path } of entryFiles) {
let file = await Bun.file(
`./output-es/${path.replaceAll("/", ".")}/index.js`,
).text();
file = file.replaceAll("../", import.meta.dir + "/output-es/");
for (let path of Array.from(clientEntryFiles.keys())) {
file = await clientImportReplacer(file, path);
}
const outdirChunks = path.toLowerCase().split("/");
const outdir = "./" + generateTempPath(outdirChunks);
await Bun.write(
`${outdir}/${outdirChunks[outdirChunks.length - 1]}.jsx`,
file,
);
}
};
const modifyClientFiles = async () => {
Array.from(clientEntryFiles.keys()).forEach(async (path) => {
let file = await Bun.file(
`./output-es/${path.replaceAll("/", ".")}/index.js`,
).text();
file = `'use client';\n` + file;
file = file.replaceAll("../", import.meta.dir + "/output-es/");
const chunks = path.toLowerCase().split("/");
file = file.replaceAll(
`output-es/${path.replaceAll("/", ".")}/index.js`,
generateTempPath(chunks) + `/${chunks[chunks.length - 1]}.js`,
);
await Bun.write(
`./${generateTempPath(chunks)}/${chunks[chunks.length - 1]}.jsx`,
file,
);
});
};
const main = async () => {
await readFolderAndCheck("src");
await modifyClientFiles();
await modifyMainFiles();
await readAndCheckJs(tempFolderPath);
// const res = await Bun.build({
// entrypoints,
// outdir: "./app",
// splitting: false,
// naming: {
// asset: "[dir]/[name].[ext]",
// chunk: "chunks/[name]-[hash].[ext]",
// },
// external: ["react", "react-dom"],
// });
await markUseClient("./temp");
await cp("./temp", "./app", { recursive: true }, (err) => {
/* callback */
});
// rmdirSync(tempFolderPath, { recursive: true });
};
main();