Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Pack the archive measurement from its own staging folder

Nguyen Xuan An59fde6eecd02
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 0f38ceea..dd995e5c 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -39,6 +39,8 @@ jobs:
key: size-base-${{ steps.base.outputs.sha }}-${{ hashFiles('scripts/measure-size.mjs', 'scripts/size-*.mjs', 'scripts/*tool-cost*.mjs', 'scripts/measure-archive.mjs', 'scripts/build-mcp.mjs', 'vite.lib.config.js', 'package-lock.json') }}
- run: npm run size -- --base "$SIZE_BASE_REF"
- run: npm run tool-cost -- --base "$SIZE_BASE_REF"
+ # The size check packs from its own staging folder, so build/ and build/mcp come from here; the live MCP tests need them.
+ - run: npm run build:lib
- run: npm test
- run: npm run test:isolated
- run: npm run prepare:release
diff --git a/scripts/build-mcp.mjs b/scripts/build-mcp.mjs
index 5e6e8c87..21e40eb6 100644
--- a/scripts/build-mcp.mjs
+++ b/scripts/build-mcp.mjs
@@ -157,12 +157,20 @@ async function reachablePreviewChunks(build, libraryDirectory) {
return chunks;
}
-export async function buildMcp({ outDir = join(root, 'build', 'mcp') } = {}) {
- const destination = resolve(root, outDir);
- const offset = relative(root, destination);
+function requireProjectSubdirectory(directory, label) {
+ const absolute = resolve(root, directory);
+ const offset = relative(root, absolute);
if (!offset || offset === '..' || offset.startsWith(`..`) || isAbsolute(offset)) {
- throw new Error('MCP output must be a dedicated subdirectory of the project root.');
+ throw new Error(`${label} must be a dedicated subdirectory of the project root.`);
}
+ return absolute;
+}
+
+// libraryDir is the folder the library build wrote, which holds the preview chunks; measure-archive.mjs points both
+// folders at its own staging package so it never rewrites build/ while something else reads it.
+export async function buildMcp({ outDir = join(root, 'build', 'mcp'), libraryDir = join(root, 'build') } = {}) {
+ const destination = requireProjectSubdirectory(outDir, 'MCP output');
+ const libraryDirectory = requireProjectSubdirectory(libraryDir, 'The library build folder');
setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);
const { build } = await import('vite');
const metadata = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
@@ -178,7 +186,7 @@ export async function buildMcp({ outDir = join(root, 'build', 'mcp') } = {}) {
build: { write: false, copyPublicDir: false, emptyOutDir: false, outDir: destination, sourcemap: false, minify: true },
};
const legalBanner = '/*! 67js MCP bundle. Complete license texts and notices: ./LICENSES.md */';
- const previewChunks = await reachablePreviewChunks(build, join(root, 'build'));
+ const previewChunks = await reachablePreviewChunks(build, libraryDirectory);
const server = singleModule(await build({
...configuration,
resolve: {
@@ -249,11 +257,19 @@ export async function buildMcp({ outDir = join(root, 'build', 'mcp') } = {}) {
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
const argumentsList = process.argv.slice(2);
- if (argumentsList.length && (argumentsList.length !== 2 || argumentsList[0] !== '--out-dir')) {
- console.error('Use node scripts/build-mcp.mjs [--out-dir <project subdirectory>].');
+ const optionNames = { '--out-dir': 'outDir', '--library-dir': 'libraryDir' };
+ const options = {};
+ let valid = argumentsList.length % 2 === 0;
+ for (let index = 0; valid && index < argumentsList.length; index += 2) {
+ const name = optionNames[argumentsList[index]];
+ if (!name || name in options) valid = false;
+ else options[name] = argumentsList[index + 1];
+ }
+ if (!valid) {
+ console.error('Use node scripts/build-mcp.mjs [--out-dir <project subdirectory>] [--library-dir <project subdirectory>].');
process.exitCode = 1;
} else {
- buildMcp(argumentsList.length ? { outDir: argumentsList[1] } : {}).catch(error => {
+ buildMcp(options).catch(error => {
console.error(error.message);
process.exitCode = 1;
});
diff --git a/scripts/measure-archive.mjs b/scripts/measure-archive.mjs
index 90a9ca8d..ce2ff629 100644
--- a/scripts/measure-archive.mjs
+++ b/scripts/measure-archive.mjs
@@ -1,15 +1,17 @@
// Packs the library the way a release does and prints the archive size: node scripts/measure-archive.mjs --json
// The base measurement of the size check runs it inside a checkout of the base branch, so the numbers come from the base source.
// It builds the library and the MCP server like prepare-release.mjs, then asks npm for the size of the archive without writing one.
+// Each run builds into its own staging package under build/ and packs that, so it never rewrites build/ or build/mcp while
+// another process (a live MCP test, a preview) reads them. build/ is ignored by git, so the MCP manifest still sees a clean tree.
import { spawnSync } from 'node:child_process';
-import { existsSync } from 'node:fs';
-import { dirname, resolve } from 'node:path';
+import { existsSync, mkdirSync, mkdtempSync, copyFileSync, rmSync } from 'node:fs';
+import { dirname, join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
const MAX_OUTPUT_BYTES = 64 * 1024 * 1024;
-function run(arguments_, capture = false) {
- const result = spawnSync(process.execPath, arguments_, { encoding: 'utf8', windowsHide: true, stdio: capture ? 'pipe' : ['ignore', 2, 2], maxBuffer: MAX_OUTPUT_BYTES });
+function run(arguments_, { capture = false, cwd } = {}) {
+ const result = spawnSync(process.execPath, arguments_, { cwd, encoding: 'utf8', windowsHide: true, stdio: capture ? 'pipe' : ['ignore', 2, 2], maxBuffer: MAX_OUTPUT_BYTES });
if (result.status !== 0) throw new Error(`${arguments_.join(' ')} failed: ${result.stderr ?? ''}`);
return result.stdout;
}
@@ -25,14 +27,36 @@ function ensureRepository() {
}
}
+function packedFiles(npmCli, cwd) {
+ const packResult = JSON.parse(run([npmCli, 'pack', '--ignore-scripts', '--dry-run', '--json'], { capture: true, cwd }));
+ return Array.isArray(packResult) ? packResult[0] : Object.values(packResult)[0];
+}
+
+// The staging package holds package.json, every packed file outside build/ and its own fresh build/, so npm applies the same
+// files list, exclusions included, to the same contents a release would pack.
+function copyPackageSources(npmCli, stagingRoot) {
+ for (const { path } of packedFiles(npmCli, '.').files) {
+ if (path.startsWith('build/')) continue;
+ const destination = join(stagingRoot, path);
+ mkdirSync(dirname(destination), { recursive: true });
+ copyFileSync(path, destination);
+ }
+}
+
export function measureArchiveBytes() {
ensureRepository();
- run(['node_modules/vite/bin/vite.js', 'build', '--config', 'vite.lib.config.js']);
- run(['scripts/build-mcp.mjs']);
const npmCli = process.env.npm_execpath ?? resolve(dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js');
- const packResult = JSON.parse(run([npmCli, 'pack', '--ignore-scripts', '--dry-run', '--json'], true));
- const packed = Array.isArray(packResult) ? packResult[0] : Object.values(packResult)[0];
- return packed.size;
+ mkdirSync('build', { recursive: true });
+ const stagingRoot = mkdtempSync(join('build', '.archive-'));
+ try {
+ const stagingBuild = join(stagingRoot, 'build');
+ run(['node_modules/vite/bin/vite.js', 'build', '--config', 'vite.lib.config.js', '--outDir', stagingBuild, '--emptyOutDir']);
+ run(['scripts/build-mcp.mjs', '--out-dir', join(stagingBuild, 'mcp'), '--library-dir', stagingBuild]);
+ copyPackageSources(npmCli, stagingRoot);
+ return packedFiles(npmCli, stagingRoot).size;
+ } finally {
+ rmSync(stagingRoot, { recursive: true, force: true });
+ }
}
// Only when this file is the script that runs: measure-size.mjs imports it and has its own --json output.