-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.optim.ts
315 lines (283 loc) · 8.46 KB
/
build.optim.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { relinka } from "@reliverse/relinka";
import fs from "fs-extra";
import { globby } from "globby";
import path from "pathe";
import { fileURLToPath } from "node:url";
// Parse command-line arguments to check for '--jsr' flag
const args: string[] = process.argv.slice(2);
const isJSR: boolean = args.includes("--jsr");
// Get current directory using import.meta.url
const currentDir = path.dirname(fileURLToPath(import.meta.url));
// Directories based on the presence of '--jsr' flag
const sourceDir: string = path.resolve(currentDir, "src");
const outputDir: string = path.resolve(
currentDir,
isJSR ? "dist-jsr" : "dist-npm",
);
// Separate patterns for files to delete in different modes
const npmFilesToDelete: string[] = [
"**/*.test.js",
"**/*.test.d.ts",
"types/internal.js",
"types/internal.d.ts",
"**/*.temp.js",
"**/*.temp.d.ts",
];
const jsrFilesToDelete: string[] = ["**/*.test.ts", "**/*.temp.ts"];
/**
* Deletes files matching the provided patterns within the base directory.
*/
async function deleteFiles(patterns: string[], baseDir: string): Promise<void> {
try {
const files: string[] = await globby(patterns, {
cwd: baseDir,
absolute: true,
});
if (files.length === 0) {
relinka("info", "No files matched the deletion patterns.");
return;
}
for (const filePath of files) {
try {
await fs.remove(filePath);
relinka("info-verbose", `Deleted: ${filePath}`);
} catch (error) {
relinka(
"error",
`Error deleting file ${filePath}:`,
error instanceof Error ? error.message : String(error),
);
}
}
} catch (error) {
relinka(
"error",
"Error processing deletion patterns:",
error instanceof Error ? error.message : String(error),
);
}
}
/**
* Replaces import paths that use '~/' with relative paths.
* If `isJSR` is true, also replaces '.js' extensions with '.ts'.
* @returns The updated file content with modified import paths.
*/
function replaceImportPaths(
content: string,
fileDir: string,
rootDir: string,
isJSR: boolean,
): string {
let updatedContent = content.replace(
// Matches both static and dynamic imports
/(from\s+['"]|import\s*\(\s*['"])(~\/?[^'"]*)(['"]\s*\)?)/g,
(
_match: string,
prefix: string,
importPath: string,
suffix: string,
): string => {
const relativePathToRoot: string = path.relative(fileDir, rootDir) ?? ".";
// Remove leading '~/' or '~' from importPath
importPath = importPath.replace(/^~\/?/, "");
let newPath: string = path.join(relativePathToRoot, importPath);
// Replace backslashes with forward slashes
newPath = newPath.replace(/\\/g, "/");
// Ensure the path starts with './' or '../'
if (!newPath.startsWith(".")) {
newPath = `./${newPath}`;
}
return `${prefix}${newPath}${suffix}`;
},
);
if (isJSR) {
// Replace '.js' extensions with '.ts' in import paths
// @see https://jsr.io/docs/publishing-packages#relative-imports
updatedContent = updatedContent.replace(/(\.js)(?=['";])/g, ".ts");
relinka("info-verbose", "Replaced '.js' with '.ts' in import paths.");
}
return updatedContent;
}
/**
* Removes comments from the given content string.
* - Strips block comments using `strip-comments`.
* @returns The content without unwanted comments.
*/
// function removeComments(content: string, filePath: string): string {
function removeComments(content: string): string {
// When not in JSR mode, strip all comments using strip-comments
// const stripped = strip(content, {
// line: true,
// block: true,
// keepProtected: true,
// preserveNewlines: false,
// });
// if (debug) {
// relinka("info", `\nProcessing file: ${filePath}`);
// relinka("info", "Stripped all comments.");
// }
return content; // return stripped;
}
/**
* Processes all relevant files in the given directory
* by replacing import paths and removing comments.
*/
async function processFiles(dir: string): Promise<void> {
const files: string[] = await fs.readdir(dir);
for (const file of files) {
const filePath: string = path.join(dir, file);
const stat: fs.Stats = await fs.stat(filePath);
if (stat.isDirectory()) {
await processFiles(filePath);
} else if (
filePath.endsWith(".ts") ||
filePath.endsWith(".tsx") ||
filePath.endsWith(".d.ts") ||
filePath.endsWith(".js") ||
filePath.endsWith(".jsx") ||
filePath.endsWith(".mjs") ||
filePath.endsWith(".cjs") ||
filePath.endsWith(".mts") ||
filePath.endsWith(".cts")
) {
relinka("info-verbose", `\nProcessing file: ${filePath}`);
try {
const content: string = await fs.readFile(filePath, "utf8");
let updatedContent: string = replaceImportPaths(
content,
path.dirname(filePath),
outputDir,
isJSR,
);
if (!isJSR) {
// updatedContent = removeComments(updatedContent, filePath);
updatedContent = removeComments(updatedContent);
}
if (content !== updatedContent) {
await fs.writeFile(filePath, updatedContent, "utf8");
relinka("info-verbose", `Updated file: ${filePath}`);
}
} catch (error) {
relinka(
"error",
`Error processing file ${filePath}:`,
error instanceof Error ? error.message : String(error),
);
}
}
}
}
/**
* Removes the output directory ('dist-jsr' or 'dist-npm') if it exists.
*/
async function removeOutputDirectory(): Promise<void> {
try {
const exists: boolean = await fs.pathExists(outputDir);
if (exists) {
await fs.remove(outputDir);
relinka("info-verbose", `Removed existing '${outputDir}' directory.`);
}
} catch (error) {
relinka(
"error",
`Error removing '${outputDir}' directory:`,
error instanceof Error ? error.message : String(error),
);
throw error;
}
}
/**
* Copies the 'src' directory to the output directory when '--jsr' flag is provided.
*/
async function copySrcToOutput(): Promise<void> {
try {
await fs.copy(sourceDir, outputDir, {
overwrite: true,
errorOnExist: false,
});
relinka("info-verbose", `Copied 'src' to '${outputDir}'`);
} catch (error) {
relinka(
"error",
`Error copying 'src' to '${outputDir}':`,
error instanceof Error ? error.message : String(error),
);
throw error;
}
}
/**
* Renames all .tsx files to -tsx.txt in the specified directory and its subdirectories.
*/
async function renameTsxFiles(dir: string): Promise<void> {
try {
const files = await globby("**/*.tsx", {
cwd: dir,
absolute: true,
});
for (const filePath of files) {
const newPath = filePath.replace(/\.tsx$/, "-tsx.txt");
await fs.rename(filePath, newPath);
relinka("info-verbose", `Renamed: ${filePath} -> ${newPath}`);
}
} catch (error) {
relinka(
"error",
"Error renaming .tsx files:",
error instanceof Error ? error.message : String(error),
);
}
}
/**
* Optimizes the build for production by processing files and deleting unnecessary ones.
*/
async function optimizeBuildForProduction(dir: string): Promise<void> {
if (isJSR) {
relinka(
"info",
"Preparing JSR build by removing existing output directory...",
);
await removeOutputDirectory(); // Remove outputDir before copying
relinka("info", "Copying 'src' to output directory...");
await copySrcToOutput();
relinka("info", "Processing copied files to replace import paths...");
await processFiles(outputDir); // Process files after copying
relinka("info", "Renaming .tsx files to -tsx.txt for JSR compatibility...");
await renameTsxFiles(outputDir);
} else {
relinka("info", "Creating an optimized production build...");
await processFiles(dir);
relinka("info", "Cleaning up unnecessary files...");
const filesToDelete: string[] = isJSR ? jsrFilesToDelete : npmFilesToDelete;
await deleteFiles(filesToDelete, dir);
}
}
async function getDirectorySize(dirPath: string): Promise<number> {
const files = await fs.readdir(dirPath);
let totalSize = 0;
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
totalSize += await getDirectorySize(filePath);
} else {
totalSize += stats.size;
}
}
return totalSize;
}
await optimizeBuildForProduction(outputDir)
.then(() => {
getDirectorySize(outputDir)
.then((size) => {
relinka("info", `Total size of ${outputDir}: ${size} bytes`);
})
.catch((error) => {
relinka(
"error",
`Error calculating directory size for ${outputDir}: ${error instanceof Error ? error.message : "Unknown error"}`,
);
});
})
.catch((error: unknown) => {
relinka("error", error instanceof Error ? error.message : String(error));
});