An open source design system that's fully customizable and agent ready
TypeScript
fix(build): scan what each Astryx package publishes, so a linked workspace builds (#7775)
Christopher Chedeaua19fceb4713c
diff --git a/.changeset/build-postcss-library-glob-cycles.md b/.changeset/build-postcss-library-glob-cycles.mdnew file mode 100644index 0000000000..b86465c8df--- /dev/null+++ b/.changeset/build-postcss-library-glob-cycles.md@@ -0,0 +1,6 @@+---+'@astryxdesign/build': patch+---++[fix] The PostCSS plugin now scans what each installed Astryx package publishes (its `files` field, or the whole package minus nested `node_modules` when it has none) instead of everything under `node_modules/@astryxdesign`. A package linked from a workspace no longer drags in its own `node_modules`, whose links can cycle back to it and made the build run out of memory, or its unpublished test fixtures. Registry installs scan the same files as before.+@vjeuxdiff --git a/packages/build/src/index.js b/packages/build/src/index.jsindex 94dca89405..6268b4cbad 100644--- a/packages/build/src/index.js+++ b/packages/build/src/index.js@@ -11,6 +11,10 @@** reset < astryx-base (library, prefix: 'astryx') < astryx-theme < product (prefix: 'x')*+ * Library source is what each installed Astryx package publishes (its+ * `files`), so a package linked from a workspace contributes what a registry+ * install of it would.+ ** The separate prefixes ensure atomic classes don't collide between* layers, which would break theme overrides. Generated replacement CSS is* parsed by the host PostCSS instance so consumers do not load a second copy.@@ -20,13 +24,14 @@ const path = require('node:path');const fs = require('node:fs');const babel = require('@babel/core');const stylexBabelPlugin = require('@stylexjs/babel-plugin');-const {globSync} = require('fast-glob');+const {escapePath, globSync} = require('fast-glob');const isGlob = require('is-glob');const globParent = require('glob-parent');const PLUGIN_NAME = '@astryxdesign/postcss-plugin';-const LIBRARY_GLOB = 'node_modules/@astryxdesign/**/*.{ts,tsx}';+const LIBRARY_SCOPE = 'node_modules/@astryxdesign';+const LIBRARY_SOURCE = /\.tsx?$/;const LIBRARY_PATTERNS = ['node_modules/@astryxdesign/','packages/core/',@@ -34,6 +39,54 @@ const LIBRARY_PATTERNS = [];const STYLEX_IMPORT_SOURCE = '@stylexjs/stylex';+/**+ * Globs for the source each installed Astryx package publishes: each `files`+ * entry, a directory (the files below it) or a file, or the whole package when+ * it has none; an entry with glob characters or a leading `!` is refused, not+ * approximated. A package's own `node_modules` is never scanned: in a+ * workspace its links can lead back to the package (here, core's dev+ * dependency `@astryxdesign/cli` links core again).+ */+function libraryGlobs(cwd) {+ const include = [];+ const ignore = [];+ const scope = path.join(cwd, LIBRARY_SCOPE);+ const seen = new Set();+ for (const name of fs.existsSync(scope) ? fs.readdirSync(scope) : []) {+ const dir = path.join(scope, name);+ const manifest = path.join(dir, 'package.json');+ if (!fs.existsSync(manifest)) continue;+ const real = fs.realpathSync(dir);+ if (seen.has(real)) continue;+ seen.add(real);+ const base = `${LIBRARY_SCOPE}/${name}`;+ ignore.push(`${base}/**/node_modules/**`);+ const {files} = JSON.parse(fs.readFileSync(manifest, 'utf8'));+ if (!Array.isArray(files)) {+ include.push(`${base}/**/*.{ts,tsx}`);+ continue;+ }+ for (const entry of files) {+ // isGlob also flags a leading `!`.+ if (isGlob(entry)) {+ throw new Error(+ `@astryxdesign/build reads plain files entries only; @astryxdesign/${name} lists "${entry}"`,+ );+ }+ const plain = entry.replace(/^\.?\/|\/$/g, '');+ const target = path.join(dir, plain);+ const isDirectory =+ fs.existsSync(target) && fs.statSync(target).isDirectory();+ include.push(+ isDirectory+ ? `${base}/${escapePath(plain)}/**/*.{ts,tsx}`+ : `${base}/${escapePath(plain)}`,+ );+ }+ }+ return {include, ignore};+}+function parseDependency(fileOrGlob, cwd) {if (fileOrGlob.startsWith('!')) return null;if (isGlob(fileOrGlob)) {@@ -91,11 +144,7 @@ function createPlugin() {libraryPatterns = LIBRARY_PATTERNS,exclude = [],}) => {- const include = [- `${appDir}/**/*.{js,jsx,ts,tsx}`,- LIBRARY_GLOB,- ...extraInclude,- ];+ const appInclude = `${appDir}/**/*.{js,jsx,ts,tsx}`;const excludeWithDefaults = ['**/*.d.ts', '**/*.flow', ...exclude];@@ -128,18 +177,26 @@ function createPlugin() {});if (styleXAtRule == null) return;- // Discover files+ // Discover files: the app's own, and the source each installed+ // Astryx package publishes+ const library = libraryGlobs(cwd);const files = new Set();- for (const pattern of include) {- const matched = globSync(pattern, {- onlyFiles: true,- ignore: excludeWithDefaults,- cwd,- });- for (const f of matched) {- files.add(path.normalize(path.resolve(cwd, f)));+ const discover = (patterns, ignore, keep) => {+ for (const pattern of patterns) {+ const matched = globSync(pattern, {onlyFiles: true, ignore, cwd});+ for (const f of matched) {+ if (keep(f)) files.add(path.normalize(path.resolve(cwd, f)));+ }}- }+ };+ const include = [appInclude, ...library.include, ...extraInclude];+ discover([appInclude], excludeWithDefaults, () => true);+ discover(+ library.include,+ [...excludeWithDefaults, ...library.ignore],+ f => LIBRARY_SOURCE.test(f),+ );+ discover(extraInclude, excludeWithDefaults, () => true);// Watch dependenciesfor (const pattern of include) {diff --git a/packages/build/src/postcss.workspace-links.test.mjs b/packages/build/src/postcss.workspace-links.test.mjsnew file mode 100644index 0000000000..f96eb92f19--- /dev/null+++ b/packages/build/src/postcss.workspace-links.test.mjs@@ -0,0 +1,163 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file postcss.workspace-links.test.mjs+ * @input The PostCSS plugin, run from apps whose Astryx packages are linked+ * from a workspace where package links form cycles+ * @output Proves the plugin compiles what each installed package publishes,+ * once, never walks a package's own `node_modules`, and refuses `files`+ * entries it cannot read faithfully+ * @position Regression test for @astryxdesign/build's PostCSS library scan+ */++import {afterAll, beforeAll, describe, expect, it} from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import {createRequire} from 'node:module';++const require = createRequire(import.meta.url);+const astryxPostcss = require('./index.js');++let root;++const stylexModule = color =>+ [+ "import * as stylex from '@stylexjs/stylex';",+ `export const styles = stylex.create({root: {color: '${color}'}});`,+ '',+ ].join('\n');++/**+ * A pnpm-style workspace. Core publishes `src` and also carries a test fixture;+ * its dev dependency, the CLI, links back to it. The theme declares no `files`,+ * and its own dependency links to core. Two more list a glob and a `!` entry.+ */+beforeAll(() => {+ root = fs.realpathSync(+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-postcss-links-')),+ );+ const manifest = (name, files) =>+ JSON.stringify(files ? {name, files} : {name});+ const files = {+ 'packages/core/package.json': manifest('@astryxdesign/core', [+ 'src',+ 'tokens.ts',+ ]),+ 'packages/core/src/Box/Box.tsx': stylexModule('red'),+ 'packages/core/tokens.ts': stylexModule('yellow'),+ 'packages/core/__fixtures__/invalid/main.ts': stylexModule('green'),+ 'packages/cli/package.json': manifest('@astryxdesign/cli', ['src']),+ 'packages/cli/src/command.ts': stylexModule('blue'),+ 'packages/theme/package.json': manifest('@astryxdesign/theme'),+ 'packages/theme/src/theme.ts': stylexModule('purple'),+ 'packages/globbed/package.json': manifest('@astryxdesign/globbed', [+ 'src/*',+ ]),+ 'packages/globbed/src/index.ts': stylexModule('orange'),+ 'packages/negated/package.json': manifest('@astryxdesign/negated', [+ 'src',+ '!src/internal',+ ]),+ 'packages/negated/src/index.ts': stylexModule('teal'),+ 'apps/site/src/page.tsx': '',+ 'apps/themed/src/page.tsx': '',+ 'apps/globbed/src/page.tsx': '',+ 'apps/negated/src/page.tsx': '',+ };+ for (const [file, contents] of Object.entries(files)) {+ fs.mkdirSync(path.dirname(path.join(root, file)), {recursive: true});+ fs.writeFileSync(path.join(root, file), contents);+ }+ const links = {+ 'apps/site/node_modules/@astryxdesign/core': 'packages/core',+ 'apps/themed/node_modules/@astryxdesign/theme': 'packages/theme',+ 'apps/globbed/node_modules/@astryxdesign/globbed': 'packages/globbed',+ 'apps/negated/node_modules/@astryxdesign/negated': 'packages/negated',+ 'packages/core/node_modules/@astryxdesign/cli': 'packages/cli',+ 'packages/cli/node_modules/@astryxdesign/core': 'packages/core',+ 'packages/theme/node_modules/@astryxdesign/core': 'packages/core',+ };+ for (const [link, target] of Object.entries(links)) {+ fs.mkdirSync(path.dirname(path.join(root, link)), {recursive: true});+ fs.symlinkSync(path.join(root, target), path.join(root, link), 'junction');+ }+});++afterAll(() => {+ fs.rmSync(root, {recursive: true, force: true});+});++/**+ * Run the plugin from `app` over a stylesheet holding `@stylex;`, the way+ * PostCSS does. Returns the replacement CSS and every file it compiled.+ */+async function compileApp(app) {+ const compiled = [];+ const recordFile = () => ({+ visitor: {+ Program(_, state) {+ compiled.push(path.relative(app, state.filename));+ },+ },+ });+ const [compile] = astryxPostcss({+ cwd: app,+ babelPlugins: [+ [+ require.resolve('@stylexjs/babel-plugin'),+ {+ dev: false,+ runtimeInjection: false,+ unstable_moduleResolution: {type: 'commonJS'},+ },+ ],+ recordFile,+ ],+ }).plugins;+ let css = null;+ const atRule = {+ name: 'stylex',+ params: '',+ replaceWith: replacement => {+ css = replacement;+ },+ };+ await compile(+ {walkAtRules: visit => visit(atRule)},+ {opts: {from: path.join(app, 'src/globals.css')}, messages: []},+ );+ return {css, compiled};+}++describe('the PostCSS plugin and workspace links', () => {+ it('compiles what each linked package publishes, once', async () => {+ const {css, compiled} = await compileApp(path.join(root, 'apps/site'));++ expect([...compiled].sort()).toEqual([+ path.join('node_modules/@astryxdesign/core/src/Box/Box.tsx'),+ path.join('node_modules/@astryxdesign/core/tokens.ts'),+ ]);+ expect(css).toMatch(/@layer astryx-base \{[^]*color:\s*red/);+ });++ it('scans a package without `files` whole, minus its node_modules', async () => {+ const {compiled} = await compileApp(path.join(root, 'apps/themed'));++ expect(compiled).toEqual([+ path.join('node_modules/@astryxdesign/theme/src/theme.ts'),+ ]);+ });++ it.each([+ ['globbed', 'src/*'],+ ['negated', '!src/internal'],+ ])(+ 'refuses a `files` entry it cannot read faithfully: %s',+ async (name, entry) => {+ await expect(compileApp(path.join(root, `apps/${name}`))).rejects.toThrow(+ `@astryxdesign/build reads plain files entries only; @astryxdesign/${name} lists "${entry}"`,+ );+ },+ );+});