@astryxdesign/build 0.7.7-canary.aa86487 → 0.7.7-canary.ae97fa8
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 +1 -1
- package/src/source-stylesheet.js +117 -59
package/package.json
CHANGED
package/src/source-stylesheet.js
CHANGED
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @astryxdesign/build: source stylesheets. A source build of a package's
|
|
7
7
|
* modules prints its prebuilt stylesheet's rules, selectors and order. It
|
|
8
|
-
* prints them in one `@layer astryx-base` after the layer order.
|
|
9
|
-
*
|
|
10
|
-
* (scripts/build-css.mjs)
|
|
11
|
-
*
|
|
12
|
-
* same options (turbopack-loader.js), so rendered class names match the rules.
|
|
8
|
+
* prints them in one `@layer astryx-base` after the layer order. The walk,
|
|
9
|
+
* the compile options and the layer block here also build the prebuilt
|
|
10
|
+
* stylesheets (scripts/build-css.mjs). The bundler compiles the same modules
|
|
11
|
+
* with the same options (turbopack-loader.js), so class names match the rules.
|
|
13
12
|
*/
|
|
14
13
|
|
|
15
14
|
const fs = require('node:fs');
|
|
@@ -54,10 +53,9 @@ const PREBUILT_STYLESHEETS = {
|
|
|
54
53
|
|
|
55
54
|
/**
|
|
56
55
|
* Variable modules a prebuilt stylesheet compiles though none of the package's
|
|
57
|
-
* modules imports them
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* targets, fails if this list drifts from it.
|
|
56
|
+
* modules imports them. Core's public `dataVars` entry is one, since an app
|
|
57
|
+
* reads its defaults as plain `var()` references. The targets of
|
|
58
|
+
* scripts/build-css.mjs take their `stylexEntries` from this list.
|
|
61
59
|
*/
|
|
62
60
|
const STYLEX_ENTRIES = {[CORE]: ['theme/dataTokens.stylex.ts']};
|
|
63
61
|
|
|
@@ -128,25 +126,35 @@ function prebuiltStylesheet(pkg) {
|
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
/**
|
|
131
|
-
* The StyleX options
|
|
129
|
+
* The StyleX options every prebuilt stylesheet is compiled with: the default
|
|
132
130
|
* `x` class-name prefix, no development class names and StyleX's default
|
|
133
|
-
* property validation.
|
|
134
|
-
|
|
131
|
+
* property validation.
|
|
132
|
+
*/
|
|
133
|
+
const PREBUILT_OPTIONS = {
|
|
134
|
+
dev: false,
|
|
135
|
+
runtimeInjection: false,
|
|
136
|
+
genConditionalClasses: true,
|
|
137
|
+
treeshakeCompensation: true,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** The aliases that point another package's imports of Core at Core's source. */
|
|
141
|
+
function coreAliases(srcDir) {
|
|
142
|
+
return {
|
|
143
|
+
'@astryxdesign/core/*': [path.join(srcDir, '*')],
|
|
144
|
+
'@astryxdesign/core': [srcDir],
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* An installed package's StyleX options. They are plain JSON, since Turbopack
|
|
150
|
+
* passes only serializable loader options. `pluginOptions()` adds the
|
|
151
|
+
* resolution's functions.
|
|
135
152
|
*/
|
|
136
153
|
function stylexOptions(pkg, packages) {
|
|
137
154
|
const core = packages.find(candidate => candidate.name === CORE);
|
|
138
155
|
return {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
genConditionalClasses: true,
|
|
142
|
-
treeshakeCompensation: true,
|
|
143
|
-
aliases:
|
|
144
|
-
pkg.name === CORE || core == null
|
|
145
|
-
? {}
|
|
146
|
-
: {
|
|
147
|
-
'@astryxdesign/core/*': [path.join(core.srcDir, '*')],
|
|
148
|
-
'@astryxdesign/core': [core.srcDir],
|
|
149
|
-
},
|
|
156
|
+
...PREBUILT_OPTIONS,
|
|
157
|
+
aliases: pkg.name === CORE || core == null ? {} : coreAliases(core.srcDir),
|
|
150
158
|
unstable_moduleResolution: {type: 'custom', rootDir: pkg.dir},
|
|
151
159
|
};
|
|
152
160
|
}
|
|
@@ -166,6 +174,13 @@ const UNRESOLVED = new Set([
|
|
|
166
174
|
* `<package name>:<path inside it>`, so Core's names do not change.
|
|
167
175
|
*/
|
|
168
176
|
function pluginOptions(options) {
|
|
177
|
+
// The prebuilt build passes the resolver the packages' JavaScript builds use.
|
|
178
|
+
// Keep it, so CSS and JavaScript name each variable the same way.
|
|
179
|
+
if (
|
|
180
|
+
typeof options.unstable_moduleResolution.filePathResolver === 'function'
|
|
181
|
+
) {
|
|
182
|
+
return options;
|
|
183
|
+
}
|
|
169
184
|
const {rootDir} = options.unstable_moduleResolution;
|
|
170
185
|
let packages;
|
|
171
186
|
const owner = request =>
|
|
@@ -265,33 +280,26 @@ function resolveStyleXImport(specifier, importer, aliases) {
|
|
|
265
280
|
}
|
|
266
281
|
|
|
267
282
|
/**
|
|
268
|
-
* The modules a
|
|
269
|
-
*
|
|
270
|
-
* `.stylex`
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
* parity test in source-stylesheet.test.mjs fails if this drifts from
|
|
274
|
-
* scripts/build-css.mjs.
|
|
283
|
+
* The modules a stylesheet is compiled from. The walk starts at every source
|
|
284
|
+
* file under `srcDir` except tests and `.stylex` modules, plus `entries`. It
|
|
285
|
+
* follows static `.stylex` imports, through `aliases`. A module in `provided`
|
|
286
|
+
* belongs to another stylesheet. The walk adds each one it reaches to
|
|
287
|
+
* `imported`.
|
|
275
288
|
*/
|
|
276
|
-
function
|
|
277
|
-
|
|
278
|
-
packages,
|
|
289
|
+
function reachableModules(
|
|
290
|
+
{srcDir, aliases, entries = []},
|
|
279
291
|
provided = new Set(),
|
|
280
292
|
imported = new Set(),
|
|
281
293
|
) {
|
|
282
|
-
const {aliases} = stylexOptions(pkg, packages);
|
|
283
294
|
const queue = [
|
|
284
295
|
...globSync('**/*.{ts,tsx}', {
|
|
285
|
-
cwd:
|
|
296
|
+
cwd: srcDir,
|
|
286
297
|
absolute: true,
|
|
287
298
|
ignore: ['**/*.test.*', '**/*.d.ts', '**/node_modules/**'],
|
|
288
299
|
})
|
|
289
300
|
.map(file => path.normalize(file))
|
|
290
301
|
.filter(file => !STYLEX_MODULE.test(file)),
|
|
291
|
-
|
|
292
|
-
...(STYLEX_ENTRIES[pkg.name] ?? [])
|
|
293
|
-
.map(entry => path.join(pkg.srcDir, entry))
|
|
294
|
-
.filter(file => fs.existsSync(file)),
|
|
302
|
+
...entries,
|
|
295
303
|
].filter(file => !provided.has(file));
|
|
296
304
|
const modules = new Set();
|
|
297
305
|
while (queue.length > 0) {
|
|
@@ -314,20 +322,32 @@ function packageModules(
|
|
|
314
322
|
return modules;
|
|
315
323
|
}
|
|
316
324
|
|
|
325
|
+
function packageWalk(pkg, packages) {
|
|
326
|
+
return {
|
|
327
|
+
srcDir: pkg.srcDir,
|
|
328
|
+
aliases: stylexOptions(pkg, packages).aliases,
|
|
329
|
+
// A version older than an entry has none of it to print.
|
|
330
|
+
entries: (STYLEX_ENTRIES[pkg.name] ?? [])
|
|
331
|
+
.map(entry => path.join(pkg.srcDir, entry))
|
|
332
|
+
.filter(file => fs.existsSync(file)),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function packageModules(pkg, packages, provided, imported) {
|
|
337
|
+
return reachableModules(packageWalk(pkg, packages), provided, imported);
|
|
338
|
+
}
|
|
339
|
+
|
|
317
340
|
/** A `defineConsts` value, as StyleX collects it: no CSS of its own. */
|
|
318
341
|
const isConstant = rule =>
|
|
319
342
|
rule[1]?.constKey != null && rule[1]?.constVal != null;
|
|
320
343
|
|
|
321
344
|
/**
|
|
322
|
-
* Every rule of a
|
|
323
|
-
*
|
|
324
|
-
* constants it reads from a provider's modules. Those constants resolve where
|
|
325
|
-
* they are read.
|
|
345
|
+
* Every rule of a stylesheet: its modules' rules, plus the constants it reads
|
|
346
|
+
* from provided modules. Those constants resolve where they are read.
|
|
326
347
|
*/
|
|
327
|
-
async function
|
|
328
|
-
const options = stylexOptions(pkg, packages);
|
|
348
|
+
async function collectRules(walk, provided, options) {
|
|
329
349
|
const imported = new Set();
|
|
330
|
-
const modules =
|
|
350
|
+
const modules = reachableModules(walk, provided, imported);
|
|
331
351
|
const rules = [];
|
|
332
352
|
for (const file of modules) {
|
|
333
353
|
rules.push(...(await compileRules(file, options)));
|
|
@@ -338,6 +358,14 @@ async function packageRules(pkg, packages, provided) {
|
|
|
338
358
|
return {modules, rules};
|
|
339
359
|
}
|
|
340
360
|
|
|
361
|
+
function packageRules(pkg, packages, provided) {
|
|
362
|
+
return collectRules(
|
|
363
|
+
packageWalk(pkg, packages),
|
|
364
|
+
provided,
|
|
365
|
+
stylexOptions(pkg, packages),
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
341
369
|
const compiled = new Map();
|
|
342
370
|
|
|
343
371
|
function stamp(file) {
|
|
@@ -345,9 +373,22 @@ function stamp(file) {
|
|
|
345
373
|
return `${stat.mtimeMs}:${stat.size}`;
|
|
346
374
|
}
|
|
347
375
|
|
|
376
|
+
// `JSON.stringify` drops a resolver's functions, so each resolver object a
|
|
377
|
+
// caller sets gets an id in the cache key.
|
|
378
|
+
const resolverIds = new WeakMap();
|
|
379
|
+
let nextResolverId = 0;
|
|
380
|
+
|
|
381
|
+
function resolverKey({unstable_moduleResolution: resolution}) {
|
|
382
|
+
if (typeof resolution?.filePathResolver !== 'function') return '';
|
|
383
|
+
if (!resolverIds.has(resolution)) {
|
|
384
|
+
resolverIds.set(resolution, nextResolverId++);
|
|
385
|
+
}
|
|
386
|
+
return `:${resolverIds.get(resolution)}`;
|
|
387
|
+
}
|
|
388
|
+
|
|
348
389
|
/** One module's StyleX rules, cached on its modification stamp. */
|
|
349
390
|
async function compileRules(file, options) {
|
|
350
|
-
const key = `${stamp(file)}:${JSON.stringify(options)}`;
|
|
391
|
+
const key = `${stamp(file)}:${JSON.stringify(options)}${resolverKey(options)}`;
|
|
351
392
|
if (compiled.get(file)?.key === key) {
|
|
352
393
|
return compiled.get(file).rules;
|
|
353
394
|
}
|
|
@@ -368,23 +409,31 @@ async function compileRules(file, options) {
|
|
|
368
409
|
return rules;
|
|
369
410
|
}
|
|
370
411
|
|
|
371
|
-
/**
|
|
372
|
-
* The cascade-layer order every Astryx stylesheet registers before its rules,
|
|
373
|
-
* the line scripts/build-css.mjs writes into each prebuilt stylesheet.
|
|
374
|
-
*/
|
|
412
|
+
/** The cascade-layer order every Astryx stylesheet registers before its rules. */
|
|
375
413
|
const LAYER_ORDER = '@layer reset, astryx-base, astryx-theme;';
|
|
376
414
|
|
|
415
|
+
/** A stylesheet's block: the layer order, then `css` inside `@layer astryx-base`. */
|
|
416
|
+
function layerBlock(css) {
|
|
417
|
+
const body = css
|
|
418
|
+
.split('\n')
|
|
419
|
+
.map(line => ` ${line}`)
|
|
420
|
+
.join('\n');
|
|
421
|
+
return `${LAYER_ORDER}\n\n@layer astryx-base {\n${body}\n}\n`;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** StyleX's CSS for `rules`, from the copy of StyleX that compiled them. */
|
|
425
|
+
function processRules(rules) {
|
|
426
|
+
return stylexBabelPlugin().processStylexRules(rules, false);
|
|
427
|
+
}
|
|
428
|
+
|
|
377
429
|
/**
|
|
378
|
-
* Print a package's rules as its prebuilt stylesheet prints them
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
* holds whichever block or chunk loads first. The product needs no line of its
|
|
382
|
-
* own.
|
|
430
|
+
* Print a package's rules as its prebuilt stylesheet prints them, or nothing
|
|
431
|
+
* when there are no rules. Each block carries the order, so the order holds
|
|
432
|
+
* whichever block or chunk loads first. The product needs no line of its own.
|
|
383
433
|
*/
|
|
384
434
|
function renderPackage(rules) {
|
|
385
|
-
const css =
|
|
386
|
-
|
|
387
|
-
return `${LAYER_ORDER}\n\n@layer astryx-base {\n${css.replace(/^/gm, ' ')}\n}\n`;
|
|
435
|
+
const css = processRules(rules);
|
|
436
|
+
return css.trim() ? layerBlock(css) : '';
|
|
388
437
|
}
|
|
389
438
|
|
|
390
439
|
/**
|
|
@@ -411,13 +460,22 @@ module.exports = {
|
|
|
411
460
|
ASTRYX_PACKAGES,
|
|
412
461
|
SOURCE_PACKAGES,
|
|
413
462
|
PREBUILT_STYLESHEETS,
|
|
463
|
+
PREBUILT_OPTIONS,
|
|
464
|
+
STYLEX_ENTRIES,
|
|
465
|
+
STYLEX_MODULE,
|
|
466
|
+
LAYER_ORDER,
|
|
414
467
|
prebuiltStylesheet,
|
|
415
468
|
findPackageDir,
|
|
416
469
|
sourcePackages,
|
|
470
|
+
coreAliases,
|
|
417
471
|
stylexOptions,
|
|
418
472
|
pluginOptions,
|
|
473
|
+
reachableModules,
|
|
419
474
|
packageModules,
|
|
475
|
+
collectRules,
|
|
420
476
|
compileRules,
|
|
477
|
+
processRules,
|
|
478
|
+
layerBlock,
|
|
421
479
|
renderPackage,
|
|
422
480
|
resolveStyleXImport,
|
|
423
481
|
sourceStylesheet,
|