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