feat: minify SDK JavaScript and improve build process
- Implemented minification of the cs-ai-agent-sdk.min.js file using Terser during the build process. - Updated build-sdk.mjs to read the source code, minify it, and write the output to the target directory. - Added logging to display the size reduction after minification. - Removed redundant code and streamlined the configuration handling in the SDK.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { cp, mkdir } from "node:fs/promises";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { minify } from "terser";
|
||||
|
||||
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const rootDir = path.resolve(currentDir, "..");
|
||||
@@ -9,6 +10,27 @@ const targetDir = path.join(rootDir, "public", "sdk");
|
||||
const target = path.join(targetDir, "cs-ai-agent-sdk.min.js");
|
||||
|
||||
await mkdir(targetDir, { recursive: true });
|
||||
await cp(source, target);
|
||||
const sourceCode = await readFile(source, "utf8");
|
||||
const result = await minify(sourceCode, {
|
||||
compress: {
|
||||
passes: 2,
|
||||
},
|
||||
mangle: true,
|
||||
format: {
|
||||
ascii_only: true,
|
||||
comments: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (!result.code) {
|
||||
throw new Error("sdk minify failed: empty output");
|
||||
}
|
||||
|
||||
await writeFile(target, `${result.code}\n`, "utf8");
|
||||
|
||||
const sourceSize = Buffer.byteLength(sourceCode, "utf8");
|
||||
const targetSize = Buffer.byteLength(result.code, "utf8");
|
||||
const reduction = ((1 - targetSize / sourceSize) * 100).toFixed(1);
|
||||
|
||||
console.log(`sdk written to ${target}`);
|
||||
console.log(`sdk minified ${sourceSize} -> ${targetSize} bytes (${reduction}% smaller)`);
|
||||
|
||||
Reference in New Issue
Block a user