@symbiote-native/css-parser 0.3.0 → 0.4.0
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 +19 -21
- package/build/generate-dts/index.js +3 -8
- package/build/generate-dts-cli.js +5 -1
- package/build/golden-corpus/fixtures/ScopedGlobalDemo.svelte +25 -0
- package/build/golden-corpus/fixtures/ScopedGlobalDemo.svelte.d.ts +3 -0
- package/build/index.d.ts +8 -7
- package/build/index.js +4 -4
- package/build/lightning/declarations.d.ts +46 -0
- package/build/lightning/declarations.js +885 -0
- package/build/lightning/rules.d.ts +52 -0
- package/build/lightning/rules.js +157 -0
- package/build/lightning/selectors.d.ts +21 -0
- package/build/lightning/selectors.js +372 -0
- package/build/metro-css-module/index.d.ts +30 -2
- package/build/metro-css-module/index.js +142 -36
- package/build/preprocessors/index.js +4 -3
- package/build/properties.d.ts +5 -6
- package/build/properties.js +26 -14
- package/build/scoped-classes.d.ts +23 -0
- package/build/scoped-classes.js +35 -0
- package/build/values.d.ts +3 -7
- package/build/values.js +3 -60
- package/package.json +2 -3
- package/typescript-plugin.cjs +21 -14
- package/build/global-selectors.d.ts +0 -25
- package/build/global-selectors.js +0 -98
- package/build/parser/index.d.ts +0 -59
- package/build/parser/index.js +0 -338
package/README.md
CHANGED
|
@@ -70,20 +70,19 @@ import './theme.css'; // plain CSS — registers classes globally, no export
|
|
|
70
70
|
│ (build time, Metro) │ (runtime, all adapters)
|
|
71
71
|
▼ ▼
|
|
72
72
|
@symbiote-native/css-parser @symbiote-native/engine's style-registry
|
|
73
|
-
preprocessors
|
|
73
|
+
preprocessors → lightning (compileCssToRules) registerRules() / resolveClassName()
|
|
74
74
|
```
|
|
75
75
|
|
|
76
76
|
A preprocessor source is reduced to plain CSS text first (`compileScss`/`compileSass`/
|
|
77
|
-
`compileLess`/`compileStylus`); `
|
|
78
|
-
mechanism below runs identically regardless of source language.
|
|
77
|
+
`compileLess`/`compileStylus`); `compileCssToRules()` is the single downstream consumer either way,
|
|
78
|
+
so every mechanism below runs identically regardless of source language.
|
|
79
79
|
|
|
80
80
|
## API surface
|
|
81
81
|
|
|
82
82
|
```ts
|
|
83
83
|
import {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
kebabToCamel, // core compiler
|
|
84
|
+
compileCssToRules, // core compiler
|
|
85
|
+
compileScopedCss, // a scoped <style> block, rules + name map
|
|
87
86
|
compileCssFile,
|
|
88
87
|
isCssModuleFile, // standalone .css/.module.css files
|
|
89
88
|
createCssMetroTransformer, // Metro babelTransformerPath factory
|
|
@@ -96,29 +95,28 @@ import {
|
|
|
96
95
|
isStyleFile,
|
|
97
96
|
classNamesToDtsSource,
|
|
98
97
|
generateModuleDts, // .d.ts generation for CSS Modules typing
|
|
99
|
-
globalClassNamesIn,
|
|
100
|
-
globalClassTokensIn,
|
|
101
98
|
hashFilePath,
|
|
102
99
|
} from '@symbiote-native/css-parser';
|
|
103
100
|
```
|
|
104
101
|
|
|
105
|
-
- **`
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
- **`compileCssToRules(css, { filename, pattern?, remToPx? })`** — the compiler core: one
|
|
103
|
+
lightningcss pass over the typed AST, resolving `var()`/`calc()` and emitting a `rules` array.
|
|
104
|
+
Each rule carries the class TOKENS its selector was written with — as authored, never camelCased
|
|
105
|
+
or collapsed into a single key — plus specificity and source order, so the registry matches by
|
|
106
|
+
token subset instead of reversing a guess. With a CSS-Modules `pattern` it also returns the
|
|
107
|
+
authored→renamed `exports` map and `globals`, the tokens lightningcss did NOT rename, which is
|
|
108
|
+
exactly the set the author put inside `:global(...)`. A selector containing a pseudo-class
|
|
109
|
+
(`:hover`, …) is dropped whole — RN has no pseudo-class concept, so there is no
|
|
110
|
+
partial-application semantics to preserve.
|
|
111
|
+
- **`compileScopedCss(css, { filename, pattern })`** — the scoped-block form (a Svelte `<style>`, a
|
|
112
|
+
Vue `<style scoped>`): the same rules plus the authored→scoped name map its markup rewriter
|
|
113
|
+
resolves every class token through. Both halves come out of ONE compile, so the style side and
|
|
114
|
+
the markup side cannot disagree on a name.
|
|
110
115
|
- **`compileCssFile` / `isCssModuleFile`** — the standalone-file form: `Card.module.css`'s classes
|
|
111
116
|
are always scoped to a per-file hash and its default export is the name→scopedName map; a plain
|
|
112
117
|
`.css` file registers globally via a side-effect import.
|
|
113
118
|
- **`createCssMetroTransformer`** — wraps an upstream RN Babel transformer, detecting a stylesheet
|
|
114
119
|
extension and compiling it before delegating everything else unchanged.
|
|
115
|
-
- **`globalClassNamesIn` / `globalClassTokensIn`** — the two halves of `:global()`, and they answer
|
|
116
|
-
different questions. The first returns registered KEYS whose selector was global in full, so the
|
|
117
|
-
key itself skips scoping. The second returns MARKUP TOKENS that came out of a `:global(...)`
|
|
118
|
-
payload wherever it sat, including inside an otherwise-scoped selector — `.card :global(.reset)`
|
|
119
|
-
yields the key `cardReset` from neither, and the token `reset` from the second. A caller that
|
|
120
|
-
suffixes class names needs both: exempting only by key leaves a partial global's token
|
|
121
|
-
scope-mangled, exempting only by token leaves a fully global compound's rule dead.
|
|
122
120
|
- **Preprocessors** — `sass`/`less`/`stylus` are lazy, **optional** `devDependencies`: a project
|
|
123
121
|
that never authors `.scss`/`.less`/`.styl` never installs any of the three.
|
|
124
122
|
- **CSS Modules type safety** — `css-dts` (bin) walks a directory and writes a real `<file>.d.ts`
|
|
@@ -143,7 +141,7 @@ import {
|
|
|
143
141
|
|
|
144
142
|
## Related packages
|
|
145
143
|
|
|
146
|
-
- [`@symbiote-native/engine`](../engine) — owns the runtime `style-registry` (`
|
|
144
|
+
- [`@symbiote-native/engine`](../engine) — owns the runtime `style-registry` (`registerRules` /
|
|
147
145
|
`resolveClassName`) this package's compiled output resolves against, and the class+style merge
|
|
148
146
|
used by every adapter.
|
|
149
147
|
- [`@symbiote-native/react`](../../adapters/react) / [`@symbiote-native/vue`](../../adapters/vue) /
|
|
@@ -8,9 +8,7 @@
|
|
|
8
8
|
// appended rather than replaced — e.g. `Card.module.css.d.ts`) so TypeScript's own module
|
|
9
9
|
// resolution picks it up for a default import of `Card.module.css` without a separate
|
|
10
10
|
// registration step, the same convention typed-css-modules uses.
|
|
11
|
-
import {
|
|
12
|
-
import { compile, detectLanguage } from "../preprocessors/index.js";
|
|
13
|
-
import { isCssModuleFile } from "../metro-css-module/index.js";
|
|
11
|
+
import { isCssModuleFile, moduleClassNames, } from "../metro-css-module/index.js";
|
|
14
12
|
const IDENTIFIER_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
15
13
|
function formatKey(className) {
|
|
16
14
|
return IDENTIFIER_RE.test(className) ? className : JSON.stringify(className);
|
|
@@ -31,13 +29,10 @@ export function classNamesToDtsSource(classNames) {
|
|
|
31
29
|
].join('\n');
|
|
32
30
|
}
|
|
33
31
|
// Mirrors compileCssFile's own module/non-module branch: a plain (non-`.module.*`) style file
|
|
34
|
-
// has no default export to type (
|
|
32
|
+
// has no default export to type (registerRules() runs as a side effect only), so it gets no
|
|
35
33
|
// `.d.ts` at all.
|
|
36
34
|
export async function generateModuleDts(source, filename) {
|
|
37
35
|
if (!isCssModuleFile(filename))
|
|
38
36
|
return null;
|
|
39
|
-
|
|
40
|
-
const css = lang === 'css' ? source : await compile(source, lang, filename);
|
|
41
|
-
const parsed = parseCSS(css, { filename });
|
|
42
|
-
return classNamesToDtsSource(Object.keys(parsed));
|
|
37
|
+
return classNamesToDtsSource(await moduleClassNames(source, filename));
|
|
43
38
|
}
|
|
@@ -17,7 +17,11 @@ import * as path from 'node:path';
|
|
|
17
17
|
import { isStyleFile } from "./preprocessors/index.js";
|
|
18
18
|
import { isCssModuleFile } from "./metro-css-module/index.js";
|
|
19
19
|
import { generateModuleDts } from "./generate-dts/index.js";
|
|
20
|
-
const SKIPPED_DIR_NAMES = new Set([
|
|
20
|
+
const SKIPPED_DIR_NAMES = new Set([
|
|
21
|
+
'node_modules',
|
|
22
|
+
'build',
|
|
23
|
+
'.git',
|
|
24
|
+
]);
|
|
21
25
|
async function collectModuleStyleFiles(root) {
|
|
22
26
|
const stat = await fs.stat(root);
|
|
23
27
|
if (!stat.isDirectory()) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Fixture, not a running component. examples/svelte has exactly one styled component and it
|
|
3
|
+
// carries no :global(), so the escape hatch had no golden coverage on this adapter either.
|
|
4
|
+
let isLoud = $state(false);
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<View class="panel">
|
|
8
|
+
<View class="panel wide" />
|
|
9
|
+
<View class={['panel', isLoud && 'wide']} />
|
|
10
|
+
<View class="untouched" />
|
|
11
|
+
</View>
|
|
12
|
+
|
|
13
|
+
<style>
|
|
14
|
+
.panel {
|
|
15
|
+
padding: 9px;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.panel.wide {
|
|
19
|
+
padding: 19px;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
:global(.untouched) {
|
|
23
|
+
margin: 0;
|
|
24
|
+
}
|
|
25
|
+
</style>
|
package/build/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type {
|
|
3
|
-
export { globalClassNamesIn, globalClassTokensIn } from './global-selectors.ts';
|
|
1
|
+
export { compileCssToRules } from './lightning/rules.ts';
|
|
2
|
+
export type { ICompiledCss, ICompileRulesOptions, IStyleRule, } from './lightning/rules.ts';
|
|
4
3
|
export { hashFilePath } from './file-scope-id.ts';
|
|
5
|
-
export {
|
|
6
|
-
export type {
|
|
7
|
-
export {
|
|
4
|
+
export { compileScopedCss } from './scoped-classes.ts';
|
|
5
|
+
export type { IScopedCss, IScopedCssOptions } from './scoped-classes.ts';
|
|
6
|
+
export { compileCssFile, compileCssModule, isCssModuleFile, } from './metro-css-module/index.ts';
|
|
7
|
+
export type { ICompiledCssFile, ICompiledCssModule, } from './metro-css-module/index.ts';
|
|
8
|
+
export { classNamesToDtsSource, generateModuleDts, } from './generate-dts/index.ts';
|
|
8
9
|
export { createCssMetroTransformer, resolveUpstreamTransformer, } from './metro-transformer/index.ts';
|
|
9
|
-
export type { IMetroTransformer, IMetroTransformParams } from './metro-transformer/index.ts';
|
|
10
|
+
export type { IMetroTransformer, IMetroTransformParams, } from './metro-transformer/index.ts';
|
|
10
11
|
export { compileScss, compileSass, compileLess, compileStylus, compile, detectLanguage, isStyleFile, } from './preprocessors/index.ts';
|
|
11
12
|
export type { IPreprocessorLanguage } from './preprocessors/index.ts';
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { globalClassNamesIn, globalClassTokensIn } from "./global-selectors.js";
|
|
1
|
+
export { compileCssToRules } from "./lightning/rules.js";
|
|
3
2
|
export { hashFilePath } from "./file-scope-id.js";
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
3
|
+
export { compileScopedCss } from "./scoped-classes.js";
|
|
4
|
+
export { compileCssFile, compileCssModule, isCssModuleFile, } from "./metro-css-module/index.js";
|
|
5
|
+
export { classNamesToDtsSource, generateModuleDts, } from "./generate-dts/index.js";
|
|
6
6
|
export { createCssMetroTransformer, resolveUpstreamTransformer, } from "./metro-transformer/index.js";
|
|
7
7
|
export { compileScss, compileSass, compileLess, compileStylus, compile, detectLanguage, isStyleFile, } from "./preprocessors/index.js";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An OBJECT-shaped RN style value. `core/engine/src/styles.ts` has exactly one such field,
|
|
3
|
+
* `shadowOffset?: { width: number; height: number }`; RN's `textShadowOffset` has the same shape
|
|
4
|
+
* (it is missing from `ITextStyle` there, but `textShadowToStyle` below emits it and RN reads
|
|
5
|
+
* it). Both are all-number, so one record type covers the family.
|
|
6
|
+
*/
|
|
7
|
+
export type IStyleValueObject = Readonly<Record<string, number>>;
|
|
8
|
+
/**
|
|
9
|
+
* One React Native style value.
|
|
10
|
+
*
|
|
11
|
+
* Most are scalars. Two are objects ({@link IStyleValueObject}). Six are ARRAY-capable in RN —
|
|
12
|
+
* `transform: ITransformProp[]`, `boxShadow`, `filter`, `experimental_backgroundImage`,
|
|
13
|
+
* `transformOrigin`, `fontVariant` — but every one of them is `raw` in PROPERTY_TABLE, i.e. this
|
|
14
|
+
* pipeline hands the engine the CSS TEXT and lets its own processors (`process-transform`,
|
|
15
|
+
* `process-box-shadow`, …) build the array at commit time. So the array branch exists so the
|
|
16
|
+
* integration step cannot hit a second widening, not because a declaration produces one today.
|
|
17
|
+
*/
|
|
18
|
+
export type IStyleValue = string | number | IStyleValueObject | ReadonlyArray<string | number | IStyleValueObject>;
|
|
19
|
+
export type IStyleObject = Record<string, IStyleValue>;
|
|
20
|
+
export interface IDeclarationContext {
|
|
21
|
+
readonly filename: string;
|
|
22
|
+
/** Custom properties (`--x`) collected from the whole file, name -> raw value text. */
|
|
23
|
+
readonly variables: ReadonlyMap<string, string>;
|
|
24
|
+
/** Root font size for rem, default 16. */
|
|
25
|
+
readonly remToPx?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One lightningcss `Declaration` (the object handed to a visitor / found in
|
|
29
|
+
* `rule.value.declarations.declarations`) mapped to zero or more RN style entries.
|
|
30
|
+
*/
|
|
31
|
+
export declare function declarationToStyle(declaration: unknown, context: IDeclarationContext): IStyleObject;
|
|
32
|
+
/**
|
|
33
|
+
* Every custom property (`--x`) declared anywhere in the file, name -> its raw value text, in the
|
|
34
|
+
* shape {@link IDeclarationContext.variables} expects.
|
|
35
|
+
*
|
|
36
|
+
* A pass of its OWN, ahead of mapping declarations, because nothing orders `:root` first — a
|
|
37
|
+
* component stylesheet may declare a token in a class rule, inside `@media`, or below its first
|
|
38
|
+
* use, and a single forward walk would miss it. Later declaration of the same name wins, which is
|
|
39
|
+
* what the cascade does within one file.
|
|
40
|
+
*
|
|
41
|
+
* The value is stored VERBATIM: a token list that is itself a `var()` chain is printed back as
|
|
42
|
+
* `var(--other)`, and resolution stays in {@link declarationToStyle}, which is the only place that
|
|
43
|
+
* knows whether the chain terminates. Serialization goes through the same token printer the
|
|
44
|
+
* `unparsed` path uses — this package keeps ONE printer.
|
|
45
|
+
*/
|
|
46
|
+
export declare function variablesIn(css: string, filename: string): ReadonlyMap<string, string>;
|