-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
271 lines (247 loc) · 9.46 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import debugbreak from "debugbreak";
import "./src/main";
import fs from "fs";
async function* walk(dir: string): AsyncGenerator<string> {
for await (const d of await fs.promises.opendir(dir)) {
const entry = `${dir}/${d.name}`;
if (entry.includes("/node_modules/")) continue;
if (entry.includes("/.git/")) continue;
if (entry.includes("/build/")) continue;
if (d.isDirectory()) {
yield* walk(entry);
} else if (d.isFile()) {
yield entry;
}
}
}
async function recursiveDelete(dir: string) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = `${dir}/${d.name}`;
if (d.isDirectory()) {
await recursiveDelete(entry);
} else if (d.isFile()) {
await fs.promises.unlink(entry);
}
}
await fs.promises.rmdir(dir);
}
setImmediate(async () => {
await build();
});
async function build() {
let time = Date.now();
let target = "./build/";
await fs.promises.mkdir(target, { recursive: true });
//await recursiveDelete(target);
let pathsToCopy: string[] = [];
let curDir = __dirname.replaceAll("\\", "/");
for (let module of Object.values(require.cache)) {
if (!module) continue;
let path = module.filename.replaceAll("\\", "/");
if (path.includes("/node_modules/typenode/")) continue;
if (path.includes("/node_modules/typescript/")) continue;
if (path === __filename.replaceAll("\\", "/")) continue;
if (path.endsWith(".ts") || path.endsWith(".tsx")) {
let dir = path.split("/").slice(0, -1).join("/");
let name = path.split("/").slice(-1)[0];
let cachePath = `${dir}/dist/${name}.cache`;
if (fs.existsSync(cachePath)) {
path = cachePath;
}
}
pathsToCopy.push(path);
}
// Recursive read files, and copy all .png and .html files
for await (let path of walk(curDir)) {
if (path.endsWith(".png") || path.endsWith(".html")) {
pathsToCopy.push(path);
}
}
let unchanged = 0;
for (let path of pathsToCopy) {
path = path.replace(curDir, "");
let newPath = target + path;
newPath = newPath.replaceAll("//", "/");
// Copy the file, creating parent directories if needed
let dir = newPath.split("/").slice(0, -1).join("/");
let fixImports = false;
if (newPath.endsWith(".cache")) {
if (dir.endsWith("/dist")) {
dir = dir.slice(0, -"/dist".length);
newPath = newPath.replace("/dist/", "/");
}
if (newPath.endsWith(".ts.cache")) {
newPath = newPath.replace(".ts.cache", ".js");
fixImports = true;
}
if (newPath.endsWith(".tsx.cache")) {
newPath = newPath.replace(".tsx.cache", ".js");
fixImports = true;
}
}
// preact uses exports, as do probably others. So... just fix imports on everything...
if (newPath.endsWith(".js")) {
fixImports = true;
}
if (!fs.existsSync(dir)) {
await fs.promises.mkdir(dir, { recursive: true });
}
if (fixImports) {
let source = curDir + path;
async function getMTime(path: string): Promise<number> {
try {
return (await fs.promises.stat(path)).mtimeMs;
} catch {
return 0;
}
}
let sourceMTime = await getMTime(source);
let newMTime = await getMTime(newPath);
// Probably unchanged, so just leave it
if (newMTime > sourceMTime) {
unchanged++;
continue;
}
let contents = await fs.promises.readFile(source, "utf8");
contents = contents.replaceAll(`Object.defineProperty(exports, "__esModule", { value: true , configurable: true});`, "");
contents = convertExportsToExport(contents);
contents = convertImports(contents, newPath);
await fs.promises.writeFile(newPath, contents);
} else {
await fs.promises.copyFile(curDir + path, newPath);
}
}
let end = Date.now();
console.log(`Copied ${pathsToCopy.length - unchanged} in ${end - time}ms at ${new Date().toLocaleTimeString()}`);
}
function convertExportsToExport(code: string): string {
let hasExports = code.includes("exports.") || code.includes("module.exports");
if (!hasExports) return code;
return `
let exports = {};
let module = { exports };
${code}
;
export default exports;
`;
}
function convertImports(code: string, path: string): string {
const lines = code.split("\n");
let braceCount = 0;
let inString = false;
let inComment = false;
let stringChar = "";
const processedLines = lines.map((line) => {
let shouldReplace = braceCount === 0;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (!inComment && !inString) {
if (char === "{") braceCount++;
else if (char === "}") braceCount--;
else if (char === "\"" || char === "'" || char === "`") {
inString = true;
stringChar = char;
}
else if (char === "/" && line[i + 1] === "/") {
break; // Rest of the line is a comment
}
else if (char === "/" && line[i + 1] === "*") {
inComment = true;
i++; // Skip next character
}
} else if (inString) {
if (char === stringChar) {
if (stringChar === "`" || line[i - 1] !== "\\") {
inString = false;
}
}
} else if (inComment) {
if (char === "*" && line[i + 1] === "/") {
inComment = false;
i++; // Skip next character
}
break; // Rest of the line is a comment
}
}
return shouldReplace ? replaceLine(code, path, line) : line;
});
code = processedLines.join("\n");
// Strip source map, because we broke it
code = code.replace(/\/\/# sourceMappingURL=.+/, "");
return code;
}
function replaceLine(code: string, path: string, line: string) {
return line.replace(
/const (\w+) = __importDefault\(require\("(.+?)"\)\);/g,
(_, variableName, importPath: string) => {
return createImport({ variableName, importPath, filePath: path, code });
}
).replace(
/Promise\.resolve\(\)\.then\(\(\) => __importStar\(require\("(.+?)"\)\)\);/g,
(_, path) => `import("${path}");`
).replace(
// const mobx = __importStar(require("mobx/dist/mobx.cjs.development.js"));
/const (\w+) = __importStar\(require\("(.+?)"\)\);/g,
// import * as mobx from "mobx/dist/mobx.cjs.development.js";
(_, variableName, importPath) => {
return createImport({ variableName, importPath, filePath: path, code });
}
).replace(
/const\s+(\w+)\s*=\s*require\(\s*["'](.+)["']\s*\);?\s*/g,
(_, variableName, importPath) => {
return createImport({ variableName, importPath, filePath: path, code });
}
).replace(
// const { ? } = require("?");
/const\s+(.+?)\s*=\s*require\(\s*["'](.+?)["']\s*\);?\s*/g,
(_, variableName, importPath) => {
let fileNameVariable = importPath.replaceAll("/", "_").replace(/\W/g, "");
return (
createImport({ variableName: fileNameVariable, importPath, filePath: path, code, forceDefault: true })
+ `\nconst ${variableName} = ${fileNameVariable}.default || ${fileNameVariable};`
);
}
).replace(
/^require\(\s*["'](.+)["']\s*\);?\s*/g,
(_, importPath) => {
return `\nimport "${importPath}";`;
}
);
}
function createImport(config: {
variableName: string;
importPath: string;
filePath: string;
code: string;
forceDefault?: boolean;
}) {
let modulePath = config.importPath;
let variableName = config.variableName;
let path = config.filePath;
let code = config.code;
let forceDefault = config.forceDefault;
if (!modulePath.startsWith(".")) {
let fullPath = require.resolve(modulePath);
if (!fullPath.includes(".")) {
// It's a built-in module, So... remove it/
return ``;
} else {
let relativePath = fullPath.replace(__dirname, ".");
modulePath = relativePath.replaceAll("\\", "/");
let depth = path.split("/").length - 3;
if (depth > 0) {
modulePath = "../".repeat(depth) + modulePath;
}
}
}
if (modulePath.endsWith(".ts") || modulePath.endsWith(".tsx")) {
modulePath = modulePath.replace(/\.tsx?$/, "");
}
let usesDefaultExportsExplicitly = code.includes(`${variableName}.default.`);
const modulePathWithExtension = modulePath.endsWith(".js") ? modulePath : `${modulePath}.js`;
if (!usesDefaultExportsExplicitly || forceDefault) {
return `import ${variableName} from "${modulePathWithExtension}";`;
} else {
return `import * as ${variableName} from "${modulePathWithExtension}";`;
}
}