@astryxdesign/build 0.7.11 → 0.7.12-canary.209e067

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 CHANGED
@@ -156,7 +156,7 @@ module.exports = {
156
156
 
157
157
  ### 3. next.config.mjs
158
158
 
159
- Or `withAstryx()` from `@astryxdesign/build/next`, which sets both of these for you.
159
+ Or `withAstryx()` from `@astryxdesign/build/next`, which sets both of these for you. Use `withAstryx()` when the app has a swizzled copy with an `astryx-swizzle.json` marker: this manual config does not rebuild the copy when its marker appears or goes.
160
160
  Either way the work happens in the `webpack` hook, so the build has to run with
161
161
  `--webpack` — see the note at the top of this section.
162
162
 
@@ -253,6 +253,8 @@ Library patterns (configurable):
253
253
  - `packages/themes/`
254
254
  - `node_modules/@astryxdesign/`
255
255
 
256
+ A swizzled copy is library code too. The Babel, PostCSS, and Vite integrations compile every module under a folder that holds an `astryx-swizzle.json` marker with the library prefix into `@layer astryx-base`, so a theme's component overrides reach the copy as they reach the package component. `astryx swizzle` does not write the marker yet: add it to the swizzle output folder, naming the package the copy came from and its version, such as `{"packages": {"@astryxdesign/core": "0.7.10"}}`. The build reads only whether the file is there. Under webpack, use `withAstryx()` with a marked copy: it makes each app module depend on the marker paths above it, so a dev session or a cached build recompiles a copy when its marker appears or disappears. In another setup that caches transforms, restart the dev server and delete that cache after adding or removing a marker. Turbopack builds do not read the marker yet.
257
+
256
258
  ### PostCSS plugin (`@astryxdesign/build/postcss`)
257
259
 
258
260
  Compiles StyleX from both library and product source files in two separate passes with different prefixes. Wraps the results in named `@layer` blocks:
package/dist/vite.mjs CHANGED
@@ -29,6 +29,13 @@ function undefinedGeneratedVariables(css, prefixes, others = []) {
29
29
 
30
30
  // src/vite.ts
31
31
  var __dirname = path.dirname(fileURLToPath(import.meta.url));
32
+ var markerReader;
33
+ function isSwizzledSource(filePath, memo) {
34
+ markerReader ??= createRequire(import.meta.url)(
35
+ path.resolve(__dirname, "../src/swizzle-marker.js")
36
+ );
37
+ return markerReader.isSwizzledSource(filePath, memo);
38
+ }
32
39
  var LIBRARY_PATTERN = "node_modules/@astryxdesign/";
33
40
  var STYLEX_CSS_PATH = "/virtual:stylex.css";
34
41
  var LIGHTNINGCSS_TARGETS = {
@@ -200,8 +207,9 @@ function renderSplitLayers(rulesById, options) {
200
207
  const libraryRules = [];
201
208
  const productRules = [];
202
209
  const collectedConstants = [];
210
+ const markerMemo = /* @__PURE__ */ new Map();
203
211
  for (const [filePath, rules] of rulesById.entries()) {
204
- const own = filePath.includes(options.libraryPattern) ? libraryRules : productRules;
212
+ const own = filePath.includes(options.libraryPattern) || isSwizzledSource(filePath, markerMemo) ? libraryRules : productRules;
205
213
  for (const rule of rules) {
206
214
  if (isConstantRule(rule)) {
207
215
  collectedConstants.push(rule);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/build",
3
- "version": "0.7.11",
3
+ "version": "0.7.12-canary.209e067",
4
4
  "description": "Build plugins for Astryx source builds — babel, PostCSS, and Vite integrations",
5
5
  "author": "Meta Open Source",
6
6
  "license": "MIT",
package/src/babel.js CHANGED
@@ -11,6 +11,9 @@
11
11
  * Library files get 'astryx' prefix (.astryx78zum5), product files get 'x' (.x78zum5).
12
12
  */
13
13
 
14
+ const path = require('node:path');
15
+ const {isSwizzledSource} = require('./swizzle-marker');
16
+
14
17
  const LIBRARY_PATTERNS = [
15
18
  'packages/core/',
16
19
  'packages/themes/',
@@ -47,8 +50,17 @@ module.exports = function astryxBabelPlugin(api, options) {
47
50
  const libraryPlugin = stylexPlugin(api, libraryOpts);
48
51
  const productPlugin = stylexPlugin(api, productOpts);
49
52
 
50
- function getPluginAndOpts(filename) {
51
- const isLibrary = libraryPatterns.some(p => filename.includes(p));
53
+ // Each transform's file is classified once, not on every visitor call.
54
+ const libraryFiles = new WeakMap();
55
+ function getPluginAndOpts(state) {
56
+ let isLibrary = libraryFiles.get(state.file);
57
+ if (isLibrary === undefined) {
58
+ const filename = state.filename || '';
59
+ isLibrary =
60
+ libraryPatterns.some(p => filename.includes(p)) ||
61
+ (filename !== '' && isSwizzledSource(path.resolve(filename)));
62
+ if (state.file != null) libraryFiles.set(state.file, isLibrary);
63
+ }
52
64
  return isLibrary
53
65
  ? {plugin: libraryPlugin, opts: libraryOpts}
54
66
  : {plugin: productPlugin, opts: productOpts};
@@ -56,7 +68,7 @@ module.exports = function astryxBabelPlugin(api, options) {
56
68
 
57
69
  // Helper: call a visitor function with overridden state.opts
58
70
  function callVisitor(visitor, key, path, state) {
59
- const {plugin, opts} = getPluginAndOpts(state.filename || '');
71
+ const {plugin, opts} = getPluginAndOpts(state);
60
72
  const v = plugin.visitor[key];
61
73
  if (!v) return;
62
74
 
package/src/index.js CHANGED
@@ -23,6 +23,7 @@
23
23
  */
24
24
 
25
25
  const path = require('node:path');
26
+ const {isSwizzledSource} = require('./swizzle-marker');
26
27
  const fs = require('node:fs');
27
28
  const babel = require('@babel/core');
28
29
  const stylexBabelPlugin = require('@stylexjs/babel-plugin');
@@ -242,11 +243,24 @@ function createPlugin() {
242
243
  // Partition files into library vs product, then compile
243
244
  // each group with its own babel config (different prefix)
244
245
  const transforms = [];
246
+ // A swizzled copy is Astryx source too: it compiles in the library pass.
247
+ const markerMemo = new Map();
245
248
  for (const filePath of files) {
249
+ // Classify every file on each pass: when a marker appears or goes,
250
+ // `withAstryx()` rebuilds the copy with the other prefix.
251
+ const isLibrary =
252
+ libraryPatterns.some(p => filePath.includes(p)) ||
253
+ isSwizzledSource(filePath, markerMemo);
254
+ const rulesMap = isLibrary ? libraryRulesMap : productRulesMap;
255
+ const movedSides = (
256
+ isLibrary ? productRulesMap : libraryRulesMap
257
+ ).delete(filePath);
258
+
246
259
  const mtimeMs = fs.existsSync(filePath)
247
260
  ? fs.statSync(filePath).mtimeMs
248
261
  : -Infinity;
249
262
  if (
263
+ !movedSides &&
250
264
  fileModifiedMap.has(filePath) &&
251
265
  mtimeMs === fileModifiedMap.get(filePath)
252
266
  ) {
@@ -257,9 +271,7 @@ function createPlugin() {
257
271
  const contents = fs.readFileSync(filePath, 'utf-8');
258
272
  if (!contents.includes(STYLEX_IMPORT_SOURCE)) continue;
259
273
 
260
- const isLibrary = libraryPatterns.some(p => filePath.includes(p));
261
274
  const config = isLibrary ? libraryBabelConfig : productBabelConfig;
262
- const rulesMap = isLibrary ? libraryRulesMap : productRulesMap;
263
275
 
264
276
  transforms.push(
265
277
  babel
package/src/next.js CHANGED
@@ -30,6 +30,7 @@ const ASTRYX_MODULE = /[\\/]node_modules[\\/]@astryxdesign[\\/]/;
30
30
 
31
31
  const escapeRegExp = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
32
32
  const TURBOPACK_LOADER = require.resolve('./turbopack-loader.js');
33
+ const SWIZZLE_MARKER_LOADER = require.resolve('./swizzle-marker-loader.js');
33
34
 
34
35
  /**
35
36
  * Match a wildcard alias key such as `@astryxdesign/*` against a request. The
@@ -370,6 +371,14 @@ function withAstryx(nextConfig = {}) {
370
371
  .map(name => findPackageDir(name, from))
371
372
  .filter(dir => dir != null)
372
373
  .map(dir => escapeRegExp(fs.realpathSync(dir) + path.sep));
374
+ // The app's own modules depend on the swizzle markers above them, so a
375
+ // cached build recompiles a copy when its marker appears or disappears.
376
+ config.module.rules.unshift({
377
+ test: /\.[cm]?[jt]sx?$/,
378
+ exclude: /[\\/]node_modules[\\/]/,
379
+ enforce: 'pre',
380
+ use: [{loader: SWIZZLE_MARKER_LOADER}],
381
+ });
373
382
  config.module.rules.unshift({
374
383
  test:
375
384
  realPackageDirs.length === 0
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use strict';
4
+
5
+ /**
6
+ * @astryxdesign/build: a webpack pre-loader that `withAstryx()` installs for
7
+ * the app's own modules. It makes each module depend on every swizzle marker
8
+ * path that decides how the module compiles, so webpack rebuilds the module,
9
+ * and its cached class names, when a marker appears or disappears. It does
10
+ * not change the source.
11
+ */
12
+
13
+ const fs = require('node:fs');
14
+ const {markerPaths} = require('./swizzle-marker');
15
+
16
+ module.exports = function astryxSwizzleMarkerLoader(source, map, meta) {
17
+ for (const marker of markerPaths(this.resourcePath, this.rootContext)) {
18
+ if (fs.existsSync(marker)) {
19
+ this.addDependency(marker);
20
+ break;
21
+ }
22
+ this.addMissingDependency(marker);
23
+ }
24
+ this.callback(null, source, map, meta);
25
+ };
@@ -0,0 +1,19 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /** The marker file in a swizzle output folder. */
4
+ export declare const SWIZZLE_MARKER: string;
5
+
6
+ /**
7
+ * Whether a module is in a swizzled copy: a folder at or above it holds the
8
+ * marker. One `memo` per build pass looks each folder up once.
9
+ */
10
+ /** The marker paths from a module's folder up to `stopDir` or the root. */
11
+ export declare function markerPaths(
12
+ filePath: string,
13
+ stopDir?: string,
14
+ ): string[];
15
+
16
+ export declare function isSwizzledSource(
17
+ filePath: string,
18
+ memo?: Map<string, boolean>,
19
+ ): boolean;
@@ -0,0 +1,68 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use strict';
4
+
5
+ /**
6
+ * @astryxdesign/build: swizzled copies. A swizzle output folder holds an
7
+ * `astryx-swizzle.json` marker. Every module under a folder that holds the
8
+ * marker is Astryx source: the library pass compiles it with
9
+ * the library class prefix into the library layer, below `astryx-theme`, so a
10
+ * theme's component overrides reach the copy as they reach the package.
11
+ */
12
+
13
+ const fs = require('node:fs');
14
+ const path = require('node:path');
15
+
16
+ /** The marker file in a swizzle output folder. */
17
+ const SWIZZLE_MARKER = 'astryx-swizzle.json';
18
+
19
+ /**
20
+ * Whether a module is in a swizzled copy: a folder at or above it holds the
21
+ * marker. Pass one `memo` to the checks of one build pass, so each folder is
22
+ * looked up once; nothing is kept between passes, so a copy swizzled while a
23
+ * dev server runs counts from the next pass.
24
+ * @param {string} filePath an absolute module path
25
+ * @param {Map<string, boolean>} [memo] folder -> whether it or a folder
26
+ * above it holds the marker
27
+ * @returns {boolean}
28
+ */
29
+ function isSwizzledSource(filePath, memo = new Map()) {
30
+ /** @type {string[]} */
31
+ const visited = [];
32
+ let dir = path.dirname(filePath);
33
+ let marked = false;
34
+ for (;;) {
35
+ const known = memo.get(dir);
36
+ if (known !== undefined) {
37
+ marked = known;
38
+ break;
39
+ }
40
+ visited.push(dir);
41
+ if (fs.existsSync(path.join(dir, SWIZZLE_MARKER))) {
42
+ marked = true;
43
+ break;
44
+ }
45
+ const parent = path.dirname(dir);
46
+ if (parent === dir) break;
47
+ dir = parent;
48
+ }
49
+ for (const folder of visited) memo.set(folder, marked);
50
+ return marked;
51
+ }
52
+
53
+ /**
54
+ * The marker paths that decide whether a module is in a swizzled copy, from
55
+ * its own folder up to `stopDir` (or the file system root).
56
+ * @param {string} filePath an absolute module path
57
+ * @param {string} [stopDir] the last folder to check, such as the project root
58
+ * @returns {string[]}
59
+ */
60
+ function markerPaths(filePath, stopDir) {
61
+ const paths = [];
62
+ for (let dir = path.dirname(filePath); ; dir = path.dirname(dir)) {
63
+ paths.push(path.join(dir, SWIZZLE_MARKER));
64
+ if (dir === stopDir || path.dirname(dir) === dir) return paths;
65
+ }
66
+ }
67
+
68
+ module.exports = {SWIZZLE_MARKER, isSwizzledSource, markerPaths};
package/src/vite.ts CHANGED
@@ -11,6 +11,15 @@ import {createHash} from 'node:crypto';
11
11
  import {undefinedGeneratedVariables} from './undefinedVariables';
12
12
 
13
13
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
14
+ // The CommonJS marker reader the Babel and PostCSS integrations share,
15
+ // loaded on first use from beside babel.js, so every integration runs one copy.
16
+ let markerReader: typeof import('./swizzle-marker.js') | undefined;
17
+ function isSwizzledSource(filePath: string, memo: Map<string, boolean>) {
18
+ markerReader ??= createRequire(import.meta.url)(
19
+ path.resolve(__dirname, 'swizzle-marker.js'),
20
+ ) as typeof import('./swizzle-marker.js');
21
+ return markerReader.isSwizzledSource(filePath, memo);
22
+ }
14
23
 
15
24
  const LIBRARY_PATTERN = 'node_modules/@astryxdesign/';
16
25
  const STYLEX_CSS_PATH = '/virtual:stylex.css';
@@ -356,10 +365,14 @@ function renderSplitLayers(
356
365
  // their own, so repeating them adds nothing to the output.
357
366
  const collectedConstants: unknown[] = [];
358
367
 
368
+ // A swizzled copy is Astryx source too: its rules go in the library layer.
369
+ const markerMemo = new Map<string, boolean>();
359
370
  for (const [filePath, rules] of rulesById.entries()) {
360
- const own = filePath.includes(options.libraryPattern)
361
- ? libraryRules
362
- : productRules;
371
+ const own =
372
+ filePath.includes(options.libraryPattern) ||
373
+ isSwizzledSource(filePath, markerMemo)
374
+ ? libraryRules
375
+ : productRules;
363
376
  for (const rule of rules) {
364
377
  if (isConstantRule(rule)) {
365
378
  collectedConstants.push(rule);