@astryxdesign/build 0.6.3 → 0.6.4-canary.10dd683
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/README.md +8 -3
- package/dist/vite.mjs +68 -32
- package/package.json +7 -4
- package/src/babel.js +17 -6
- package/src/babel.test.mjs +48 -10
- package/src/config.js +10 -7
- package/src/index.js +13 -9
- package/src/packed-consumer.test.mjs +186 -0
- package/src/vite.build.test.ts +188 -33
- package/src/vite.test.ts +41 -1
- package/src/vite.ts +99 -52
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @astryxdesign/build
|
|
2
2
|
|
|
3
|
-
Build plugins for
|
|
3
|
+
Build plugins for Astryx source builds. Provides babel, PostCSS, and Vite integrations that compile Astryx library and product code with separate class name prefixes, which enables independent CSS layers:
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
reset < astryx-base (library, astryx prefix) < astryx-theme < product (app, x prefix)
|
|
@@ -12,9 +12,10 @@ StyleX generates atomic CSS: same declaration = same class name. Without separat
|
|
|
12
12
|
|
|
13
13
|
`@astryxdesign/build` solves this by:
|
|
14
14
|
|
|
15
|
-
1. Compiling
|
|
15
|
+
1. Compiling Astryx library code with `astryx` prefix (`.astryx78zum5`)
|
|
16
16
|
2. Compiling product code with default `x` prefix (`.x78zum5`)
|
|
17
17
|
3. Placing each group in its own CSS `@layer`
|
|
18
|
+
4. Rejecting unsupported StyleX declarations instead of silently omitting them
|
|
18
19
|
|
|
19
20
|
## Packages
|
|
20
21
|
|
|
@@ -74,6 +75,7 @@ module.exports = {
|
|
|
74
75
|
{
|
|
75
76
|
dev: process.env.NODE_ENV !== 'production',
|
|
76
77
|
runtimeInjection: false,
|
|
78
|
+
propertyValidationMode: 'throw',
|
|
77
79
|
treeshakeCompensation: true,
|
|
78
80
|
enableInlinedConditionalMerge: true,
|
|
79
81
|
aliases: {
|
|
@@ -186,6 +188,7 @@ export default defineConfig({
|
|
|
186
188
|
stylexOptions: {
|
|
187
189
|
dev: process.env.NODE_ENV === 'development',
|
|
188
190
|
runtimeInjection: false,
|
|
191
|
+
propertyValidationMode: 'throw',
|
|
189
192
|
treeshakeCompensation: true,
|
|
190
193
|
unstable_moduleResolution: {
|
|
191
194
|
type: 'commonJS',
|
|
@@ -232,7 +235,9 @@ Compiles StyleX from both library and product source files in two separate passe
|
|
|
232
235
|
|
|
233
236
|
### Vite plugin (`@astryxdesign/build/vite`)
|
|
234
237
|
|
|
235
|
-
Wraps `@stylexjs/unplugin` and intercepts the dev CSS endpoint (`/virtual:stylex.css`). Partitions the collected rules by file path and serves split-layer CSS.
|
|
238
|
+
Wraps `@stylexjs/unplugin` and intercepts the dev CSS endpoint (`/virtual:stylex.css`). Partitions the collected rules by file path and serves split-layer CSS. Production builds write those rules to one cache-safe shared stylesheet and link it from every emitted HTML entry, without cross-loading entry-owned CSS.
|
|
239
|
+
|
|
240
|
+
Unsupported declarations fail the build by default with StyleX's replacement guidance. Set `propertyValidationMode` explicitly in `stylexOverrides` (modern API) or `stylexOptions` (legacy API) only when a migration needs the upstream `warn` or `silent` behavior.
|
|
236
241
|
|
|
237
242
|
---
|
|
238
243
|
|
package/dist/vite.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import fs from "node:fs";
|
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
import { createRequire } from "node:module";
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
10
11
|
var __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
12
|
var LIBRARY_PATTERN = "node_modules/@astryxdesign/";
|
|
12
13
|
var STYLEX_CSS_PATH = "/virtual:stylex.css";
|
|
@@ -34,6 +35,7 @@ function astryxStylex(options = {}) {
|
|
|
34
35
|
const stylexOptions = {
|
|
35
36
|
dev,
|
|
36
37
|
runtimeInjection: false,
|
|
38
|
+
propertyValidationMode: "throw",
|
|
37
39
|
treeshakeCompensation: true,
|
|
38
40
|
unstable_moduleResolution: {
|
|
39
41
|
type: "commonJS",
|
|
@@ -84,18 +86,34 @@ function astryxStylex(options = {}) {
|
|
|
84
86
|
}
|
|
85
87
|
} catch {
|
|
86
88
|
}
|
|
89
|
+
const corePackageDir = path.resolve(
|
|
90
|
+
rootDir,
|
|
91
|
+
"node_modules/@astryxdesign/core"
|
|
92
|
+
);
|
|
93
|
+
const coreSourceDir = path.join(corePackageDir, "src");
|
|
87
94
|
return {
|
|
88
95
|
resolve: {
|
|
89
|
-
alias:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
alias: [
|
|
97
|
+
{
|
|
98
|
+
find: /^@astryxdesign\/core\/locales\/(.+)\.generated\.js$/,
|
|
99
|
+
replacement: path.join(
|
|
100
|
+
coreSourceDir,
|
|
101
|
+
"i18n/generated-locales/$1.generated.ts"
|
|
102
|
+
)
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
find: /^@astryxdesign\/core\/locales\/(.+)\.json$/,
|
|
106
|
+
replacement: path.join(corePackageDir, "locales/$1.json")
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
find: "@astryxdesign/core/theme/tokens.stylex",
|
|
110
|
+
replacement: path.join(coreSourceDir, "theme/tokens.stylex.ts")
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
find: "@astryxdesign/core",
|
|
114
|
+
replacement: coreSourceDir
|
|
115
|
+
}
|
|
116
|
+
]
|
|
99
117
|
},
|
|
100
118
|
optimizeDeps: {
|
|
101
119
|
exclude: xdsPackages
|
|
@@ -189,24 +207,41 @@ function buildLayerSplitPlugin(basePlugin, options) {
|
|
|
189
207
|
);
|
|
190
208
|
const outDir = outputOptions.dir ? outputOptions.dir : outputOptions.file ? path.dirname(outputOptions.file) : null;
|
|
191
209
|
if (!outDir || !fs.existsSync(outDir)) return;
|
|
192
|
-
const
|
|
210
|
+
const containingFiles = [];
|
|
193
211
|
for (const file of listCssFiles(outDir)) {
|
|
194
212
|
const css = fs.readFileSync(file, "utf-8");
|
|
195
213
|
const at = css.lastIndexOf(merged);
|
|
196
214
|
if (at === -1) continue;
|
|
197
|
-
|
|
198
|
-
file,
|
|
199
|
-
css.slice(0, at) + split + css.slice(at + merged.length)
|
|
200
|
-
);
|
|
201
|
-
patched.push(file);
|
|
215
|
+
containingFiles.push({ file, css, at });
|
|
202
216
|
}
|
|
203
|
-
if (
|
|
217
|
+
if (containingFiles.length === 0) {
|
|
204
218
|
this.error(
|
|
205
219
|
`astryx-build-layer-split: StyleX emitted rules but its CSS block was not found in any stylesheet under ${outDir}, so Astryx and product styles could not be separated into their cascade layers. Leaving the build unsplit would let product styles lose to a theme. This usually means the StyleX plugin version changed how it emits CSS.`
|
|
206
220
|
);
|
|
207
221
|
return;
|
|
208
222
|
}
|
|
209
|
-
|
|
223
|
+
if (listHtmlFiles(outDir).length === 0) {
|
|
224
|
+
for (const { file, css, at } of containingFiles) {
|
|
225
|
+
fs.writeFileSync(
|
|
226
|
+
file,
|
|
227
|
+
css.slice(0, at) + split + css.slice(at + merged.length)
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
for (const { file, css, at } of containingFiles) {
|
|
233
|
+
fs.writeFileSync(
|
|
234
|
+
file,
|
|
235
|
+
css.slice(0, at) + css.slice(at + merged.length)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const hash = createHash("sha256").update(split).digest("hex").slice(0, 8);
|
|
239
|
+
const sharedStylesheet = path.join(
|
|
240
|
+
path.dirname(containingFiles[0].file),
|
|
241
|
+
`astryx-stylex-${hash}.css`
|
|
242
|
+
);
|
|
243
|
+
fs.writeFileSync(sharedStylesheet, split);
|
|
244
|
+
linkStylesheetsForEveryPage(outDir, [sharedStylesheet], base);
|
|
210
245
|
}
|
|
211
246
|
};
|
|
212
247
|
}
|
|
@@ -228,21 +263,18 @@ function listHtmlFiles(dir) {
|
|
|
228
263
|
}
|
|
229
264
|
return out;
|
|
230
265
|
}
|
|
231
|
-
function
|
|
266
|
+
function linkStylesheetsForEveryPage(outDir, cssFiles, base) {
|
|
232
267
|
const pages = listHtmlFiles(outDir);
|
|
233
268
|
if (pages.length === 0) return;
|
|
234
|
-
const orphans = cssFiles.filter((css) => {
|
|
235
|
-
const name = path.basename(css);
|
|
236
|
-
return !pages.some((page) => fs.readFileSync(page, "utf-8").includes(name));
|
|
237
|
-
});
|
|
238
|
-
if (orphans.length === 0) return;
|
|
239
|
-
const links = orphans.map((css) => {
|
|
240
|
-
const href = base.replace(/\/$/, "") + "/" + path.relative(outDir, css).split(path.sep).join("/");
|
|
241
|
-
return `<link rel="stylesheet" crossorigin href="${href}">`;
|
|
242
|
-
}).join("\n ");
|
|
243
269
|
for (const page of pages) {
|
|
244
270
|
const html = fs.readFileSync(page, "utf-8");
|
|
245
271
|
if (!html.includes("</head>")) continue;
|
|
272
|
+
const missing = cssFiles.filter((css) => !html.includes(path.basename(css)));
|
|
273
|
+
if (missing.length === 0) continue;
|
|
274
|
+
const links = missing.map((css) => {
|
|
275
|
+
const href = base.replace(/\/$/, "") + "/" + path.relative(outDir, css).split(path.sep).join("/");
|
|
276
|
+
return `<link rel="stylesheet" crossorigin href="${href}">`;
|
|
277
|
+
}).join("\n ");
|
|
246
278
|
fs.writeFileSync(page, html.replace("</head>", ` ${links}
|
|
247
279
|
</head>`));
|
|
248
280
|
}
|
|
@@ -273,18 +305,22 @@ function astryxStylexLegacy(options) {
|
|
|
273
305
|
} = options;
|
|
274
306
|
const libraryLayer = layers.library ?? "astryx-base";
|
|
275
307
|
const productLayer = layers.product ?? "product";
|
|
308
|
+
const validatedStylexOptions = {
|
|
309
|
+
propertyValidationMode: "throw",
|
|
310
|
+
...stylexOptions
|
|
311
|
+
};
|
|
276
312
|
const astryxBabelPlugin = path.resolve(__dirname, "../src/babel.js");
|
|
277
|
-
const existingPlugins =
|
|
313
|
+
const existingPlugins = validatedStylexOptions.babelConfig?.plugins ?? [];
|
|
278
314
|
const basePlugin = stylex.vite({
|
|
279
|
-
...
|
|
315
|
+
...validatedStylexOptions,
|
|
280
316
|
useCSSLayers: true,
|
|
281
317
|
babelConfig: {
|
|
282
|
-
...
|
|
318
|
+
...validatedStylexOptions.babelConfig,
|
|
283
319
|
plugins: [
|
|
284
320
|
[
|
|
285
321
|
astryxBabelPlugin,
|
|
286
322
|
{
|
|
287
|
-
...
|
|
323
|
+
...validatedStylexOptions,
|
|
288
324
|
libraryPrefix: stylexPrefix,
|
|
289
325
|
babelConfig: void 0
|
|
290
326
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/build",
|
|
3
|
-
"version": "0.6.
|
|
4
|
-
"description": "Build plugins for
|
|
3
|
+
"version": "0.6.4-canary.10dd683",
|
|
4
|
+
"description": "Build plugins for Astryx source builds — babel, PostCSS, and Vite integrations",
|
|
5
5
|
"author": "Meta Open Source",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/facebook/astryx#readme",
|
|
@@ -36,15 +36,18 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@stylexjs/babel-plugin": ">=0.18.0",
|
|
40
39
|
"@babel/core": ">=7.0.0",
|
|
40
|
+
"@stylexjs/babel-plugin": ">=0.18.0",
|
|
41
41
|
"@stylexjs/unplugin": ">=0.18.0",
|
|
42
42
|
"vite": "^8.1.3"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
+
"autoprefixer": "^10.5.2",
|
|
46
|
+
"browserslist": "^4.28.4",
|
|
45
47
|
"fast-glob": "^3.3.0",
|
|
46
48
|
"glob-parent": "^6.0.0",
|
|
47
|
-
"is-glob": "^4.0.0"
|
|
49
|
+
"is-glob": "^4.0.0",
|
|
50
|
+
"lightningcss": "^1.32.0"
|
|
48
51
|
},
|
|
49
52
|
"devDependencies": {
|
|
50
53
|
"esbuild": "^0.28.1"
|
package/src/babel.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
'use strict';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @astryxdesign/build/babel
|
|
@@ -28,9 +28,20 @@ module.exports = function astryxBabelPlugin(api, options) {
|
|
|
28
28
|
...stylexOptions
|
|
29
29
|
} = options;
|
|
30
30
|
|
|
31
|
+
// StyleX's upstream default is `silent`, which turns unsupported declarations
|
|
32
|
+
// into missing CSS. Astryx fails closed by default so consumer builds point at
|
|
33
|
+
// the declaration and the supported replacement instead.
|
|
34
|
+
const validatedStylexOptions = {
|
|
35
|
+
propertyValidationMode: 'throw',
|
|
36
|
+
...stylexOptions,
|
|
37
|
+
};
|
|
38
|
+
|
|
31
39
|
// Build the two sets of options — only classNamePrefix differs
|
|
32
|
-
const libraryOpts = {
|
|
33
|
-
|
|
40
|
+
const libraryOpts = {
|
|
41
|
+
...validatedStylexOptions,
|
|
42
|
+
classNamePrefix: libraryPrefix,
|
|
43
|
+
};
|
|
44
|
+
const productOpts = {...validatedStylexOptions, classNamePrefix};
|
|
34
45
|
|
|
35
46
|
// Create two plugin instances
|
|
36
47
|
const libraryPlugin = stylexPlugin(api, libraryOpts);
|
|
@@ -82,18 +93,18 @@ module.exports = function astryxBabelPlugin(api, options) {
|
|
|
82
93
|
// enter/exit form (Program uses this)
|
|
83
94
|
visitor[key] = {};
|
|
84
95
|
if (sample.enter) {
|
|
85
|
-
visitor[key].enter = function(path, state) {
|
|
96
|
+
visitor[key].enter = function (path, state) {
|
|
86
97
|
callVisitor.call(this, 'enter', key, path, state);
|
|
87
98
|
};
|
|
88
99
|
}
|
|
89
100
|
if (sample.exit) {
|
|
90
|
-
visitor[key].exit = function(path, state) {
|
|
101
|
+
visitor[key].exit = function (path, state) {
|
|
91
102
|
callVisitor.call(this, 'exit', key, path, state);
|
|
92
103
|
};
|
|
93
104
|
}
|
|
94
105
|
} else {
|
|
95
106
|
// Simple function visitor
|
|
96
|
-
visitor[key] = function(path, state) {
|
|
107
|
+
visitor[key] = function (path, state) {
|
|
97
108
|
callVisitor.call(this, 'enter', key, path, state);
|
|
98
109
|
};
|
|
99
110
|
}
|
package/src/babel.test.mjs
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file babel.test.mjs
|
|
5
|
-
* @description Verifies that the
|
|
6
|
-
* library StyleX class-name prefix to
|
|
7
|
-
*
|
|
8
|
-
* before the final cutover.
|
|
5
|
+
* @description Verifies that the Astryx babel wrapper applies the configured
|
|
6
|
+
* library StyleX class-name prefix to Astryx library files. The prefix
|
|
7
|
+
* defaults to `astryx` and remains configurable.
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
10
|
import {describe, it, expect} from 'vitest';
|
|
@@ -14,6 +13,7 @@ import {createRequire} from 'node:module';
|
|
|
14
13
|
const require = createRequire(import.meta.url);
|
|
15
14
|
const babel = require('@babel/core');
|
|
16
15
|
const astryxBabelPlugin = require('./babel.js');
|
|
16
|
+
const {stylexOptions: buildStylexOptions} = require('./config.js');
|
|
17
17
|
|
|
18
18
|
const SOURCE = `
|
|
19
19
|
import * as stylex from '@stylexjs/stylex';
|
|
@@ -22,16 +22,27 @@ export const styles = stylex.create({
|
|
|
22
22
|
});
|
|
23
23
|
`;
|
|
24
24
|
|
|
25
|
+
const INVALID_BORDER_SOURCE = `
|
|
26
|
+
import * as stylex from '@stylexjs/stylex';
|
|
27
|
+
export const styles = stylex.create({
|
|
28
|
+
box: {borderTop: '1px solid var(--color-border)'},
|
|
29
|
+
});
|
|
30
|
+
`;
|
|
31
|
+
|
|
25
32
|
/**
|
|
26
|
-
* Transform a StyleX source through the
|
|
27
|
-
* library file, returning the emitted code. `libraryPrefix` controls the
|
|
33
|
+
* Transform a StyleX source through the Astryx babel wrapper as if it were an
|
|
34
|
+
* Astryx library file, returning the emitted code. `libraryPrefix` controls the
|
|
28
35
|
* atomic class-name prefix for library files.
|
|
29
36
|
*/
|
|
30
|
-
function transformLibraryFile(
|
|
31
|
-
|
|
37
|
+
function transformLibraryFile(
|
|
38
|
+
libraryPrefix,
|
|
39
|
+
source = SOURCE,
|
|
40
|
+
stylexOptions = {},
|
|
41
|
+
) {
|
|
42
|
+
const result = babel.transformSync(source, {
|
|
32
43
|
// A path matching one of the library patterns so the wrapper routes it
|
|
33
44
|
// through the library plugin instance.
|
|
34
|
-
filename: 'node_modules/@astryxdesign/core/src/Box/
|
|
45
|
+
filename: 'node_modules/@astryxdesign/core/src/Box/AstryxBox.tsx',
|
|
35
46
|
babelrc: false,
|
|
36
47
|
configFile: false,
|
|
37
48
|
plugins: [
|
|
@@ -40,6 +51,7 @@ function transformLibraryFile(libraryPrefix) {
|
|
|
40
51
|
{
|
|
41
52
|
...(libraryPrefix ? {libraryPrefix} : {}),
|
|
42
53
|
unstable_moduleResolution: {type: 'commonJS', rootDir: process.cwd()},
|
|
54
|
+
...stylexOptions,
|
|
43
55
|
},
|
|
44
56
|
],
|
|
45
57
|
],
|
|
@@ -53,7 +65,7 @@ function atomicClasses(code) {
|
|
|
53
65
|
return code.match(/\b(?:xds|astryx|lib)[a-z0-9]{4,}\b/g) ?? [];
|
|
54
66
|
}
|
|
55
67
|
|
|
56
|
-
describe('
|
|
68
|
+
describe('Astryx babel wrapper -- library StyleX prefix', () => {
|
|
57
69
|
it('defaults library atoms to the `astryx` prefix', () => {
|
|
58
70
|
const code = transformLibraryFile(undefined);
|
|
59
71
|
const atoms = atomicClasses(code);
|
|
@@ -70,4 +82,30 @@ describe('xds babel wrapper -- library StyleX prefix', () => {
|
|
|
70
82
|
expect(atoms.length).toBeGreaterThan(0);
|
|
71
83
|
expect(atoms.every(c => c.startsWith('lib'))).toBe(true);
|
|
72
84
|
});
|
|
85
|
+
|
|
86
|
+
it('rejects declarations that StyleX would otherwise drop silently', () => {
|
|
87
|
+
expect(() =>
|
|
88
|
+
transformLibraryFile(undefined, INVALID_BORDER_SOURCE),
|
|
89
|
+
).toThrow(
|
|
90
|
+
/borderTop is not supported.*borderTopWidth.*borderTopStyle.*borderTopColor/s,
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('keeps the upstream validation mode override available', () => {
|
|
95
|
+
expect(() =>
|
|
96
|
+
transformLibraryFile(undefined, INVALID_BORDER_SOURCE, {
|
|
97
|
+
propertyValidationMode: 'silent',
|
|
98
|
+
}),
|
|
99
|
+
).not.toThrow();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('uses the same strict default in generated Babel and PostCSS configs', () => {
|
|
103
|
+
expect(buildStylexOptions(process.cwd()).propertyValidationMode).toBe(
|
|
104
|
+
'throw',
|
|
105
|
+
);
|
|
106
|
+
expect(
|
|
107
|
+
buildStylexOptions(process.cwd(), {propertyValidationMode: 'warn'})
|
|
108
|
+
.propertyValidationMode,
|
|
109
|
+
).toBe('warn');
|
|
110
|
+
});
|
|
73
111
|
});
|
package/src/config.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
'use strict';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @astryxdesign/build
|
|
7
7
|
*
|
|
8
|
-
* Unified build configuration for Astryx source builds.
|
|
9
|
-
*
|
|
8
|
+
* Unified build configuration for Astryx source builds. The complete PostCSS
|
|
9
|
+
* helper resolves its bundled Autoprefixer from this package rather than from
|
|
10
|
+
* the consuming project's dependency graph.
|
|
11
|
+
*
|
|
10
12
|
* Usage:
|
|
11
13
|
* // babel.config.js
|
|
12
14
|
* const {babel} = require('@astryxdesign/build');
|
|
13
15
|
* module.exports = babel(__dirname);
|
|
14
|
-
*
|
|
16
|
+
*
|
|
15
17
|
* // postcss.config.js
|
|
16
18
|
* const {postcss} = require('@astryxdesign/build');
|
|
17
19
|
* module.exports = postcss(__dirname);
|
|
@@ -39,6 +41,7 @@ function stylexOptions(rootDir, overrides = {}) {
|
|
|
39
41
|
return {
|
|
40
42
|
dev: process.env.NODE_ENV !== 'production',
|
|
41
43
|
runtimeInjection: false,
|
|
44
|
+
propertyValidationMode: 'throw',
|
|
42
45
|
enableInlinedConditionalMerge: true,
|
|
43
46
|
treeshakeCompensation: true,
|
|
44
47
|
aliases: resolveAliases(rootDir),
|
|
@@ -51,7 +54,7 @@ function stylexOptions(rootDir, overrides = {}) {
|
|
|
51
54
|
|
|
52
55
|
/**
|
|
53
56
|
* Generate a complete babel.config.js for Astryx source builds.
|
|
54
|
-
*
|
|
57
|
+
*
|
|
55
58
|
* @param {string} rootDir — __dirname of the project root
|
|
56
59
|
* @param {object} [overrides] — extra StyleX options to merge
|
|
57
60
|
* @returns {object} babel config object
|
|
@@ -79,7 +82,7 @@ function babel(rootDir, overrides = {}) {
|
|
|
79
82
|
|
|
80
83
|
/**
|
|
81
84
|
* Generate a complete postcss.config.js for Astryx source builds.
|
|
82
|
-
*
|
|
85
|
+
*
|
|
83
86
|
* @param {string} rootDir — __dirname of the project root
|
|
84
87
|
* @param {object} [overrides] — extra options (appDir, extraInclude, etc.)
|
|
85
88
|
* @returns {object} postcss config object
|
|
@@ -96,7 +99,7 @@ function postcss(rootDir, overrides = {}) {
|
|
|
96
99
|
],
|
|
97
100
|
extraInclude,
|
|
98
101
|
},
|
|
99
|
-
autoprefixer: {},
|
|
102
|
+
[require.resolve('autoprefixer')]: {},
|
|
100
103
|
},
|
|
101
104
|
};
|
|
102
105
|
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
'use strict';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @astryxdesign/postcss-plugin
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
* reset < astryx-base (library, prefix: 'astryx') < astryx-theme < product (prefix: 'x')
|
|
13
13
|
*
|
|
14
14
|
* The separate prefixes ensure atomic classes don't collide between
|
|
15
|
-
* layers, which would break theme overrides.
|
|
15
|
+
* layers, which would break theme overrides. Generated replacement CSS is
|
|
16
|
+
* parsed by the host PostCSS instance so consumers do not load a second copy.
|
|
16
17
|
*/
|
|
17
18
|
|
|
18
19
|
const path = require('node:path');
|
|
19
20
|
const fs = require('node:fs');
|
|
20
|
-
const postcss = require('postcss');
|
|
21
21
|
const babel = require('@babel/core');
|
|
22
22
|
const stylexBabelPlugin = require('@stylexjs/babel-plugin');
|
|
23
23
|
const {globSync} = require('fast-glob');
|
|
@@ -27,7 +27,11 @@ const globParent = require('glob-parent');
|
|
|
27
27
|
const PLUGIN_NAME = '@astryxdesign/postcss-plugin';
|
|
28
28
|
|
|
29
29
|
const LIBRARY_GLOB = 'node_modules/@astryxdesign/**/*.{ts,tsx}';
|
|
30
|
-
const LIBRARY_PATTERNS = [
|
|
30
|
+
const LIBRARY_PATTERNS = [
|
|
31
|
+
'node_modules/@astryxdesign/',
|
|
32
|
+
'packages/core/',
|
|
33
|
+
'packages/themes/',
|
|
34
|
+
];
|
|
31
35
|
const STYLEX_IMPORT_SOURCE = '@stylexjs/stylex';
|
|
32
36
|
|
|
33
37
|
function parseDependency(fileOrGlob, cwd) {
|
|
@@ -117,7 +121,7 @@ function createPlugin() {
|
|
|
117
121
|
const fileName = result.opts.from;
|
|
118
122
|
|
|
119
123
|
let styleXAtRule = null;
|
|
120
|
-
root.walkAtRules(
|
|
124
|
+
root.walkAtRules(atRule => {
|
|
121
125
|
if (atRule.name === 'stylex' && !atRule.params) {
|
|
122
126
|
styleXAtRule = atRule;
|
|
123
127
|
}
|
|
@@ -193,7 +197,7 @@ function createPlugin() {
|
|
|
193
197
|
rulesMap.set(filePath, stylex);
|
|
194
198
|
}
|
|
195
199
|
})
|
|
196
|
-
.catch(
|
|
200
|
+
.catch(error => {
|
|
197
201
|
if (shouldSkipTransformError) {
|
|
198
202
|
console.warn(
|
|
199
203
|
`[${PLUGIN_NAME}] Failed to transform "${filePath}": ${error.message}`,
|
|
@@ -231,9 +235,9 @@ function createPlugin() {
|
|
|
231
235
|
parts.push(`@layer ${layers.product} {\n${productCss}\n}`);
|
|
232
236
|
}
|
|
233
237
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
styleXAtRule.replaceWith(
|
|
238
|
+
// Let the host PostCSS instance parse the replacement so this plugin
|
|
239
|
+
// does not need or load a second copy of PostCSS.
|
|
240
|
+
styleXAtRule.replaceWith(parts.join('\n\n'));
|
|
237
241
|
result.root = root;
|
|
238
242
|
|
|
239
243
|
if (!shouldSkipTransformError) {
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file packed-consumer.test.mjs
|
|
5
|
+
* @input Packs @astryxdesign/build, reconstructs only the dependencies declared
|
|
6
|
+
* by its tarball, and exercises the public PostCSS helper and Vite plugin
|
|
7
|
+
* @output Proves shipped build integrations need no ambient workspace packages
|
|
8
|
+
* and preserve generated browser compatibility CSS
|
|
9
|
+
* @position Consumer-package regression for @astryxdesign/build
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {execFileSync} from 'node:child_process';
|
|
13
|
+
import {
|
|
14
|
+
mkdtempSync,
|
|
15
|
+
mkdirSync,
|
|
16
|
+
readFileSync,
|
|
17
|
+
readdirSync,
|
|
18
|
+
realpathSync,
|
|
19
|
+
rmSync,
|
|
20
|
+
symlinkSync,
|
|
21
|
+
writeFileSync,
|
|
22
|
+
} from 'node:fs';
|
|
23
|
+
import {tmpdir} from 'node:os';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
import {fileURLToPath} from 'node:url';
|
|
26
|
+
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
|
|
27
|
+
|
|
28
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
const packageDir = path.resolve(__dirname, '..');
|
|
30
|
+
const repoRoot = path.resolve(packageDir, '../..');
|
|
31
|
+
|
|
32
|
+
function run(command, args, cwd) {
|
|
33
|
+
try {
|
|
34
|
+
return execFileSync(command, args, {
|
|
35
|
+
cwd,
|
|
36
|
+
encoding: 'utf8',
|
|
37
|
+
env: {...process.env, NODE_PATH: undefined},
|
|
38
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
const stdout = error.stdout?.toString() || '';
|
|
42
|
+
const stderr = error.stderr?.toString() || '';
|
|
43
|
+
throw new Error(
|
|
44
|
+
`${command} ${args.join(' ')} failed in ${cwd}\n${stdout}${stderr}`,
|
|
45
|
+
{cause: error},
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function linkPackage(sourceRoot, name, targetRoot) {
|
|
51
|
+
const source = realpathSync(path.join(sourceRoot, 'node_modules', name));
|
|
52
|
+
const target = path.join(targetRoot, 'node_modules', name);
|
|
53
|
+
mkdirSync(path.dirname(target), {recursive: true});
|
|
54
|
+
symlinkSync(source, target, 'junction');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('the packed @astryxdesign/build consumer contract', () => {
|
|
58
|
+
let root;
|
|
59
|
+
let appDir;
|
|
60
|
+
let packedPackageDir;
|
|
61
|
+
|
|
62
|
+
beforeAll(() => {
|
|
63
|
+
root = mkdtempSync(path.join(tmpdir(), 'astryx-build-consumer-'));
|
|
64
|
+
const packDir = path.join(root, 'pack');
|
|
65
|
+
appDir = path.join(root, 'app');
|
|
66
|
+
packedPackageDir = path.join(root, 'packed-build');
|
|
67
|
+
mkdirSync(packDir);
|
|
68
|
+
mkdirSync(path.join(appDir, 'node_modules/@astryxdesign'), {
|
|
69
|
+
recursive: true,
|
|
70
|
+
});
|
|
71
|
+
mkdirSync(path.join(appDir, 'src'), {recursive: true});
|
|
72
|
+
mkdirSync(packedPackageDir);
|
|
73
|
+
|
|
74
|
+
run('pnpm', ['pack', '--pack-destination', packDir], packageDir);
|
|
75
|
+
const tarball = path.join(
|
|
76
|
+
packDir,
|
|
77
|
+
readdirSync(packDir).find(name => name.endsWith('.tgz')),
|
|
78
|
+
);
|
|
79
|
+
run(
|
|
80
|
+
'tar',
|
|
81
|
+
['-xzf', tarball, '-C', packedPackageDir, '--strip-components=1'],
|
|
82
|
+
root,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const manifest = JSON.parse(
|
|
86
|
+
readFileSync(path.join(packedPackageDir, 'package.json'), 'utf8'),
|
|
87
|
+
);
|
|
88
|
+
// Recreate strict dependency visibility from the packed manifest rather
|
|
89
|
+
// than letting the monorepo's hoisted packages satisfy missing entries.
|
|
90
|
+
for (const dependency of Object.keys(manifest.dependencies || {})) {
|
|
91
|
+
linkPackage(packageDir, dependency, packedPackageDir);
|
|
92
|
+
}
|
|
93
|
+
for (const peer of Object.keys(manifest.peerDependencies || {})) {
|
|
94
|
+
linkPackage(packageDir, peer, packedPackageDir);
|
|
95
|
+
linkPackage(packageDir, peer, appDir);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
symlinkSync(
|
|
99
|
+
packedPackageDir,
|
|
100
|
+
path.join(appDir, 'node_modules/@astryxdesign/build'),
|
|
101
|
+
'junction',
|
|
102
|
+
);
|
|
103
|
+
const virtualStoreRoot = path.join(repoRoot, 'node_modules/.pnpm');
|
|
104
|
+
linkPackage(virtualStoreRoot, 'postcss', appDir);
|
|
105
|
+
linkPackage(virtualStoreRoot, '@stylexjs/stylex', appDir);
|
|
106
|
+
|
|
107
|
+
writeFileSync(
|
|
108
|
+
path.join(appDir, 'package.json'),
|
|
109
|
+
JSON.stringify({
|
|
110
|
+
name: 'astryx-build-consumer-contract',
|
|
111
|
+
private: true,
|
|
112
|
+
type: 'module',
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
writeFileSync(
|
|
116
|
+
path.join(appDir, 'index.html'),
|
|
117
|
+
'<!doctype html><html><body><script type="module" src="/src/main.js"></script></body></html>\n',
|
|
118
|
+
);
|
|
119
|
+
writeFileSync(
|
|
120
|
+
path.join(appDir, 'src/main.js'),
|
|
121
|
+
[
|
|
122
|
+
"import * as stylex from '@stylexjs/stylex';",
|
|
123
|
+
"const styles = stylex.create({item: {userSelect: 'none'}});",
|
|
124
|
+
'document.body.className = stylex.props(styles.item).className;',
|
|
125
|
+
'',
|
|
126
|
+
].join('\n'),
|
|
127
|
+
);
|
|
128
|
+
writeFileSync(
|
|
129
|
+
path.join(appDir, 'vite.config.mjs'),
|
|
130
|
+
[
|
|
131
|
+
"import {astryxStylex} from '@astryxdesign/build/vite';",
|
|
132
|
+
'export default {plugins: [...astryxStylex({rootDir: process.cwd()})]};',
|
|
133
|
+
'',
|
|
134
|
+
].join('\n'),
|
|
135
|
+
);
|
|
136
|
+
writeFileSync(
|
|
137
|
+
path.join(appDir, 'check-postcss.cjs'),
|
|
138
|
+
[
|
|
139
|
+
"const path = require('node:path');",
|
|
140
|
+
"const postcss = require('postcss');",
|
|
141
|
+
"const {postcss: buildPostcss} = require('@astryxdesign/build');",
|
|
142
|
+
"for (const dependency of ['autoprefixer', 'browserslist', 'lightningcss']) {",
|
|
143
|
+
' try {',
|
|
144
|
+
' require.resolve(dependency);',
|
|
145
|
+
' throw new Error(`${dependency} leaked into the consumer root`);',
|
|
146
|
+
' } catch (error) {',
|
|
147
|
+
" if (error.code !== 'MODULE_NOT_FOUND') throw error;",
|
|
148
|
+
' }',
|
|
149
|
+
'}',
|
|
150
|
+
'const config = buildPostcss(__dirname);',
|
|
151
|
+
'const plugins = Object.entries(config.plugins).map(([id, options]) => {',
|
|
152
|
+
' if (!path.isAbsolute(id)) throw new Error(`unowned PostCSS plugin: ${id}`);',
|
|
153
|
+
' return require(id)(options);',
|
|
154
|
+
'});',
|
|
155
|
+
"postcss(plugins).process('@stylex;', {from: 'input.css'}).then(() => {",
|
|
156
|
+
" process.stdout.write('postcss-ok\\n');",
|
|
157
|
+
'});',
|
|
158
|
+
'',
|
|
159
|
+
].join('\n'),
|
|
160
|
+
);
|
|
161
|
+
}, 120_000);
|
|
162
|
+
|
|
163
|
+
afterAll(() => {
|
|
164
|
+
if (root) rmSync(root, {recursive: true, force: true});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('loads the public PostCSS helper without undeclared consumer packages', () => {
|
|
168
|
+
expect(run(process.execPath, ['check-postcss.cjs'], appDir)).toBe(
|
|
169
|
+
'postcss-ok\n',
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('keeps the Vite compatibility pass inside the package boundary', () => {
|
|
174
|
+
const viteCli = path.join(
|
|
175
|
+
packedPackageDir,
|
|
176
|
+
'node_modules/vite/bin/vite.js',
|
|
177
|
+
);
|
|
178
|
+
run(process.execPath, [viteCli, 'build'], appDir);
|
|
179
|
+
const assetsDir = path.join(appDir, 'dist/assets');
|
|
180
|
+
const cssFile = readdirSync(assetsDir).find(name => name.endsWith('.css'));
|
|
181
|
+
expect(cssFile).toBeDefined();
|
|
182
|
+
expect(readFileSync(path.join(assetsDir, cssFile), 'utf8')).toContain(
|
|
183
|
+
'-webkit-user-select',
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
});
|
package/src/vite.build.test.ts
CHANGED
|
@@ -11,15 +11,20 @@
|
|
|
11
11
|
* `astryx-base` they lose to one. Only the built artifact shows it, so this
|
|
12
12
|
* builds one.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
14
|
+
* Three shapes, because StyleX emits through two different paths and merges
|
|
15
|
+
* all entries into one stylesheet:
|
|
15
16
|
*
|
|
16
|
-
* `layer-split`
|
|
17
|
-
*
|
|
18
|
-
* `layer-split-nocss`
|
|
19
|
-
*
|
|
17
|
+
* `layer-split` the app imports a stylesheet, so the bundle has a
|
|
18
|
+
* CSS asset and StyleX appends in `generateBundle`
|
|
19
|
+
* `layer-split-nocss` the app imports none, so StyleX writes its own file
|
|
20
|
+
* in `writeBundle`, outside Rollup's graph
|
|
21
|
+
* `layer-split-multipage` two entries own distinct authored CSS, while both
|
|
22
|
+
* must load the one shared StyleX result
|
|
20
23
|
*
|
|
21
|
-
* The
|
|
22
|
-
* not run there at all, and the file StyleX writes
|
|
24
|
+
* The no-CSS shape was originally missing, which hid two defects: the split did
|
|
25
|
+
* not run there at all, and the file StyleX writes was not linked by any page.
|
|
26
|
+
* The multi-page shape protects against linking StyleX from only one entry or
|
|
27
|
+
* cross-loading that entry's authored CSS into every page.
|
|
23
28
|
*
|
|
24
29
|
* What none of this can prove is which declaration actually paints — that is a
|
|
25
30
|
* cascade fact, and it lives in .github/scripts/theme-layer-cascade.js.
|
|
@@ -28,7 +33,16 @@
|
|
|
28
33
|
import {describe, it, expect, beforeAll, afterAll} from 'vitest';
|
|
29
34
|
import {build} from 'vite';
|
|
30
35
|
import react from '@vitejs/plugin-react';
|
|
31
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
mkdirSync,
|
|
38
|
+
mkdtempSync,
|
|
39
|
+
readdirSync,
|
|
40
|
+
readFileSync,
|
|
41
|
+
realpathSync,
|
|
42
|
+
rmSync,
|
|
43
|
+
symlinkSync,
|
|
44
|
+
writeFileSync,
|
|
45
|
+
} from 'node:fs';
|
|
32
46
|
import {tmpdir} from 'node:os';
|
|
33
47
|
import * as path from 'node:path';
|
|
34
48
|
import {fileURLToPath} from 'node:url';
|
|
@@ -38,44 +52,89 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
38
52
|
const REPO_ROOT = path.resolve(__dirname, '../../..');
|
|
39
53
|
const FIXTURES = path.resolve(__dirname, '../__fixtures__');
|
|
40
54
|
const CORE_SRC = path.join(REPO_ROOT, 'packages/core/src');
|
|
55
|
+
const CORE_PACKAGE = path.join(REPO_ROOT, 'packages/core');
|
|
41
56
|
|
|
42
57
|
/** The class prefixes @astryxdesign/build/babel assigns per origin. */
|
|
43
58
|
const LIBRARY_CLASS = /\.astryx[a-z0-9]{5,}/g;
|
|
44
59
|
const PRODUCT_CLASS = /\.x[a-z0-9]{5,}/g;
|
|
45
60
|
|
|
46
|
-
type Built = {
|
|
61
|
+
type Built = {
|
|
62
|
+
css: string;
|
|
63
|
+
cssName: string;
|
|
64
|
+
cssByName: Record<string, string>;
|
|
65
|
+
html: string;
|
|
66
|
+
htmlByName: Record<string, string>;
|
|
67
|
+
outDir: string;
|
|
68
|
+
};
|
|
47
69
|
|
|
48
|
-
async function buildFixture(
|
|
70
|
+
async function buildFixture(
|
|
71
|
+
name: string,
|
|
72
|
+
htmlNames: string[] = ['index.html'],
|
|
73
|
+
): Promise<Built> {
|
|
49
74
|
const root = path.join(FIXTURES, name);
|
|
50
75
|
const outDir = mkdtempSync(path.join(tmpdir(), `astryx-${name}-`));
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
76
|
+
try {
|
|
77
|
+
await build({
|
|
78
|
+
root,
|
|
79
|
+
logLevel: 'error',
|
|
80
|
+
build: {
|
|
81
|
+
outDir,
|
|
82
|
+
emptyOutDir: true,
|
|
83
|
+
...(htmlNames.length > 1 && {
|
|
84
|
+
rollupOptions: {
|
|
85
|
+
input: Object.fromEntries(
|
|
86
|
+
htmlNames.map(file => [
|
|
87
|
+
path.basename(file, '.html'),
|
|
88
|
+
path.join(root, file),
|
|
89
|
+
]),
|
|
90
|
+
),
|
|
65
91
|
},
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
94
|
+
resolve: {alias: {'@astryxdesign/core': CORE_SRC}},
|
|
95
|
+
plugins: [
|
|
96
|
+
react(),
|
|
97
|
+
...astryxStylex({
|
|
98
|
+
stylexOptions: {
|
|
99
|
+
dev: false,
|
|
100
|
+
unstable_moduleResolution: {type: 'commonJS', rootDir: REPO_ROOT},
|
|
101
|
+
aliases: {
|
|
102
|
+
'@astryxdesign/core/*': [path.join(CORE_SRC, '*')],
|
|
103
|
+
'@astryxdesign/core': [CORE_SRC],
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
libraryPattern: 'packages/core/',
|
|
107
|
+
}),
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
rmSync(outDir, {recursive: true, force: true});
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
71
114
|
|
|
72
115
|
const assets = path.join(outDir, 'assets');
|
|
73
|
-
const
|
|
74
|
-
|
|
116
|
+
const cssNames = readdirSync(assets).filter(f => f.endsWith('.css'));
|
|
117
|
+
const cssByName = Object.fromEntries(
|
|
118
|
+
cssNames.map(file => [
|
|
119
|
+
file,
|
|
120
|
+
readFileSync(path.join(assets, file), 'utf-8'),
|
|
121
|
+
]),
|
|
122
|
+
);
|
|
123
|
+
const cssName = cssNames.find(f => f.startsWith('astryx-stylex-'));
|
|
124
|
+
if (!cssName)
|
|
125
|
+
throw new Error(`no shared StyleX stylesheet emitted into ${assets}`);
|
|
126
|
+
const htmlByName = Object.fromEntries(
|
|
127
|
+
htmlNames.map(file => [
|
|
128
|
+
file,
|
|
129
|
+
readFileSync(path.join(outDir, file), 'utf-8'),
|
|
130
|
+
]),
|
|
131
|
+
);
|
|
75
132
|
return {
|
|
76
|
-
css:
|
|
133
|
+
css: cssByName[cssName],
|
|
77
134
|
cssName,
|
|
78
|
-
|
|
135
|
+
cssByName,
|
|
136
|
+
html: htmlByName['index.html'],
|
|
137
|
+
htmlByName,
|
|
79
138
|
outDir,
|
|
80
139
|
};
|
|
81
140
|
}
|
|
@@ -126,6 +185,14 @@ function itSplitsCorrectly(get: () => Built) {
|
|
|
126
185
|
expect(library.match(PRODUCT_CLASS)).toBeNull();
|
|
127
186
|
});
|
|
128
187
|
|
|
188
|
+
it('keeps nested pseudo-elements and keyframes in the product layer', () => {
|
|
189
|
+
const product = layerBlock(get().css, 'product');
|
|
190
|
+
expect(product).toContain(':after');
|
|
191
|
+
expect(product).toContain('#010203');
|
|
192
|
+
expect(product).toContain('@keyframes');
|
|
193
|
+
expect(product).toContain('.717s');
|
|
194
|
+
});
|
|
195
|
+
|
|
129
196
|
it('leaves no StyleX rule outside the two layers', () => {
|
|
130
197
|
const {css} = get();
|
|
131
198
|
const outside = css
|
|
@@ -148,6 +215,43 @@ function itSplitsCorrectly(get: () => Built) {
|
|
|
148
215
|
});
|
|
149
216
|
}
|
|
150
217
|
|
|
218
|
+
describe('a production build resolves generated locale modules', () => {
|
|
219
|
+
it('loads a generated string map through the standard Astryx Vite aliases', async () => {
|
|
220
|
+
const root = realpathSync(
|
|
221
|
+
mkdtempSync(path.join(tmpdir(), 'astryx-locale-import-')),
|
|
222
|
+
);
|
|
223
|
+
const packageDir = path.join(root, 'node_modules/@astryxdesign');
|
|
224
|
+
const outDir = path.join(root, 'dist');
|
|
225
|
+
mkdirSync(packageDir, {recursive: true});
|
|
226
|
+
symlinkSync(CORE_PACKAGE, path.join(packageDir, 'core'), 'dir');
|
|
227
|
+
writeFileSync(
|
|
228
|
+
path.join(root, 'index.html'),
|
|
229
|
+
'<script type="module" src="/main.ts"></script>',
|
|
230
|
+
);
|
|
231
|
+
writeFileSync(
|
|
232
|
+
path.join(root, 'main.ts'),
|
|
233
|
+
`import frFR from '@astryxdesign/core/locales/fr-FR.generated.js';\ndocument.body.textContent = frFR['@astryx.pagination.next'];\n`,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
await build({
|
|
238
|
+
root,
|
|
239
|
+
logLevel: 'error',
|
|
240
|
+
build: {outDir, emptyOutDir: true, minify: false},
|
|
241
|
+
plugins: [...astryxStylex({rootDir: root})],
|
|
242
|
+
});
|
|
243
|
+
const assets = path.join(outDir, 'assets');
|
|
244
|
+
const jsName = readdirSync(assets).find(file => file.endsWith('.js'));
|
|
245
|
+
expect(jsName).toBeTruthy();
|
|
246
|
+
expect(readFileSync(path.join(assets, jsName!), 'utf8')).toContain(
|
|
247
|
+
'Aller à la page suivante',
|
|
248
|
+
);
|
|
249
|
+
} finally {
|
|
250
|
+
rmSync(root, {recursive: true, force: true});
|
|
251
|
+
}
|
|
252
|
+
}, 180_000);
|
|
253
|
+
});
|
|
254
|
+
|
|
151
255
|
describe('a production build separates Astryx and product styles by layer', () => {
|
|
152
256
|
describe('when the app imports a stylesheet of its own', () => {
|
|
153
257
|
let built: Built;
|
|
@@ -176,4 +280,55 @@ describe('a production build separates Astryx and product styles by layer', () =
|
|
|
176
280
|
|
|
177
281
|
itSplitsCorrectly(() => built);
|
|
178
282
|
});
|
|
283
|
+
|
|
284
|
+
describe('when the app has multiple HTML entries', () => {
|
|
285
|
+
let built: Built;
|
|
286
|
+
beforeAll(async () => {
|
|
287
|
+
built = await buildFixture('layer-split-multipage', [
|
|
288
|
+
'index.html',
|
|
289
|
+
'setup.html',
|
|
290
|
+
]);
|
|
291
|
+
}, 180_000);
|
|
292
|
+
afterAll(
|
|
293
|
+
() => built && rmSync(built.outDir, {recursive: true, force: true}),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
it('links the shared StyleX stylesheet from every page exactly once', () => {
|
|
297
|
+
for (const html of Object.values(built.htmlByName)) {
|
|
298
|
+
expect(html).toContain(built.cssName);
|
|
299
|
+
expect(
|
|
300
|
+
html.split(built.cssName).length - 1,
|
|
301
|
+
'stylesheet is not linked exactly once',
|
|
302
|
+
).toBe(1);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('keeps entry-owned CSS on its own page', () => {
|
|
307
|
+
const mainCss = Object.entries(built.cssByName).find(([, css]) =>
|
|
308
|
+
css.includes('--multipage-main-only'),
|
|
309
|
+
);
|
|
310
|
+
const setupCss = Object.entries(built.cssByName).find(([, css]) =>
|
|
311
|
+
css.includes('--multipage-setup-only'),
|
|
312
|
+
);
|
|
313
|
+
expect(mainCss).toBeDefined();
|
|
314
|
+
expect(setupCss).toBeDefined();
|
|
315
|
+
|
|
316
|
+
const [mainCssName] = mainCss!;
|
|
317
|
+
const [setupCssName] = setupCss!;
|
|
318
|
+
expect(built.htmlByName['index.html']).toContain(mainCssName);
|
|
319
|
+
expect(built.htmlByName['index.html']).not.toContain(setupCssName);
|
|
320
|
+
expect(built.htmlByName['setup.html']).toContain(setupCssName);
|
|
321
|
+
expect(built.htmlByName['setup.html']).not.toContain(mainCssName);
|
|
322
|
+
expect(built.css).not.toContain('--multipage-main-only');
|
|
323
|
+
expect(built.css).not.toContain('--multipage-setup-only');
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe('a production build validates StyleX declarations', () => {
|
|
329
|
+
it('fails with longhand guidance instead of dropping a border shorthand', async () => {
|
|
330
|
+
await expect(buildFixture('invalid-border')).rejects.toThrow(
|
|
331
|
+
/borderTop is not supported.*borderTopWidth.*borderTopStyle.*borderTopColor/s,
|
|
332
|
+
);
|
|
333
|
+
}, 180_000);
|
|
179
334
|
});
|
package/src/vite.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @file vite.test.ts
|
|
5
|
-
* @description Verifies CSS layer-order injection in the
|
|
5
|
+
* @description Verifies CSS layer-order injection in the Astryx Vite plugin.
|
|
6
6
|
* The library layer name is configurable (default `astryx-base`); the
|
|
7
7
|
* theme layer name is fixed at `astryx-theme`.
|
|
8
8
|
*/
|
|
@@ -16,6 +16,26 @@ import {astryxStylex} from './vite';
|
|
|
16
16
|
|
|
17
17
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
18
18
|
|
|
19
|
+
function getInjectedConfig(rootDir: string): any {
|
|
20
|
+
const plugins = astryxStylex({rootDir});
|
|
21
|
+
const configPlugin = plugins.find(p => p.name === 'astryx-config');
|
|
22
|
+
expect(configPlugin, 'astryx-config plugin should exist').toBeTruthy();
|
|
23
|
+
const config = (configPlugin as any).config;
|
|
24
|
+
return typeof config === 'function' ? config() : config.handler();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function applyAlias(aliases: any[], id: string): string {
|
|
28
|
+
for (const alias of aliases) {
|
|
29
|
+
if (
|
|
30
|
+
(typeof alias.find === 'string' && id.startsWith(alias.find)) ||
|
|
31
|
+
(alias.find instanceof RegExp && alias.find.test(id))
|
|
32
|
+
) {
|
|
33
|
+
return id.replace(alias.find, alias.replacement);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return id;
|
|
37
|
+
}
|
|
38
|
+
|
|
19
39
|
/** Pull the injected `@layer ...;` order statement out of the plugin set. */
|
|
20
40
|
function getLayerOrder(plugins: ReturnType<typeof astryxStylex>): string {
|
|
21
41
|
const layerPlugin = plugins.find(p => p.name === 'astryx-css-layer-order');
|
|
@@ -99,6 +119,26 @@ describe('astryxStylex build-time layer split', () => {
|
|
|
99
119
|
});
|
|
100
120
|
});
|
|
101
121
|
|
|
122
|
+
describe('astryxStylex locale aliases', () => {
|
|
123
|
+
it('routes generated and rich locale imports to their real package files', () => {
|
|
124
|
+
const rootDir = path.join(tmpdir(), 'astryx-vite-alias');
|
|
125
|
+
const aliases = getInjectedConfig(rootDir).resolve.alias;
|
|
126
|
+
const corePackage = path.join(rootDir, 'node_modules/@astryxdesign/core');
|
|
127
|
+
|
|
128
|
+
expect(
|
|
129
|
+
applyAlias(aliases, '@astryxdesign/core/locales/fr-FR.generated.js'),
|
|
130
|
+
).toBe(
|
|
131
|
+
path.join(corePackage, 'src/i18n/generated-locales/fr-FR.generated.ts'),
|
|
132
|
+
);
|
|
133
|
+
expect(applyAlias(aliases, '@astryxdesign/core/locales/fr-FR.json')).toBe(
|
|
134
|
+
path.join(corePackage, 'locales/fr-FR.json'),
|
|
135
|
+
);
|
|
136
|
+
expect(applyAlias(aliases, '@astryxdesign/core/Button')).toBe(
|
|
137
|
+
path.join(corePackage, 'src/Button'),
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
102
142
|
describe('astryxStylex optimizeDeps package discovery', () => {
|
|
103
143
|
let rootDir: string;
|
|
104
144
|
|
package/src/vite.ts
CHANGED
|
@@ -7,6 +7,7 @@ import fs from 'node:fs';
|
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import {fileURLToPath} from 'node:url';
|
|
9
9
|
import {createRequire} from 'node:module';
|
|
10
|
+
import {createHash} from 'node:crypto';
|
|
10
11
|
|
|
11
12
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
13
|
|
|
@@ -136,6 +137,7 @@ export function astryxStylex(
|
|
|
136
137
|
const stylexOptions: Record<string, unknown> = {
|
|
137
138
|
dev,
|
|
138
139
|
runtimeInjection: false,
|
|
140
|
+
propertyValidationMode: 'throw',
|
|
139
141
|
treeshakeCompensation: true,
|
|
140
142
|
unstable_moduleResolution: {
|
|
141
143
|
type: 'commonJS',
|
|
@@ -202,18 +204,35 @@ export function astryxStylex(
|
|
|
202
204
|
// Fallback to just @astryxdesign/core if discovery fails
|
|
203
205
|
}
|
|
204
206
|
|
|
207
|
+
const corePackageDir = path.resolve(
|
|
208
|
+
rootDir,
|
|
209
|
+
'node_modules/@astryxdesign/core',
|
|
210
|
+
);
|
|
211
|
+
const coreSourceDir = path.join(corePackageDir, 'src');
|
|
212
|
+
|
|
205
213
|
return {
|
|
206
214
|
resolve: {
|
|
207
|
-
alias:
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
215
|
+
alias: [
|
|
216
|
+
{
|
|
217
|
+
find: /^@astryxdesign\/core\/locales\/(.+)\.generated\.js$/,
|
|
218
|
+
replacement: path.join(
|
|
219
|
+
coreSourceDir,
|
|
220
|
+
'i18n/generated-locales/$1.generated.ts',
|
|
221
|
+
),
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
find: /^@astryxdesign\/core\/locales\/(.+)\.json$/,
|
|
225
|
+
replacement: path.join(corePackageDir, 'locales/$1.json'),
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
find: '@astryxdesign/core/theme/tokens.stylex',
|
|
229
|
+
replacement: path.join(coreSourceDir, 'theme/tokens.stylex.ts'),
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
find: '@astryxdesign/core',
|
|
233
|
+
replacement: coreSourceDir,
|
|
234
|
+
},
|
|
235
|
+
],
|
|
217
236
|
},
|
|
218
237
|
optimizeDeps: {
|
|
219
238
|
exclude: xdsPackages,
|
|
@@ -282,6 +301,10 @@ export function astryxStylex(
|
|
|
282
301
|
* layer (below `astryx-theme`, so a theme can override them), product styles
|
|
283
302
|
* into the product layer (above it, so an app always wins).
|
|
284
303
|
*
|
|
304
|
+
* The split output is written to one dedicated shared stylesheet. Keeping the
|
|
305
|
+
* collected rules out of an entry-owned CSS asset lets every HTML entry load
|
|
306
|
+
* the same StyleX output without also importing another page's authored CSS.
|
|
307
|
+
*
|
|
285
308
|
* This is the one implementation. The dev middleware and the build hook differ
|
|
286
309
|
* only in when they run and what they do with the string — the partition itself
|
|
287
310
|
* must not diverge, because it did: the build shipped a version that wrapped
|
|
@@ -333,11 +356,12 @@ function renderSplitLayers(
|
|
|
333
356
|
* component overrides were silently dropped in the built app while working in
|
|
334
357
|
* dev.
|
|
335
358
|
*
|
|
336
|
-
* This
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
359
|
+
* This removes that merged block from the entry-owned stylesheet, writes the
|
|
360
|
+
* partitioned pair to a dedicated shared stylesheet, and links it from every
|
|
361
|
+
* built HTML page. It runs in `writeBundle` because StyleX emits through two
|
|
362
|
+
* different paths depending on whether the bundle already has a stylesheet to
|
|
363
|
+
* append to (`generateBundle`) or has to write its own file (`writeBundle`) —
|
|
364
|
+
* on disk, after both, there is one case instead of two.
|
|
341
365
|
*
|
|
342
366
|
* The block is located by an exact match against StyleX's own collector rather
|
|
343
367
|
* than by looking for `@layer priority1`: a wrong guess about where the block
|
|
@@ -388,19 +412,16 @@ function buildLayerSplitPlugin(
|
|
|
388
412
|
// patch and nothing was shipped, so this is not a failure.
|
|
389
413
|
if (!outDir || !fs.existsSync(outDir)) return;
|
|
390
414
|
|
|
391
|
-
const
|
|
415
|
+
const containingFiles: Array<{file: string; css: string; at: number}> =
|
|
416
|
+
[];
|
|
392
417
|
for (const file of listCssFiles(outDir)) {
|
|
393
418
|
const css = fs.readFileSync(file, 'utf-8');
|
|
394
419
|
const at = css.lastIndexOf(merged);
|
|
395
420
|
if (at === -1) continue;
|
|
396
|
-
|
|
397
|
-
file,
|
|
398
|
-
css.slice(0, at) + split + css.slice(at + merged.length),
|
|
399
|
-
);
|
|
400
|
-
patched.push(file);
|
|
421
|
+
containingFiles.push({file, css, at});
|
|
401
422
|
}
|
|
402
423
|
|
|
403
|
-
if (
|
|
424
|
+
if (containingFiles.length === 0) {
|
|
404
425
|
this.error(
|
|
405
426
|
'astryx-build-layer-split: StyleX emitted rules but its CSS block ' +
|
|
406
427
|
`was not found in any stylesheet under ${outDir}, so Astryx and ` +
|
|
@@ -412,7 +433,30 @@ function buildLayerSplitPlugin(
|
|
|
412
433
|
return;
|
|
413
434
|
}
|
|
414
435
|
|
|
415
|
-
|
|
436
|
+
if (listHtmlFiles(outDir).length === 0) {
|
|
437
|
+
for (const {file, css, at} of containingFiles) {
|
|
438
|
+
fs.writeFileSync(
|
|
439
|
+
file,
|
|
440
|
+
css.slice(0, at) + split + css.slice(at + merged.length),
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
for (const {file, css, at} of containingFiles) {
|
|
447
|
+
fs.writeFileSync(
|
|
448
|
+
file,
|
|
449
|
+
css.slice(0, at) + css.slice(at + merged.length),
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const hash = createHash('sha256').update(split).digest('hex').slice(0, 8);
|
|
454
|
+
const sharedStylesheet = path.join(
|
|
455
|
+
path.dirname(containingFiles[0].file),
|
|
456
|
+
`astryx-stylex-${hash}.css`,
|
|
457
|
+
);
|
|
458
|
+
fs.writeFileSync(sharedStylesheet, split);
|
|
459
|
+
linkStylesheetsForEveryPage(outDir, [sharedStylesheet], base);
|
|
416
460
|
},
|
|
417
461
|
};
|
|
418
462
|
}
|
|
@@ -440,20 +484,21 @@ function listHtmlFiles(dir: string): string[] {
|
|
|
440
484
|
}
|
|
441
485
|
|
|
442
486
|
/**
|
|
443
|
-
*
|
|
487
|
+
* Ensure every built page loads the stylesheet containing StyleX's rules.
|
|
444
488
|
*
|
|
445
489
|
* When an app imports no CSS of its own, StyleX has no bundle asset to append
|
|
446
490
|
* to, so it writes `assets/stylex.css` itself — outside Rollup's graph, which
|
|
447
|
-
* means Vite's HTML plugin never learns about it and emits no `<link>`.
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
491
|
+
* means Vite's HTML plugin never learns about it and emits no `<link>`. In a
|
|
492
|
+
* multi-page build, Vite can also attach the one merged StyleX stylesheet to
|
|
493
|
+
* only one entry. The other pages then ship valid compiled classes but load no
|
|
494
|
+
* rules for them.
|
|
451
495
|
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
* library mode — is left alone
|
|
496
|
+
* Check each page independently. A stylesheet already referenced by that page
|
|
497
|
+
* is left alone; a missing reference is inserted exactly once. A build with no
|
|
498
|
+
* HTML at all — library mode — is left alone because its consumer imports the
|
|
499
|
+
* CSS themselves.
|
|
455
500
|
*/
|
|
456
|
-
function
|
|
501
|
+
function linkStylesheetsForEveryPage(
|
|
457
502
|
outDir: string,
|
|
458
503
|
cssFiles: string[],
|
|
459
504
|
base: string,
|
|
@@ -461,25 +506,23 @@ function linkOrphanStylesheets(
|
|
|
461
506
|
const pages = listHtmlFiles(outDir);
|
|
462
507
|
if (pages.length === 0) return;
|
|
463
508
|
|
|
464
|
-
const orphans = cssFiles.filter(css => {
|
|
465
|
-
const name = path.basename(css);
|
|
466
|
-
return !pages.some(page => fs.readFileSync(page, 'utf-8').includes(name));
|
|
467
|
-
});
|
|
468
|
-
if (orphans.length === 0) return;
|
|
469
|
-
|
|
470
|
-
const links = orphans
|
|
471
|
-
.map(css => {
|
|
472
|
-
const href =
|
|
473
|
-
base.replace(/\/$/, '') +
|
|
474
|
-
'/' +
|
|
475
|
-
path.relative(outDir, css).split(path.sep).join('/');
|
|
476
|
-
return `<link rel="stylesheet" crossorigin href="${href}">`;
|
|
477
|
-
})
|
|
478
|
-
.join('\n ');
|
|
479
|
-
|
|
480
509
|
for (const page of pages) {
|
|
481
510
|
const html = fs.readFileSync(page, 'utf-8');
|
|
482
511
|
if (!html.includes('</head>')) continue;
|
|
512
|
+
|
|
513
|
+
const missing = cssFiles.filter(css => !html.includes(path.basename(css)));
|
|
514
|
+
if (missing.length === 0) continue;
|
|
515
|
+
|
|
516
|
+
const links = missing
|
|
517
|
+
.map(css => {
|
|
518
|
+
const href =
|
|
519
|
+
base.replace(/\/$/, '') +
|
|
520
|
+
'/' +
|
|
521
|
+
path.relative(outDir, css).split(path.sep).join('/');
|
|
522
|
+
return `<link rel="stylesheet" crossorigin href="${href}">`;
|
|
523
|
+
})
|
|
524
|
+
.join('\n ');
|
|
525
|
+
|
|
483
526
|
fs.writeFileSync(page, html.replace('</head>', ` ${links}\n </head>`));
|
|
484
527
|
}
|
|
485
528
|
}
|
|
@@ -527,19 +570,23 @@ function astryxStylexLegacy(options: AstryxVitePluginLegacyOptions): Plugin[] {
|
|
|
527
570
|
const libraryLayer = layers.library ?? 'astryx-base';
|
|
528
571
|
const productLayer = layers.product ?? 'product';
|
|
529
572
|
|
|
573
|
+
const validatedStylexOptions = {
|
|
574
|
+
propertyValidationMode: 'throw',
|
|
575
|
+
...(stylexOptions as any),
|
|
576
|
+
};
|
|
530
577
|
const astryxBabelPlugin = path.resolve(__dirname, 'babel.js');
|
|
531
|
-
const existingPlugins =
|
|
578
|
+
const existingPlugins = validatedStylexOptions.babelConfig?.plugins ?? [];
|
|
532
579
|
|
|
533
580
|
const basePlugin = stylex.vite({
|
|
534
|
-
...
|
|
581
|
+
...validatedStylexOptions,
|
|
535
582
|
useCSSLayers: true,
|
|
536
583
|
babelConfig: {
|
|
537
|
-
...
|
|
584
|
+
...validatedStylexOptions.babelConfig,
|
|
538
585
|
plugins: [
|
|
539
586
|
[
|
|
540
587
|
astryxBabelPlugin,
|
|
541
588
|
{
|
|
542
|
-
...
|
|
589
|
+
...validatedStylexOptions,
|
|
543
590
|
libraryPrefix: stylexPrefix,
|
|
544
591
|
babelConfig: undefined,
|
|
545
592
|
},
|