An open source design system that's fully customizable and agent ready
TypeScript
ci: component size writes no temp file and measures the import's chunk (#9778)
Cindy Zhang47790d3a2ac2
diff --git a/.github/scripts/lib/component-size.js b/.github/scripts/lib/component-size.jsindex 622931e8e3..2f8e856213 100644--- a/.github/scripts/lib/component-size.js+++ b/.github/scripts/lib/component-size.js@@ -47,6 +47,15 @@ function packageStylesheetPath(packageDir) {return null;}+// The import under measurement reaches esbuild as stdin, read as if it were+// this file beside the entry. The outputs stay in memory under this directory+// of the working directory, so nothing is written. esbuild reads the listing+// of every directory above each file it bundles. On a shared machine the+// system temp directory can hold hundreds of thousands of entries. A file+// written there for the import cost more than the build itself.+const ENTRY_SOURCE = '__astryx-size-entry__.mjs';+const OUT_DIR = '.astryx-size-out';+// Bundle what importing `entry` loads eagerly: the entry chunk and the chunks// it imports statically, including modules reached through package imports// (other Astryx packages, React, StyleX, third-party libraries), minified for@@ -55,26 +64,25 @@ function packageStylesheetPath(packageDir) {// bundled.function bundleImportWithInputs(entry, exportName) {if (!entry || !fs.existsSync(entry)) return null;- const os = require('node:os');- const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-size-'));try {const esbuild = require('esbuild');const workingDir = process.cwd();- const entryFile = path.join(scratch, 'entry.mjs');- fs.writeFileSync(- entryFile,- exportName- ? `export {${exportName}} from ${JSON.stringify(path.resolve(entry))};`- : `export * from ${JSON.stringify(path.resolve(entry))};`,- );+ const resolveDir = path.dirname(path.resolve(entry));+ const entryFile = path.join(resolveDir, ENTRY_SOURCE);// Code splitting keeps modules reached only through import() in their own// chunks, so the figure counts what the import loads eagerly: the entry// chunk and every chunk it imports statically.- const outdir = path.join(scratch, 'out');const result = esbuild.buildSync({absWorkingDir: workingDir,- entryPoints: [entryFile],- outdir,+ stdin: {+ contents: exportName+ ? `export {${exportName}} from ${JSON.stringify(path.resolve(entry))};`+ : `export * from ${JSON.stringify(path.resolve(entry))};`,+ resolveDir,+ sourcefile: ENTRY_SOURCE,+ loader: 'js',+ },+ outdir: path.join(workingDir, OUT_DIR),metafile: true,bundle: true,splitting: true,@@ -93,9 +101,16 @@ function bundleImportWithInputs(entry, exportName) {file.text,]),);+ // A chunk an import() reaches is an entry point too: the import's own+ // chunk is the one whose entry is the import.+ const entryPoint = path+ .relative(workingDir, entryFile)+ .split(path.sep)+ .join('/');const entryOutput = Object.keys(outputs).find(- key => outputs[key].entryPoint !== undefined,+ key => outputs[key].entryPoint === entryPoint,);+ if (entryOutput === undefined) return null;// Chunks the entry reaches, following only the given import kinds.function reachable(kinds) {const seen = new Set();@@ -129,8 +144,6 @@ function bundleImportWithInputs(entry, exportName) {return {text, inputs: [...inputs]};} catch {return null;- } finally {- fs.rmSync(scratch, {recursive: true, force: true});}}@@ -140,15 +153,13 @@ function bundleImportWithInputs(entry, exportName) {function bundleInputsByEntry(entries) {const existing = entries.filter(entry => fs.existsSync(entry));if (existing.length === 0) return new Map();- const os = require('node:os');- const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-reach-'));try {const esbuild = require('esbuild');const workingDir = process.cwd();const result = esbuild.buildSync({absWorkingDir: workingDir,entryPoints: existing,- outdir: scratch,+ outdir: path.join(workingDir, OUT_DIR),metafile: true,bundle: true,format: 'esm',@@ -176,8 +187,6 @@ function bundleInputsByEntry(entries) {return byEntry;} catch {return null;- } finally {- fs.rmSync(scratch, {recursive: true, force: true});}}diff --git a/.github/scripts/lib/component-size.test.mjs b/.github/scripts/lib/component-size.test.mjsindex 2566ddf3d1..1e9dc7f09c 100644--- a/.github/scripts/lib/component-size.test.mjs+++ b/.github/scripts/lib/component-size.test.mjs@@ -9,9 +9,9 @@import fs from 'node:fs';import {createRequire} from 'node:module';-import os from 'node:os';import path from 'node:path';-import {afterEach, describe, expect, it} from 'vitest';+import {fileURLToPath} from 'node:url';+import {afterEach, describe, expect, it, vi} from 'vitest';const require = createRequire(import.meta.url);const {@@ -22,9 +22,18 @@ const {rulesForClasses,} = require('./component-size.js');+// Fixture trees live under the repository's `node_modules/.cache`, not the+// system temp directory. esbuild reads the listing of every directory above+// each file it bundles. On a shared machine the temp directory can hold+// hundreds of thousands of entries, which cost more than the builds.+const FIXTURES = path.join(+ path.dirname(fileURLToPath(import.meta.url)),+ '../../../node_modules/.cache/astryx-component-size',+);const roots = [];function tree(files) {- const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-size-'));+ fs.mkdirSync(FIXTURES, {recursive: true});+ const root = fs.mkdtempSync(path.join(FIXTURES, 'tree-'));roots.push(root);for (const [file, content] of Object.entries(files)) {fs.mkdirSync(path.dirname(path.join(root, file)), {recursive: true});@@ -122,6 +131,28 @@ describe('component import size', () => {expect(js).toContain('import(');});+ it("measures the import's own chunk when an import() target's chunk comes first", () => {+ // Each import() target is an entry point too. With eight of them, one+ // target's chunk comes before the import's own in esbuild's outputs.+ const files = {+ 'dist/Menu/index.js': [+ "export function Menu() { return 'MENU_OWN_CODE'; }",+ ...Array.from(+ {length: 8},+ (_, i) => `export const load${i} = () => import('./Lazy${i}.js');`,+ ),+ ].join('\n'),+ };+ for (let i = 0; i < 8; i += 1) {+ files[`dist/Menu/Lazy${i}.js`] =+ `export const lazy = 'LAZY_${i}_CODE';\n`;+ }+ const root = tree(files);+ const js = bundleImport(path.join(root, 'dist/Menu/index.js'));+ expect(js).toContain('MENU_OWN_CODE');+ expect(js).not.toMatch(/LAZY_\d_CODE/);+ });+it('counts CSS a lazily loaded module references, since it ships in the stylesheet', () => {const root = tree({'dist/Menu/index.js':@@ -172,6 +203,26 @@ describe('component import size', () => {);});+ it('writes no file to measure an import', () => {+ const root = tree({+ 'dist/Card/index.js': "export const Card = () => 'xcard1';\n",+ });+ const temps = vi.spyOn(fs, 'mkdtempSync');+ const writes = vi.spyOn(fs, 'writeFileSync');+ try {+ const size = measureComponentImport(+ path.join(root, 'dist/Card/index.js'),+ '.xcard1{color:red}',+ );+ expect(size.importGzipBytes).toBeGreaterThan(0);+ expect(temps).not.toHaveBeenCalled();+ expect(writes).not.toHaveBeenCalled();+ } finally {+ temps.mockRestore();+ writes.mockRestore();+ }+ });+it('measures gzip JavaScript and CSS, and null for a missing entry', () => {const root = tree({'package.json': JSON.stringify({