Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(packages): richtext, lab, and charts keep their stylesheet in bundles (AST-080 FR4) (#7586)

Cindy Zhang3649b43706e5
diff --git a/.changeset/package-stylesheet-side-effects.md b/.changeset/package-stylesheet-side-effects.md
new file mode 100644
index 0000000000..5a4e1adf24
--- /dev/null
+++ b/.changeset/package-stylesheet-side-effects.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/richtext': patch
+---
+
+[fix] `@astryxdesign/richtext` (and the Lab and Charts packages) now declare their stylesheet as a side effect, so a bundler that removes side-effect-free imports keeps `import '@astryxdesign/richtext/richtext.css'` instead of dropping it and leaving the editor unstyled. JavaScript modules stay tree-shakable.
+
+@cixzhang
diff --git a/packages/charts/package.json b/packages/charts/package.json
index 82354efa52..72c45b19bf 100644
--- a/packages/charts/package.json
+++ b/packages/charts/package.json
@@ -27,7 +27,9 @@
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
- "sideEffects": false,
+ "sideEffects": [
+ "**/*.css"
+ ],
"files": [
"astryx.integration.mjs",
"blocks",
diff --git a/packages/lab/package.json b/packages/lab/package.json
index bdaa6abc08..624768f783 100644
--- a/packages/lab/package.json
+++ b/packages/lab/package.json
@@ -27,7 +27,9 @@
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
- "sideEffects": false,
+ "sideEffects": [
+ "**/*.css"
+ ],
"files": [
"astryx.integration.mjs",
"blocks",
diff --git a/packages/richtext/package.json b/packages/richtext/package.json
index 573d2a0761..008f83bdb6 100644
--- a/packages/richtext/package.json
+++ b/packages/richtext/package.json
@@ -25,7 +25,9 @@
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
- "sideEffects": false,
+ "sideEffects": [
+ "**/*.css"
+ ],
"files": [
"astryx.integration.mjs",
"blocks",
diff --git a/scripts/package-stylesheet-side-effects.test.mjs b/scripts/package-stylesheet-side-effects.test.mjs
new file mode 100644
index 0000000000..4f614ef577
--- /dev/null
+++ b/scripts/package-stylesheet-side-effects.test.mjs
@@ -0,0 +1,110 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file package-stylesheet-side-effects.test.mjs
+ * spec:AST-080 FR4: a product's `import '@astryxdesign/<package>/<name>.css'`
+ * survives bundling. Every package that ships a prebuilt stylesheet exports
+ * its `.css` subpaths, and its `sideEffects` declaration covers each file, so a
+ * bundler that prunes side-effect-free modules (webpack in production) keeps
+ * the import.
+ *
+ * `sideEffects` globs are read the way webpack reads them: matched against the
+ * file's path inside the package, and a pattern with no `/` matches the base
+ * name at any depth.
+ */
+
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {describe, expect, it} from 'vitest';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const PACKAGES = path.join(ROOT, 'packages');
+
+/** Packages whose build writes a prebuilt stylesheet with scripts/build-css.mjs. */
+async function stylesheetPackages() {
+ const found = [];
+ for (const dir of await fs.readdir(PACKAGES)) {
+ const file = path.join(PACKAGES, dir, 'package.json');
+ let manifest;
+ try {
+ manifest = JSON.parse(await fs.readFile(file, 'utf8'));
+ } catch {
+ continue;
+ }
+ if (manifest.scripts?.['build:css']?.includes('build-css.mjs')) {
+ found.push(manifest);
+ }
+ }
+ return found;
+}
+
+/** Every `.css` subpath a manifest exports, with the file it resolves to. */
+function cssExports(manifest) {
+ return Object.entries(manifest.exports ?? {})
+ .filter(([subpath]) => subpath.endsWith('.css'))
+ .map(([subpath, target]) => ({
+ subpath,
+ file: typeof target === 'string' ? target : target.default,
+ }));
+}
+
+/** Translate one `sideEffects` glob to a RegExp over a package-relative path. */
+function globToRegExp(glob) {
+ const pattern = glob.replace(/^\.\//, '');
+ const source = pattern
+ .split(/(\*\*\/|\*\*|\*|\?)/)
+ .map(part => {
+ if (part === '**/') return '(?:.*/)?';
+ if (part === '**') return '.*';
+ if (part === '*') return '[^/]*';
+ if (part === '?') return '[^/]';
+ return part.replace(/[.+^${}()|[\]\\]/g, '\\$&');
+ })
+ .join('');
+ return new RegExp(
+ pattern.includes('/') ? `^${source}$` : `(?:^|/)${source}$`,
+ );
+}
+
+/** Whether a manifest's `sideEffects` keeps the package file `file`. */
+function keepsFile(sideEffects, file) {
+ if (sideEffects === undefined || sideEffects === true) return true;
+ if (!Array.isArray(sideEffects)) return false;
+ const relative = file.replace(/^\.\//, '');
+ return sideEffects.some(glob => globToRegExp(glob).test(relative));
+}
+
+describe('package stylesheets survive bundling (spec:AST-080 FR4)', async () => {
+ const manifests = await stylesheetPackages();
+
+ it('finds every package that ships a prebuilt stylesheet', () => {
+ const names = manifests.map(manifest => manifest.name);
+ expect(names).toEqual(
+ expect.arrayContaining([
+ '@astryxdesign/core',
+ '@astryxdesign/lab',
+ '@astryxdesign/richtext',
+ '@astryxdesign/charts',
+ ]),
+ );
+ });
+
+ for (const manifest of manifests) {
+ for (const {subpath, file} of cssExports(manifest)) {
+ it(`${manifest.name}/${subpath.slice(2)} stays in a production bundle`, async () => {
+ expect(file).toMatch(/\.css$/);
+ expect(keepsFile(manifest.sideEffects, file)).toBe(true);
+ });
+ }
+ }
+
+ it('reads sideEffects the way a bundler does, so the check has teeth', () => {
+ expect(keepsFile(false, './dist/lab.css')).toBe(false);
+ expect(keepsFile(['**/*.stylex.js'], './dist/lab.css')).toBe(false);
+ expect(keepsFile(['*.css'], './dist/lab.css')).toBe(true);
+ expect(keepsFile(['**/*.css'], './src/reset.css')).toBe(true);
+ expect(keepsFile(['./dist/*.css'], './dist/lab.css')).toBe(true);
+ expect(keepsFile(['./src/*.css'], './dist/lab.css')).toBe(false);
+ });
+});