An open source design system that's fully customizable and agent ready
TypeScript
test(scripts): the English-fallback import-cost test bundles its six components in one build (#9927)
Cindy Zhangeb985e53ea5a
diff --git a/.github/scripts/lib/component-size.js b/.github/scripts/lib/component-size.jsindex 2f8e856213..fbd81e1e38 100644--- a/.github/scripts/lib/component-size.js+++ b/.github/scripts/lib/component-size.js@@ -8,8 +8,9 @@* (the minified entry chunk and the chunks it imports statically, package* imports included) and the gzip size of the stylesheet rules referenced by* the modules it can load, import() targets included- * @position Shared by analyze-pr.js for the PR size report. Sizes are gzip at- * the default compression level (spec:AST-080 FR8,+ * @position Shared by analyze-pr.js for the PR size report, and by the+ * English-fallback import-cost test, which bundles several imports in one+ * build. Sizes are gzip at the default compression level (spec:AST-080 FR8,* architecture:component-style-authoring INV6).*/@@ -65,41 +66,21 @@ const OUT_DIR = '.astryx-size-out';function bundleImportWithInputs(entry, exportName) {if (!entry || !fs.existsSync(entry)) return null;try {- const esbuild = require('esbuild');const workingDir = process.cwd();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 result = esbuild.buildSync({- absWorkingDir: workingDir,- stdin: {- contents: exportName- ? `export {${exportName}} from ${JSON.stringify(path.resolve(entry))};`- : `export * from ${JSON.stringify(path.resolve(entry))};`,- resolveDir,- sourcefile: ENTRY_SOURCE,- loader: 'js',+ const {outputs, byPath} = splitBuild(+ {+ 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,- minify: true,- format: 'esm',- platform: 'browser',- define: {'process.env.NODE_ENV': '"production"'},- loader: {'.css': 'empty'},- write: false,- logLevel: 'silent',- });- const outputs = result.metafile.outputs;- const byPath = new Map(- result.outputFiles.map(file => [- path.relative(workingDir, file.path),- file.text,- ]),+ workingDir,);// A chunk an import() reaches is an entry point too: the import's own// chunk is the one whose entry is the import.@@ -111,31 +92,15 @@ function bundleImportWithInputs(entry, exportName) {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();- const pending = [entryOutput];- while (pending.length > 0) {- const key = pending.pop();- if (key === undefined || seen.has(key)) continue;- seen.add(key);- for (const imported of outputs[key].imports || []) {- if (kinds.includes(imported.kind) && !imported.external) {- pending.push(imported.path);- }- }- }- return seen;- }- let text = '';- for (const key of reachable(['import-statement'])) {- text += byPath.get(key) ?? '';- }+ const text = eagerText(outputs, byPath, entryOutput);// CSS ships in the package stylesheet whether its module loads eagerly or// through import(), so class names come from every chunk the entry can// load, dynamic imports included; chunks no kept code imports are ignored.const inputs = new Set();- for (const key of reachable(['import-statement', 'dynamic-import'])) {+ for (const key of reachableChunks(outputs, entryOutput, [+ 'import-statement',+ 'dynamic-import',+ ])) {for (const input of Object.keys(outputs[key].inputs || {})) {inputs.add(path.resolve(workingDir, input));}@@ -147,6 +112,112 @@ function bundleImportWithInputs(entry, exportName) {}}+// One build as the size report measures an import: minified ESM for the+// browser, without CSS, its outputs in memory. Code splitting keeps modules+// reached only through import() in their own chunks. So an entry chunk and+// the chunks it imports statically are what the import loads eagerly.+// `input` names the entries: `{stdin}` or `{entryPoints}`. Returns the+// metafile outputs and each output's text by its relative path.+function splitBuild(input, workingDir) {+ const esbuild = require('esbuild');+ const result = esbuild.buildSync({+ absWorkingDir: workingDir,+ ...input,+ outdir: path.join(workingDir, OUT_DIR),+ metafile: true,+ bundle: true,+ splitting: true,+ minify: true,+ format: 'esm',+ platform: 'browser',+ define: {'process.env.NODE_ENV': '"production"'},+ loader: {'.css': 'empty'},+ write: false,+ logLevel: 'silent',+ });+ const byPath = new Map(+ result.outputFiles.map(file => [+ path.relative(workingDir, file.path),+ file.text,+ ]),+ );+ return {outputs: result.metafile.outputs, byPath};+}++// Chunks an entry chunk reaches in a build's metafile outputs, following only+// the given import kinds.+function reachableChunks(outputs, entryOutput, kinds) {+ const seen = new Set();+ const pending = [entryOutput];+ while (pending.length > 0) {+ const key = pending.pop();+ if (key === undefined || seen.has(key)) continue;+ seen.add(key);+ for (const imported of outputs[key].imports || []) {+ if (kinds.includes(imported.kind) && !imported.external) {+ pending.push(imported.path);+ }+ }+ }+ return seen;+}++// What importing an entry loads eagerly: its chunk and every chunk it imports+// statically, concatenated.+function eagerText(outputs, byPath, entryOutput) {+ let text = '';+ for (const key of reachableChunks(outputs, entryOutput, [+ 'import-statement',+ ])) {+ text += byPath.get(key) ?? '';+ }+ return text;+}++// Each entry's output key in a build over the entries, by the path the caller+// passed. esbuild names an entry by its path from the working directory, with+// links resolved, so the match compares real paths.+function entryOutputKeys(entries, outputs, workingDir) {+ const byRealPath = new Map();+ for (const [key, output] of Object.entries(outputs)) {+ if (output.entryPoint === undefined) continue;+ byRealPath.set(+ fs.realpathSync(path.resolve(workingDir, output.entryPoint)),+ key,+ );+ }+ const keys = new Map();+ for (const entry of entries) {+ const key = byRealPath.get(fs.realpathSync(entry));+ if (key !== undefined) keys.set(entry, key);+ }+ return keys;+}++// What importing each entry loads eagerly, from one build over every entry.+// The build parses the modules the entries share once, not once per entry. A+// file the entries share goes into one chunk with every part that any entry+// keeps. Each entry is an entry point, so its default export counts too. So+// each text holds at least what the entry's own build loads eagerly. That+// bounds what an import keeps from above, and it is not the import's size.+// Returns a Map of each bundled entry path to its text, or null when the+// entries cannot be bundled together.+function bundleImportsByEntry(entries) {+ const existing = entries.filter(entry => fs.existsSync(entry));+ if (existing.length === 0) return new Map();+ try {+ const workingDir = process.cwd();+ const {outputs, byPath} = splitBuild({entryPoints: existing}, workingDir);+ const texts = new Map();+ for (const [entry, key] of entryOutputKeys(existing, outputs, workingDir)) {+ texts.set(entry, eagerText(outputs, byPath, key));+ }+ return texts;+ } catch {+ return null;+ }+}+// The modules each entry's bundle reaches, import() targets included, from one// build over every entry: Map of absolute entry path to absolute input paths.// Returns null when the entries cannot be bundled together.@@ -169,20 +240,19 @@ function bundleInputsByEntry(entries) {write: false,logLevel: 'silent',});- const reach = new Map();- for (const output of Object.values(result.metafile.outputs)) {- if (output.entryPoint === undefined) continue;- reach.set(- fs.realpathSync(path.resolve(workingDir, output.entryPoint)),- Object.keys(output.inputs).map(input =>- path.resolve(workingDir, input),- ),- );- }- // Key by the paths callers passed, which may differ from real paths.+ const outputs = result.metafile.outputs;+ const keys = entryOutputKeys(existing, outputs, workingDir);const byEntry = new Map();for (const entry of existing) {- byEntry.set(entry, reach.get(fs.realpathSync(entry)) ?? []);+ const key = keys.get(entry);+ byEntry.set(+ entry,+ key === undefined+ ? []+ : Object.keys(outputs[key].inputs).map(input =>+ path.resolve(workingDir, input),+ ),+ );}return byEntry;} catch {@@ -335,5 +405,6 @@ module.exports = {classNamesIn,rulesForClasses,measureComponentImport,+ bundleImportsByEntry,bundleInputsByEntry,};diff --git a/.github/scripts/lib/component-size.test.mjs b/.github/scripts/lib/component-size.test.mjsindex 1e9dc7f09c..792690c736 100644--- a/.github/scripts/lib/component-size.test.mjs+++ b/.github/scripts/lib/component-size.test.mjs@@ -16,6 +16,7 @@ import {afterEach, describe, expect, it, vi} from 'vitest';const require = createRequire(import.meta.url);const {bundleImport,+ bundleImportsByEntry,classNamesIn,measureComponentImport,packageStylesheetPath,@@ -74,6 +75,29 @@ describe('component import size', () => {]);});+ it('bundles several imports in one build, each with at least what its own build loads eagerly', () => {+ const root = tree({+ 'dist/Card/index.js':+ "import {forCard} from '../shared/helper.js';\nexport const Card = () => 'xcard1 ' + forCard();\n",+ 'dist/Menu/index.js':+ "import {forMenu} from '../shared/helper.js';\nexport const Menu = () => 'xmenu1 ' + forMenu() + import('./Touch.js');\n",+ 'dist/Menu/Touch.js': "export const touch = 'LAZY_TOUCH_CODE';\n",+ 'dist/shared/helper.js':+ "export const forCard = () => 'xhelp22';\nexport const forMenu = () => 'xhelp33';\n",+ });+ const card = path.join(root, 'dist/Card/index.js');+ const menu = path.join(root, 'dist/Menu/index.js');+ const texts = bundleImportsByEntry([card, menu]);+ for (const entry of [card, menu]) {+ const shared = classNamesIn(texts.get(entry));+ for (const name of classNamesIn(bundleImport(entry))) {+ expect(shared.has(name), name).toBe(true);+ }+ }+ expect(texts.get(card)).not.toContain('xmenu1');+ expect(texts.get(menu)).not.toContain('LAZY_TOUCH_CODE');+ });+it('keeps only the rules whose selectors name a referenced class, with their at-rules', () => {const css = ['@layer reset, astryx-base, astryx-theme;',diff --git a/scripts/i18n-english-fallback.test.mjs b/scripts/i18n-english-fallback.test.mjsindex bfcacf1535..d8ca79d9e9 100644--- a/scripts/i18n-english-fallback.test.mjs+++ b/scripts/i18n-english-fallback.test.mjs@@ -222,8 +222,10 @@ describe('findWiringProblems', () => {});describe('import cost', () => {- // Same per-component import measurement as the pull-request size report.- const {bundleImport} = require('../.github/scripts/lib/component-size.js');+ // What each component's import loads eagerly, from one build over all six.+ const {+ bundleImportsByEntry,+ } = require('../.github/scripts/lib/component-size.js');const enCatalogFile = path.join(CORE_DIST,'i18n',@@ -245,18 +247,25 @@ describe('import cost', () => {it('bundles only the English a component names, not the whole catalog', () => {const keys = loadCatalogKeys();expect(keys.length).toBeGreaterThan(100);- for (const name of [- 'Button',- 'TreeList',- 'SideNav',- 'Selector',- 'Table',- 'DateInput',- ]) {- const text = bundleImport(path.join(CORE_DIST, name, `${name}.js`));- expect(text, name).not.toBeNull();- const bundled = catalogKeysIn(text, keys);- expect(bundled.length, name).toBeGreaterThan(0);+ const entries = new Map(+ ['Button', 'TreeList', 'SideNav', 'Selector', 'Table', 'DateInput'].map(+ name => [name, path.join(CORE_DIST, name, `${name}.js`)],+ ),+ );+ // Each text holds at least what the component's own import keeps, so the+ // upper bound is sound. The lower bound counts only the component's own+ // keys, which no other component's code can supply.+ const texts = bundleImportsByEntry([...entries.values()]);+ expect(texts).not.toBeNull();+ for (const [name, entry] of entries) {+ const text = texts?.get(entry);+ expect(text, name).toBeDefined();+ const bundled = catalogKeysIn(text ?? '', keys);+ const own = `@astryx.${name[0].toLowerCase()}${name.slice(1)}.`;+ expect(+ bundled.filter(key => key.startsWith(own)).length,+ name,+ ).toBeGreaterThan(0);expect(bundled.length, name).toBeLessThan(keys.length / 4);}});