@astryxdesign/build 0.7.2-canary.fe2c655 → 0.7.3-canary.1379cb7
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/index.js +81 -17
- package/src/next.js +24 -8
- package/src/next.test.mjs +116 -0
- package/src/postcss.test.mjs +135 -0
- package/src/postcss.workspace-links.test.mjs +163 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -11,22 +11,29 @@
|
|
|
11
11
|
*
|
|
12
12
|
* reset < astryx-base (library, prefix: 'astryx') < astryx-theme < product (prefix: 'x')
|
|
13
13
|
*
|
|
14
|
+
* Library source is what each installed Astryx package publishes (its
|
|
15
|
+
* `files`), so a package linked from a workspace contributes what a registry
|
|
16
|
+
* install of it would.
|
|
17
|
+
*
|
|
14
18
|
* The separate prefixes ensure atomic classes don't collide between
|
|
15
19
|
* layers, which would break theme overrides. Generated replacement CSS is
|
|
16
20
|
* parsed by the host PostCSS instance so consumers do not load a second copy.
|
|
21
|
+
* The library pass reads no Babel config file, so the app's own Babel config
|
|
22
|
+
* never transforms Astryx source; the product pass still loads it.
|
|
17
23
|
*/
|
|
18
24
|
|
|
19
25
|
const path = require('node:path');
|
|
20
26
|
const fs = require('node:fs');
|
|
21
27
|
const babel = require('@babel/core');
|
|
22
28
|
const stylexBabelPlugin = require('@stylexjs/babel-plugin');
|
|
23
|
-
const {globSync} = require('fast-glob');
|
|
29
|
+
const {escapePath, globSync} = require('fast-glob');
|
|
24
30
|
const isGlob = require('is-glob');
|
|
25
31
|
const globParent = require('glob-parent');
|
|
26
32
|
|
|
27
33
|
const PLUGIN_NAME = '@astryxdesign/postcss-plugin';
|
|
28
34
|
|
|
29
|
-
const
|
|
35
|
+
const LIBRARY_SCOPE = 'node_modules/@astryxdesign';
|
|
36
|
+
const LIBRARY_SOURCE = /\.tsx?$/;
|
|
30
37
|
const LIBRARY_PATTERNS = [
|
|
31
38
|
'node_modules/@astryxdesign/',
|
|
32
39
|
'packages/core/',
|
|
@@ -34,6 +41,54 @@ const LIBRARY_PATTERNS = [
|
|
|
34
41
|
];
|
|
35
42
|
const STYLEX_IMPORT_SOURCE = '@stylexjs/stylex';
|
|
36
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Globs for the source each installed Astryx package publishes: each `files`
|
|
46
|
+
* entry, a directory (the files below it) or a file, or the whole package when
|
|
47
|
+
* it has none; an entry with glob characters or a leading `!` is refused, not
|
|
48
|
+
* approximated. A package's own `node_modules` is never scanned: in a
|
|
49
|
+
* workspace its links can lead back to the package (here, core's dev
|
|
50
|
+
* dependency `@astryxdesign/cli` links core again).
|
|
51
|
+
*/
|
|
52
|
+
function libraryGlobs(cwd) {
|
|
53
|
+
const include = [];
|
|
54
|
+
const ignore = [];
|
|
55
|
+
const scope = path.join(cwd, LIBRARY_SCOPE);
|
|
56
|
+
const seen = new Set();
|
|
57
|
+
for (const name of fs.existsSync(scope) ? fs.readdirSync(scope) : []) {
|
|
58
|
+
const dir = path.join(scope, name);
|
|
59
|
+
const manifest = path.join(dir, 'package.json');
|
|
60
|
+
if (!fs.existsSync(manifest)) continue;
|
|
61
|
+
const real = fs.realpathSync(dir);
|
|
62
|
+
if (seen.has(real)) continue;
|
|
63
|
+
seen.add(real);
|
|
64
|
+
const base = `${LIBRARY_SCOPE}/${name}`;
|
|
65
|
+
ignore.push(`${base}/**/node_modules/**`);
|
|
66
|
+
const {files} = JSON.parse(fs.readFileSync(manifest, 'utf8'));
|
|
67
|
+
if (!Array.isArray(files)) {
|
|
68
|
+
include.push(`${base}/**/*.{ts,tsx}`);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
for (const entry of files) {
|
|
72
|
+
// isGlob also flags a leading `!`.
|
|
73
|
+
if (isGlob(entry)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`@astryxdesign/build reads plain files entries only; @astryxdesign/${name} lists "${entry}"`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
const plain = entry.replace(/^\.?\/|\/$/g, '');
|
|
79
|
+
const target = path.join(dir, plain);
|
|
80
|
+
const isDirectory =
|
|
81
|
+
fs.existsSync(target) && fs.statSync(target).isDirectory();
|
|
82
|
+
include.push(
|
|
83
|
+
isDirectory
|
|
84
|
+
? `${base}/${escapePath(plain)}/**/*.{ts,tsx}`
|
|
85
|
+
: `${base}/${escapePath(plain)}`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {include, ignore};
|
|
90
|
+
}
|
|
91
|
+
|
|
37
92
|
function parseDependency(fileOrGlob, cwd) {
|
|
38
93
|
if (fileOrGlob.startsWith('!')) return null;
|
|
39
94
|
if (isGlob(fileOrGlob)) {
|
|
@@ -91,17 +146,18 @@ function createPlugin() {
|
|
|
91
146
|
libraryPatterns = LIBRARY_PATTERNS,
|
|
92
147
|
exclude = [],
|
|
93
148
|
}) => {
|
|
94
|
-
const
|
|
95
|
-
`${appDir}/**/*.{js,jsx,ts,tsx}`,
|
|
96
|
-
LIBRARY_GLOB,
|
|
97
|
-
...extraInclude,
|
|
98
|
-
];
|
|
149
|
+
const appInclude = `${appDir}/**/*.{js,jsx,ts,tsx}`;
|
|
99
150
|
|
|
100
151
|
const excludeWithDefaults = ['**/*.d.ts', '**/*.flow', ...exclude];
|
|
101
152
|
|
|
102
153
|
// Two babel configs — different classNamePrefix
|
|
103
154
|
const libraryBabelConfig = {
|
|
104
155
|
babelrc: false,
|
|
156
|
+
// Astryx source needs only the StyleX plugin. Without this, Babel also
|
|
157
|
+
// loads the app's root config (`next/babel` in the documented setup),
|
|
158
|
+
// and its preset-env rewrites package source the app cannot change,
|
|
159
|
+
// such as `\p{…}` regular expressions, before StyleX reads it.
|
|
160
|
+
configFile: false,
|
|
105
161
|
parserOpts: {plugins: ['typescript', 'jsx']},
|
|
106
162
|
plugins: withClassNamePrefix(babelPlugins, libraryPrefix),
|
|
107
163
|
};
|
|
@@ -128,18 +184,26 @@ function createPlugin() {
|
|
|
128
184
|
});
|
|
129
185
|
if (styleXAtRule == null) return;
|
|
130
186
|
|
|
131
|
-
// Discover files
|
|
187
|
+
// Discover files: the app's own, and the source each installed
|
|
188
|
+
// Astryx package publishes
|
|
189
|
+
const library = libraryGlobs(cwd);
|
|
132
190
|
const files = new Set();
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
onlyFiles: true,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
for (const f of matched) {
|
|
140
|
-
files.add(path.normalize(path.resolve(cwd, f)));
|
|
191
|
+
const discover = (patterns, ignore, keep) => {
|
|
192
|
+
for (const pattern of patterns) {
|
|
193
|
+
const matched = globSync(pattern, {onlyFiles: true, ignore, cwd});
|
|
194
|
+
for (const f of matched) {
|
|
195
|
+
if (keep(f)) files.add(path.normalize(path.resolve(cwd, f)));
|
|
196
|
+
}
|
|
141
197
|
}
|
|
142
|
-
}
|
|
198
|
+
};
|
|
199
|
+
const include = [appInclude, ...library.include, ...extraInclude];
|
|
200
|
+
discover([appInclude], excludeWithDefaults, () => true);
|
|
201
|
+
discover(
|
|
202
|
+
library.include,
|
|
203
|
+
[...excludeWithDefaults, ...library.ignore],
|
|
204
|
+
f => LIBRARY_SOURCE.test(f),
|
|
205
|
+
);
|
|
206
|
+
discover(extraInclude, excludeWithDefaults, () => true);
|
|
143
207
|
|
|
144
208
|
// Watch dependencies
|
|
145
209
|
for (const pattern of include) {
|
package/src/next.js
CHANGED
|
@@ -19,6 +19,8 @@ const path = require('path');
|
|
|
19
19
|
|
|
20
20
|
const ASTRYX_MODULE = /[\\/]node_modules[\\/]@astryxdesign[\\/]/;
|
|
21
21
|
|
|
22
|
+
const escapeRegExp = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
23
|
+
|
|
22
24
|
/**
|
|
23
25
|
* Every alias this helper installs lives in `nextConfig.webpack`, and Turbopack
|
|
24
26
|
* never calls that hook. So `withAstryx()` under Turbopack reinstates exactly
|
|
@@ -271,8 +273,24 @@ function withAstryx(nextConfig = {}) {
|
|
|
271
273
|
// raw TS. This is robust to new third-party `source`-shipping deps.
|
|
272
274
|
config.module = config.module || {};
|
|
273
275
|
config.module.rules = config.module.rules || [];
|
|
276
|
+
// `Rule.test` matches the module being processed, by the path the
|
|
277
|
+
// resolver reports, and webpack reports a symlinked package by its real
|
|
278
|
+
// path. Under pnpm's isolated layout that path still runs through
|
|
279
|
+
// `node_modules/@astryxdesign/`, but a workspace link's does not
|
|
280
|
+
// (`packages/core/...`), so the rule also matches each installed
|
|
281
|
+
// package's real directory.
|
|
282
|
+
const from = (context && context.dir) || process.cwd();
|
|
283
|
+
const realPackageDirs = astryxPackages
|
|
284
|
+
.map(name => findPackageDir(name, from))
|
|
285
|
+
.filter(dir => dir != null)
|
|
286
|
+
.map(dir => escapeRegExp(fs.realpathSync(dir) + path.sep));
|
|
274
287
|
config.module.rules.unshift({
|
|
275
|
-
test:
|
|
288
|
+
test:
|
|
289
|
+
realPackageDirs.length === 0
|
|
290
|
+
? ASTRYX_MODULE
|
|
291
|
+
: new RegExp(
|
|
292
|
+
`${ASTRYX_MODULE.source}|^(?:${realPackageDirs.join('|')})`,
|
|
293
|
+
),
|
|
276
294
|
resolve: {
|
|
277
295
|
conditionNames: ['source', '...'],
|
|
278
296
|
},
|
|
@@ -290,13 +308,11 @@ function withAstryx(nextConfig = {}) {
|
|
|
290
308
|
// third-party `source` shippers such as `lexical`.
|
|
291
309
|
config.resolve = config.resolve || {};
|
|
292
310
|
|
|
293
|
-
//
|
|
294
|
-
//
|
|
295
|
-
//
|
|
296
|
-
// the
|
|
297
|
-
//
|
|
298
|
-
// breaking subpath imports like '@astryxdesign/core/AlertDialog'.
|
|
299
|
-
config.resolve.symlinks = false;
|
|
311
|
+
// Leave `resolve.symlinks` at webpack's default (true). Under pnpm's
|
|
312
|
+
// isolated layout a package's own dependencies (`intl-messageformat`
|
|
313
|
+
// for Core, `lucide-react` for the neutral theme) sit beside its real
|
|
314
|
+
// path, not under the app's `node_modules`, so resolving from the
|
|
315
|
+
// symlink path cannot find them.
|
|
300
316
|
|
|
301
317
|
// Call user's webpack config if provided
|
|
302
318
|
const merged = existingWebpack
|
package/src/next.test.mjs
CHANGED
|
@@ -635,3 +635,119 @@ describe('withAstryx alias coverage', () => {
|
|
|
635
635
|
}
|
|
636
636
|
});
|
|
637
637
|
});
|
|
638
|
+
|
|
639
|
+
describe('withAstryx under symlinked installs', () => {
|
|
640
|
+
let root;
|
|
641
|
+
|
|
642
|
+
/** Write files under `dir`, creating directories as needed. */
|
|
643
|
+
function writeFiles(dir, files) {
|
|
644
|
+
for (const [file, contents] of Object.entries(files)) {
|
|
645
|
+
fs.mkdirSync(path.dirname(path.join(dir, file)), {recursive: true});
|
|
646
|
+
fs.writeFileSync(path.join(dir, file), contents);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/** A minimal `@astryxdesign/core` whose source imports a dependency. */
|
|
651
|
+
const coreFiles = {
|
|
652
|
+
'package.json': JSON.stringify({
|
|
653
|
+
name: '@astryxdesign/core',
|
|
654
|
+
exports: {'.': {source: './src/index.ts', default: './dist/index.js'}},
|
|
655
|
+
}),
|
|
656
|
+
'src/index.ts': "import 'intl-messageformat';\n",
|
|
657
|
+
'dist/index.js': '',
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
/** Link `target` at `link`, the way pnpm links a package. */
|
|
661
|
+
function link(target, linkPath) {
|
|
662
|
+
fs.mkdirSync(path.dirname(linkPath), {recursive: true});
|
|
663
|
+
fs.symlinkSync(target, linkPath, 'junction');
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** Resolve `request` from `dir` with the resolver options webpack gets. */
|
|
667
|
+
function resolveWith(resolve, dir, request) {
|
|
668
|
+
const resolver = ResolverFactory.createResolver({
|
|
669
|
+
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
670
|
+
extensions: ['.ts', '.tsx', '.js'],
|
|
671
|
+
conditionNames: ['import', 'default'],
|
|
672
|
+
alias: resolve.alias,
|
|
673
|
+
...('symlinks' in resolve ? {symlinks: resolve.symlinks} : {}),
|
|
674
|
+
useSyncFileSystemCalls: true,
|
|
675
|
+
});
|
|
676
|
+
return resolver.resolveSync({}, dir, request);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
beforeAll(() => {
|
|
680
|
+
root = fs.realpathSync(
|
|
681
|
+
fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-next-links-')),
|
|
682
|
+
);
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
afterAll(() => {
|
|
686
|
+
fs.rmSync(root, {recursive: true, force: true});
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
it("resolves a package's own dependencies under pnpm's isolated layout", () => {
|
|
690
|
+
// pnpm keeps each package and its dependencies side by side in its store,
|
|
691
|
+
// and links only the app's direct dependencies into `node_modules`.
|
|
692
|
+
const app = path.join(root, 'isolated');
|
|
693
|
+
const store = path.join(
|
|
694
|
+
app,
|
|
695
|
+
'node_modules/.pnpm/@astryxdesign+core@1.0.0/node_modules',
|
|
696
|
+
);
|
|
697
|
+
writeFiles(path.join(store, '@astryxdesign/core'), coreFiles);
|
|
698
|
+
writeFiles(path.join(store, 'intl-messageformat'), {
|
|
699
|
+
'package.json': JSON.stringify({name: 'intl-messageformat'}),
|
|
700
|
+
'index.js': '',
|
|
701
|
+
});
|
|
702
|
+
link(
|
|
703
|
+
path.join(store, '@astryxdesign/core'),
|
|
704
|
+
path.join(app, 'node_modules/@astryxdesign/core'),
|
|
705
|
+
);
|
|
706
|
+
fs.mkdirSync(path.join(app, 'src'));
|
|
707
|
+
|
|
708
|
+
const {resolve} = withAstryx({}).webpack({resolve: {}}, {dir: app});
|
|
709
|
+
const entry = resolveWith(
|
|
710
|
+
resolve,
|
|
711
|
+
path.join(app, 'src'),
|
|
712
|
+
'@astryxdesign/core',
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
expect(
|
|
716
|
+
resolveWith(resolve, path.dirname(entry), 'intl-messageformat'),
|
|
717
|
+
).toBe(path.join(store, 'intl-messageformat/index.js'));
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
it("scopes the source condition to a workspace link's real path", () => {
|
|
721
|
+
// A workspace package links from outside any `node_modules`, so its real
|
|
722
|
+
// path has no `node_modules/@astryxdesign/` segment.
|
|
723
|
+
const workspace = path.join(root, 'workspace');
|
|
724
|
+
const app = path.join(workspace, 'apps/site');
|
|
725
|
+
writeFiles(path.join(workspace, 'packages/core'), coreFiles);
|
|
726
|
+
link(
|
|
727
|
+
path.join(workspace, 'packages/core'),
|
|
728
|
+
path.join(app, 'node_modules/@astryxdesign/core'),
|
|
729
|
+
);
|
|
730
|
+
|
|
731
|
+
const config = withAstryx({}).webpack({resolve: {}}, {dir: app});
|
|
732
|
+
const [rule] = config.module.rules;
|
|
733
|
+
// Webpack's condition semantics: a RegExp tests the path, a string
|
|
734
|
+
// matches it as a prefix.
|
|
735
|
+
const matches = file =>
|
|
736
|
+
[rule.test]
|
|
737
|
+
.flat()
|
|
738
|
+
.some(condition =>
|
|
739
|
+
typeof condition === 'string'
|
|
740
|
+
? file.startsWith(condition)
|
|
741
|
+
: condition.test(file),
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
expect(rule.resolve.conditionNames).toEqual(['source', '...']);
|
|
745
|
+
expect(matches(path.join(workspace, 'packages/core/src/index.ts'))).toBe(
|
|
746
|
+
true,
|
|
747
|
+
);
|
|
748
|
+
expect(matches(path.join(workspace, 'packages/core-x/src/index.ts'))).toBe(
|
|
749
|
+
false,
|
|
750
|
+
);
|
|
751
|
+
expect(matches(path.join(app, 'src/page.tsx'))).toBe(false);
|
|
752
|
+
});
|
|
753
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file postcss.test.mjs
|
|
5
|
+
* @input The documented `postcss(rootDir)` config and its PostCSS plugin, run
|
|
6
|
+
* from an app directory that carries its own root `babel.config.js`
|
|
7
|
+
* @output Proves the plugin compiles Astryx library source without the app's
|
|
8
|
+
* Babel config, while the app's own files still get it
|
|
9
|
+
* @position Regression test for @astryxdesign/build's PostCSS plugin
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
|
|
13
|
+
import * as fs from 'node:fs';
|
|
14
|
+
import * as os from 'node:os';
|
|
15
|
+
import * as path from 'node:path';
|
|
16
|
+
import {createRequire} from 'node:module';
|
|
17
|
+
|
|
18
|
+
const require = createRequire(import.meta.url);
|
|
19
|
+
const {postcss} = require('./config.js');
|
|
20
|
+
|
|
21
|
+
const LIBRARY_FILE = 'node_modules/@astryxdesign/core/src/Box/Box.tsx';
|
|
22
|
+
const PRODUCT_FILE = 'src/page.tsx';
|
|
23
|
+
|
|
24
|
+
let appDir;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An app laid out like the documented Next.js setup: a root Babel config (here
|
|
28
|
+
* one that records every file it is applied to), StyleX's Babel plugin
|
|
29
|
+
* installed, one Astryx library module and one app module.
|
|
30
|
+
*/
|
|
31
|
+
beforeAll(() => {
|
|
32
|
+
appDir = fs.realpathSync(
|
|
33
|
+
fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-postcss-')),
|
|
34
|
+
);
|
|
35
|
+
const files = {
|
|
36
|
+
'babel.config.js': [
|
|
37
|
+
'module.exports = {',
|
|
38
|
+
' plugins: [',
|
|
39
|
+
' () => ({',
|
|
40
|
+
' visitor: {',
|
|
41
|
+
' Program(_, state) {',
|
|
42
|
+
' globalThis.appBabelConfigFiles.push(state.filename);',
|
|
43
|
+
' },',
|
|
44
|
+
' },',
|
|
45
|
+
' }),',
|
|
46
|
+
' ],',
|
|
47
|
+
'};',
|
|
48
|
+
'',
|
|
49
|
+
].join('\n'),
|
|
50
|
+
// An installed Astryx package has a manifest; the plugin scans what each
|
|
51
|
+
// one publishes, so without it the module would count as app code.
|
|
52
|
+
'node_modules/@astryxdesign/core/package.json': JSON.stringify({
|
|
53
|
+
name: '@astryxdesign/core',
|
|
54
|
+
}),
|
|
55
|
+
[LIBRARY_FILE]: [
|
|
56
|
+
"import * as stylex from '@stylexjs/stylex';",
|
|
57
|
+
"export const styles = stylex.create({box: {color: 'red'}});",
|
|
58
|
+
'',
|
|
59
|
+
].join('\n'),
|
|
60
|
+
[PRODUCT_FILE]: [
|
|
61
|
+
"import * as stylex from '@stylexjs/stylex';",
|
|
62
|
+
"export const styles = stylex.create({page: {color: 'blue'}});",
|
|
63
|
+
'',
|
|
64
|
+
].join('\n'),
|
|
65
|
+
};
|
|
66
|
+
for (const [file, contents] of Object.entries(files)) {
|
|
67
|
+
fs.mkdirSync(path.dirname(path.join(appDir, file)), {recursive: true});
|
|
68
|
+
fs.writeFileSync(path.join(appDir, file), contents);
|
|
69
|
+
}
|
|
70
|
+
// The documented config names `@stylexjs/babel-plugin`, which Babel resolves
|
|
71
|
+
// from the app, so install it there the way an app's devDependency would be.
|
|
72
|
+
const stylexPluginDir = path.dirname(
|
|
73
|
+
require.resolve('@stylexjs/babel-plugin/package.json'),
|
|
74
|
+
);
|
|
75
|
+
const installed = path.join(appDir, 'node_modules/@stylexjs/babel-plugin');
|
|
76
|
+
fs.mkdirSync(path.dirname(installed), {recursive: true});
|
|
77
|
+
fs.symlinkSync(fs.realpathSync(stylexPluginDir), installed, 'junction');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
afterAll(() => {
|
|
81
|
+
fs.rmSync(appDir, {recursive: true, force: true});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Run the documented config's StyleX plugin over the app the way `next build`
|
|
86
|
+
* does: from the app directory, on a stylesheet holding `@stylex;`. Returns the
|
|
87
|
+
* CSS that replaces the at-rule.
|
|
88
|
+
*/
|
|
89
|
+
async function compileApp() {
|
|
90
|
+
const config = postcss(appDir);
|
|
91
|
+
const options = config.plugins[require.resolve('./index.js')];
|
|
92
|
+
const [compile] = require('./index.js')(options).plugins;
|
|
93
|
+
let css = null;
|
|
94
|
+
const atRule = {
|
|
95
|
+
name: 'stylex',
|
|
96
|
+
params: '',
|
|
97
|
+
replaceWith: replacement => {
|
|
98
|
+
css = replacement;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
const root = {walkAtRules: visit => visit(atRule)};
|
|
102
|
+
const result = {
|
|
103
|
+
opts: {from: path.join(appDir, 'src/globals.css')},
|
|
104
|
+
messages: [],
|
|
105
|
+
};
|
|
106
|
+
const cwd = process.cwd();
|
|
107
|
+
process.chdir(appDir);
|
|
108
|
+
try {
|
|
109
|
+
await compile(root, result);
|
|
110
|
+
} finally {
|
|
111
|
+
process.chdir(cwd);
|
|
112
|
+
}
|
|
113
|
+
return css;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
describe('the PostCSS plugin and the app Babel config', () => {
|
|
117
|
+
it("compiles Astryx source without the app's Babel config", async () => {
|
|
118
|
+
globalThis.appBabelConfigFiles = [];
|
|
119
|
+
try {
|
|
120
|
+
const css = await compileApp();
|
|
121
|
+
|
|
122
|
+
expect(globalThis.appBabelConfigFiles).not.toContain(
|
|
123
|
+
path.join(appDir, LIBRARY_FILE),
|
|
124
|
+
);
|
|
125
|
+
// The app's own files still get the app's config.
|
|
126
|
+
expect(globalThis.appBabelConfigFiles).toContain(
|
|
127
|
+
path.join(appDir, PRODUCT_FILE),
|
|
128
|
+
);
|
|
129
|
+
expect(css).toMatch(/@layer astryx-base \{[^]*color:\s*red/);
|
|
130
|
+
expect(css).toMatch(/@layer product \{[^]*color:\s*blue/);
|
|
131
|
+
} finally {
|
|
132
|
+
delete globalThis.appBabelConfigFiles;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file postcss.workspace-links.test.mjs
|
|
5
|
+
* @input The PostCSS plugin, run from apps whose Astryx packages are linked
|
|
6
|
+
* from a workspace where package links form cycles
|
|
7
|
+
* @output Proves the plugin compiles what each installed package publishes,
|
|
8
|
+
* once, never walks a package's own `node_modules`, and refuses `files`
|
|
9
|
+
* entries it cannot read faithfully
|
|
10
|
+
* @position Regression test for @astryxdesign/build's PostCSS library scan
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as os from 'node:os';
|
|
16
|
+
import * as path from 'node:path';
|
|
17
|
+
import {createRequire} from 'node:module';
|
|
18
|
+
|
|
19
|
+
const require = createRequire(import.meta.url);
|
|
20
|
+
const astryxPostcss = require('./index.js');
|
|
21
|
+
|
|
22
|
+
let root;
|
|
23
|
+
|
|
24
|
+
const stylexModule = color =>
|
|
25
|
+
[
|
|
26
|
+
"import * as stylex from '@stylexjs/stylex';",
|
|
27
|
+
`export const styles = stylex.create({root: {color: '${color}'}});`,
|
|
28
|
+
'',
|
|
29
|
+
].join('\n');
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A pnpm-style workspace. Core publishes `src` and also carries a test fixture;
|
|
33
|
+
* its dev dependency, the CLI, links back to it. The theme declares no `files`,
|
|
34
|
+
* and its own dependency links to core. Two more list a glob and a `!` entry.
|
|
35
|
+
*/
|
|
36
|
+
beforeAll(() => {
|
|
37
|
+
root = fs.realpathSync(
|
|
38
|
+
fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-postcss-links-')),
|
|
39
|
+
);
|
|
40
|
+
const manifest = (name, files) =>
|
|
41
|
+
JSON.stringify(files ? {name, files} : {name});
|
|
42
|
+
const files = {
|
|
43
|
+
'packages/core/package.json': manifest('@astryxdesign/core', [
|
|
44
|
+
'src',
|
|
45
|
+
'tokens.ts',
|
|
46
|
+
]),
|
|
47
|
+
'packages/core/src/Box/Box.tsx': stylexModule('red'),
|
|
48
|
+
'packages/core/tokens.ts': stylexModule('yellow'),
|
|
49
|
+
'packages/core/__fixtures__/invalid/main.ts': stylexModule('green'),
|
|
50
|
+
'packages/cli/package.json': manifest('@astryxdesign/cli', ['src']),
|
|
51
|
+
'packages/cli/src/command.ts': stylexModule('blue'),
|
|
52
|
+
'packages/theme/package.json': manifest('@astryxdesign/theme'),
|
|
53
|
+
'packages/theme/src/theme.ts': stylexModule('purple'),
|
|
54
|
+
'packages/globbed/package.json': manifest('@astryxdesign/globbed', [
|
|
55
|
+
'src/*',
|
|
56
|
+
]),
|
|
57
|
+
'packages/globbed/src/index.ts': stylexModule('orange'),
|
|
58
|
+
'packages/negated/package.json': manifest('@astryxdesign/negated', [
|
|
59
|
+
'src',
|
|
60
|
+
'!src/internal',
|
|
61
|
+
]),
|
|
62
|
+
'packages/negated/src/index.ts': stylexModule('teal'),
|
|
63
|
+
'apps/site/src/page.tsx': '',
|
|
64
|
+
'apps/themed/src/page.tsx': '',
|
|
65
|
+
'apps/globbed/src/page.tsx': '',
|
|
66
|
+
'apps/negated/src/page.tsx': '',
|
|
67
|
+
};
|
|
68
|
+
for (const [file, contents] of Object.entries(files)) {
|
|
69
|
+
fs.mkdirSync(path.dirname(path.join(root, file)), {recursive: true});
|
|
70
|
+
fs.writeFileSync(path.join(root, file), contents);
|
|
71
|
+
}
|
|
72
|
+
const links = {
|
|
73
|
+
'apps/site/node_modules/@astryxdesign/core': 'packages/core',
|
|
74
|
+
'apps/themed/node_modules/@astryxdesign/theme': 'packages/theme',
|
|
75
|
+
'apps/globbed/node_modules/@astryxdesign/globbed': 'packages/globbed',
|
|
76
|
+
'apps/negated/node_modules/@astryxdesign/negated': 'packages/negated',
|
|
77
|
+
'packages/core/node_modules/@astryxdesign/cli': 'packages/cli',
|
|
78
|
+
'packages/cli/node_modules/@astryxdesign/core': 'packages/core',
|
|
79
|
+
'packages/theme/node_modules/@astryxdesign/core': 'packages/core',
|
|
80
|
+
};
|
|
81
|
+
for (const [link, target] of Object.entries(links)) {
|
|
82
|
+
fs.mkdirSync(path.dirname(path.join(root, link)), {recursive: true});
|
|
83
|
+
fs.symlinkSync(path.join(root, target), path.join(root, link), 'junction');
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
afterAll(() => {
|
|
88
|
+
fs.rmSync(root, {recursive: true, force: true});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Run the plugin from `app` over a stylesheet holding `@stylex;`, the way
|
|
93
|
+
* PostCSS does. Returns the replacement CSS and every file it compiled.
|
|
94
|
+
*/
|
|
95
|
+
async function compileApp(app) {
|
|
96
|
+
const compiled = [];
|
|
97
|
+
const recordFile = () => ({
|
|
98
|
+
visitor: {
|
|
99
|
+
Program(_, state) {
|
|
100
|
+
compiled.push(path.relative(app, state.filename));
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
const [compile] = astryxPostcss({
|
|
105
|
+
cwd: app,
|
|
106
|
+
babelPlugins: [
|
|
107
|
+
[
|
|
108
|
+
require.resolve('@stylexjs/babel-plugin'),
|
|
109
|
+
{
|
|
110
|
+
dev: false,
|
|
111
|
+
runtimeInjection: false,
|
|
112
|
+
unstable_moduleResolution: {type: 'commonJS'},
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
recordFile,
|
|
116
|
+
],
|
|
117
|
+
}).plugins;
|
|
118
|
+
let css = null;
|
|
119
|
+
const atRule = {
|
|
120
|
+
name: 'stylex',
|
|
121
|
+
params: '',
|
|
122
|
+
replaceWith: replacement => {
|
|
123
|
+
css = replacement;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
await compile(
|
|
127
|
+
{walkAtRules: visit => visit(atRule)},
|
|
128
|
+
{opts: {from: path.join(app, 'src/globals.css')}, messages: []},
|
|
129
|
+
);
|
|
130
|
+
return {css, compiled};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
describe('the PostCSS plugin and workspace links', () => {
|
|
134
|
+
it('compiles what each linked package publishes, once', async () => {
|
|
135
|
+
const {css, compiled} = await compileApp(path.join(root, 'apps/site'));
|
|
136
|
+
|
|
137
|
+
expect([...compiled].sort()).toEqual([
|
|
138
|
+
path.join('node_modules/@astryxdesign/core/src/Box/Box.tsx'),
|
|
139
|
+
path.join('node_modules/@astryxdesign/core/tokens.ts'),
|
|
140
|
+
]);
|
|
141
|
+
expect(css).toMatch(/@layer astryx-base \{[^]*color:\s*red/);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('scans a package without `files` whole, minus its node_modules', async () => {
|
|
145
|
+
const {compiled} = await compileApp(path.join(root, 'apps/themed'));
|
|
146
|
+
|
|
147
|
+
expect(compiled).toEqual([
|
|
148
|
+
path.join('node_modules/@astryxdesign/theme/src/theme.ts'),
|
|
149
|
+
]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it.each([
|
|
153
|
+
['globbed', 'src/*'],
|
|
154
|
+
['negated', '!src/internal'],
|
|
155
|
+
])(
|
|
156
|
+
'refuses a `files` entry it cannot read faithfully: %s',
|
|
157
|
+
async (name, entry) => {
|
|
158
|
+
await expect(compileApp(path.join(root, `apps/${name}`))).rejects.toThrow(
|
|
159
|
+
`@astryxdesign/build reads plain files entries only; @astryxdesign/${name} lists "${entry}"`,
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
});
|