@astryxdesign/build 0.7.5-canary.f2dafed → 0.7.5-canary.f48ad2a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/build",
3
- "version": "0.7.5-canary.f2dafed",
3
+ "version": "0.7.5-canary.f48ad2a",
4
4
  "description": "Build plugins for Astryx source builds — babel, PostCSS, and Vite integrations",
5
5
  "author": "Meta Open Source",
6
6
  "license": "MIT",
@@ -0,0 +1,168 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file postcss.data-defaults.test.mjs
5
+ * @input The documented `postcss(rootDir, overrides)` config, run in
6
+ * development and production modes over an app that has the real Core
7
+ * package installed and never imports `dataVars`
8
+ * @output Proves the PostCSS source build declares Core's canonical 56
9
+ * data-token defaults, names and values from the canonical source, once, on
10
+ * `:root` inside `@layer astryx-base`
11
+ * @position Regression test for spec:AST-066 FR3 in @astryxdesign/build
12
+ *
13
+ * The PostCSS helper compiles every module each installed Astryx package
14
+ * publishes, so Core's `theme/dataTokens.stylex.ts` is always among them and
15
+ * its variable group is always emitted. Raw `var(--color-data-*)` CSS in a
16
+ * PostCSS source build relies on this.
17
+ */
18
+
19
+ import {afterAll, beforeAll, describe, expect, it} from 'vitest';
20
+ import * as fs from 'node:fs';
21
+ import * as os from 'node:os';
22
+ import * as path from 'node:path';
23
+ import {createRequire} from 'node:module';
24
+ import {fileURLToPath} from 'node:url';
25
+ import {parseDataTokenDefaults} from '../../../scripts/generate-data-tokens.mjs';
26
+
27
+ const require = createRequire(import.meta.url);
28
+ const {postcss} = require('./config.js');
29
+
30
+ const REPO_ROOT = path.resolve(
31
+ path.dirname(fileURLToPath(import.meta.url)),
32
+ '../../..',
33
+ );
34
+ const CORE_PACKAGE = path.join(REPO_ROOT, 'packages/core');
35
+
36
+ /** The canonical names and values, read from Core's StyleX source. */
37
+ const CANONICAL = new Map(
38
+ parseDataTokenDefaults(
39
+ fs.readFileSync(
40
+ path.join(CORE_PACKAGE, 'src/theme/dataTokens.stylex.ts'),
41
+ 'utf8',
42
+ ),
43
+ ),
44
+ );
45
+
46
+ const appDirs = [];
47
+ /** @type {Record<'development' | 'production', string>} */
48
+ const output = {development: '', production: ''};
49
+
50
+ /**
51
+ * Run the documented config over a fresh app. Each mode gets its own app
52
+ * folder: the plugin caches compiled files by path, so a shared folder would
53
+ * reuse the first mode's rules.
54
+ * @param {boolean} dev
55
+ */
56
+ async function compile(dev) {
57
+ const appDir = fs.realpathSync(
58
+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-postcss-data-')),
59
+ );
60
+ appDirs.push(appDir);
61
+ fs.mkdirSync(path.join(appDir, 'node_modules/@astryxdesign'), {
62
+ recursive: true,
63
+ });
64
+ fs.symlinkSync(
65
+ CORE_PACKAGE,
66
+ path.join(appDir, 'node_modules/@astryxdesign/core'),
67
+ 'dir',
68
+ );
69
+ fs.mkdirSync(path.join(appDir, 'src'));
70
+ fs.writeFileSync(
71
+ path.join(appDir, 'src/page.tsx'),
72
+ [
73
+ "import * as stylex from '@stylexjs/stylex';",
74
+ "export const styles = stylex.create({page: {color: 'red'}});",
75
+ '',
76
+ ].join('\n'),
77
+ );
78
+
79
+ const config = postcss(appDir, {dev});
80
+ const [pluginPath, options] = Object.entries(config.plugins)[0];
81
+ const plugin = require(pluginPath)(options);
82
+ let css = '';
83
+ const atRule = {
84
+ name: 'stylex',
85
+ params: '',
86
+ replaceWith(next) {
87
+ css = next;
88
+ },
89
+ };
90
+ const root = {walkAtRules: visit => visit(atRule)};
91
+ for (const run of plugin.plugins) {
92
+ await run(root, {
93
+ opts: {from: path.join(appDir, 'src/app.css')},
94
+ messages: [],
95
+ });
96
+ }
97
+ return css;
98
+ }
99
+
100
+ beforeAll(async () => {
101
+ output.development = await compile(true);
102
+ output.production = await compile(false);
103
+ }, 180_000);
104
+
105
+ afterAll(() => {
106
+ for (const dir of appDirs) fs.rmSync(dir, {recursive: true, force: true});
107
+ });
108
+
109
+ /** The slice of a stylesheet inside a given top-level layer block. */
110
+ function layerBlock(css, name) {
111
+ const start = css.indexOf(`@layer ${name} {`);
112
+ expect(start, `@layer ${name} is not in the output`).toBeGreaterThan(-1);
113
+ let depth = 0;
114
+ for (let i = css.indexOf('{', start); i < css.length; i++) {
115
+ if (css[i] === '{') depth++;
116
+ else if (css[i] === '}' && --depth === 0) return css.slice(start, i + 1);
117
+ }
118
+ throw new Error(`@layer ${name} is unterminated`);
119
+ }
120
+
121
+ /** Every rule whose declarations include a data-token default. */
122
+ function dataRules(css) {
123
+ return [...css.matchAll(/([^{}]+)\{([^{}]*--color-data-[^{}]*)\}/g)].map(
124
+ ([, selector, body]) => ({
125
+ selectors: selector
126
+ .trim()
127
+ .split(',')
128
+ .map(part => part.trim()),
129
+ declarations: body
130
+ .split(';')
131
+ .map(part => part.trim())
132
+ .filter(Boolean)
133
+ .map(part => {
134
+ const colon = part.indexOf(':');
135
+ return [part.slice(0, colon).trim(), part.slice(colon + 1).trim()];
136
+ }),
137
+ }),
138
+ );
139
+ }
140
+
141
+ describe('the PostCSS source build, without a dataVars import', () => {
142
+ it('reads 56 canonical tokens from Core source', () => {
143
+ expect(CANONICAL.size).toBe(56);
144
+ });
145
+
146
+ for (const mode of /** @type {const} */ (['development', 'production'])) {
147
+ describe(`in ${mode} mode`, () => {
148
+ it('declares the canonical names and values once, in one group', () => {
149
+ const rules = dataRules(output[mode]);
150
+ expect(rules).toHaveLength(1);
151
+ const [{declarations}] = rules;
152
+ expect(declarations).toHaveLength(56);
153
+ expect(new Map(declarations)).toEqual(CANONICAL);
154
+ });
155
+
156
+ it('declares the group on :root', () => {
157
+ const [{selectors}] = dataRules(output[mode]);
158
+ expect(selectors).toContain(':root');
159
+ });
160
+
161
+ it('places the group in the library layer, not the product layer', () => {
162
+ const css = output[mode];
163
+ expect(dataRules(layerBlock(css, 'astryx-base'))).toHaveLength(1);
164
+ expect(dataRules(layerBlock(css, 'product'))).toHaveLength(0);
165
+ });
166
+ });
167
+ }
168
+ });