@wix/validation-infra 0.0.0-stage → 0.5.1
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 +192 -2
- package/dist/constants.d.ts +29 -0
- package/dist/constants.js +45 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/package-readers/constants.d.ts +6 -0
- package/dist/package-readers/constants.js +6 -0
- package/dist/package-readers/index.d.ts +1 -0
- package/dist/package-readers/index.js +1 -0
- package/dist/package-readers/read-from-files.d.ts +6 -0
- package/dist/package-readers/read-from-files.js +76 -0
- package/dist/package-readers/types.d.ts +7 -0
- package/dist/package-readers/types.js +1 -0
- package/dist/package-readers/utils.d.ts +4 -0
- package/dist/package-readers/utils.js +15 -0
- package/dist/page-model/build-page-model.d.ts +3 -0
- package/dist/page-model/build-page-model.js +12 -0
- package/dist/page-model/connect-css.d.ts +3 -0
- package/dist/page-model/connect-css.js +43 -0
- package/dist/page-model/constants.d.ts +17 -0
- package/dist/page-model/constants.js +17 -0
- package/dist/page-model/css-selectors.d.ts +2 -0
- package/dist/page-model/css-selectors.js +7 -0
- package/dist/page-model/index.d.ts +4 -0
- package/dist/page-model/index.js +4 -0
- package/dist/page-model/pages.d.ts +2 -0
- package/dist/page-model/pages.js +18 -0
- package/dist/page-model/read-css.d.ts +4 -0
- package/dist/page-model/read-css.js +98 -0
- package/dist/page-model/read-structure.d.ts +3 -0
- package/dist/page-model/read-structure.js +59 -0
- package/dist/page-model/read-theme.d.ts +6 -0
- package/dist/page-model/read-theme.js +14 -0
- package/dist/page-model/types.d.ts +92 -0
- package/dist/page-model/types.js +1 -0
- package/dist/page-model/utils.d.ts +16 -0
- package/dist/page-model/utils.js +29 -0
- package/dist/project-model/index.d.ts +5 -0
- package/dist/project-model/index.js +5 -0
- package/dist/project-model/menu-files.d.ts +4 -0
- package/dist/project-model/menu-files.js +14 -0
- package/dist/project-model/package-json.d.ts +5 -0
- package/dist/project-model/package-json.js +27 -0
- package/dist/project-model/popups.d.ts +4 -0
- package/dist/project-model/popups.js +12 -0
- package/dist/project-model/project-model.d.ts +4 -0
- package/dist/project-model/project-model.js +14 -0
- package/dist/project-model/types.d.ts +20 -0
- package/dist/project-model/types.js +2 -0
- package/dist/rules/index.d.ts +4 -0
- package/dist/rules/index.js +5 -0
- package/dist/rules/project/define-project-rule.d.ts +5 -0
- package/dist/rules/project/define-project-rule.js +4 -0
- package/dist/rules/project/index.d.ts +2 -0
- package/dist/rules/project/index.js +2 -0
- package/dist/rules/project/types.d.ts +16 -0
- package/dist/rules/project/types.js +1 -0
- package/dist/rules/site-app/define-rule.d.ts +5 -0
- package/dist/rules/site-app/define-rule.js +4 -0
- package/dist/rules/site-app/index.d.ts +2 -0
- package/dist/rules/site-app/index.js +1 -0
- package/dist/rules/site-app/types.d.ts +26 -0
- package/dist/rules/site-app/types.js +1 -0
- package/dist/rules/site-code/define-site-code-rule.d.ts +5 -0
- package/dist/rules/site-code/define-site-code-rule.js +4 -0
- package/dist/rules/site-code/index.d.ts +2 -0
- package/dist/rules/site-code/index.js +2 -0
- package/dist/rules/site-code/types.d.ts +11 -0
- package/dist/rules/site-code/types.js +1 -0
- package/dist/rules/types.d.ts +13 -0
- package/dist/rules/types.js +3 -0
- package/dist/rules-adapter/eslint/index.d.ts +1 -0
- package/dist/rules-adapter/eslint/index.js +1 -0
- package/dist/rules-adapter/eslint/types.d.ts +5 -0
- package/dist/rules-adapter/eslint/types.js +1 -0
- package/dist/rules-adapter/index.d.ts +2 -0
- package/dist/rules-adapter/index.js +2 -0
- package/dist/rules-adapter/types.d.ts +16 -0
- package/dist/rules-adapter/types.js +1 -0
- package/dist/rules-adapter/utils.d.ts +7 -0
- package/dist/rules-adapter/utils.js +20 -0
- package/dist/rules-api/component-catalog.d.ts +8 -0
- package/dist/rules-api/component-catalog.js +28 -0
- package/dist/rules-api/constants.d.ts +7 -0
- package/dist/rules-api/constants.js +7 -0
- package/dist/rules-api/index.d.ts +18 -0
- package/dist/rules-api/index.js +14 -0
- package/dist/rules-api/report.d.ts +8 -0
- package/dist/rules-api/report.js +13 -0
- package/dist/rules-api/rules-api.d.ts +13 -0
- package/dist/rules-api/rules-api.js +139 -0
- package/dist/rules-api/types.d.ts +141 -0
- package/dist/rules-api/types.js +1 -0
- package/dist/rules-api/utils.d.ts +14 -0
- package/dist/rules-api/utils.js +27 -0
- package/dist/shared/constants.d.ts +188 -0
- package/dist/shared/constants.js +242 -0
- package/dist/shared/css-variables.d.ts +9 -0
- package/dist/shared/css-variables.js +38 -0
- package/dist/shared/css.d.ts +78 -0
- package/dist/shared/css.js +426 -0
- package/dist/shared/element-match.d.ts +16 -0
- package/dist/shared/element-match.js +72 -0
- package/dist/shared/font-weights.d.ts +2 -0
- package/dist/shared/font-weights.js +551 -0
- package/dist/shared/index.d.ts +16 -0
- package/dist/shared/index.js +15 -0
- package/dist/shared/interactions-config.d.ts +15 -0
- package/dist/shared/interactions-config.js +120 -0
- package/dist/shared/links.d.ts +21 -0
- package/dist/shared/links.js +93 -0
- package/dist/shared/posix-path.d.ts +8 -0
- package/dist/shared/posix-path.js +27 -0
- package/dist/shared/site-files-tree-mapping.d.ts +40 -0
- package/dist/shared/site-files-tree-mapping.js +41 -0
- package/dist/shared/site-folders.d.ts +3 -0
- package/dist/shared/site-folders.js +17 -0
- package/dist/shared/site-media.d.ts +3 -0
- package/dist/shared/site-media.js +11 -0
- package/dist/shared/syntax-tree.d.ts +4 -0
- package/dist/shared/syntax-tree.js +30 -0
- package/dist/shared/transformation-values.d.ts +22 -0
- package/dist/shared/transformation-values.js +74 -0
- package/dist/shared/types.d.ts +477 -0
- package/dist/shared/types.js +1 -0
- package/dist/shared/utils.d.ts +155 -0
- package/dist/shared/utils.js +553 -0
- package/dist/shared/wrapper-contract.d.ts +31 -0
- package/dist/shared/wrapper-contract.js +157 -0
- package/dist/shared/wrapper-image-props.d.ts +3 -0
- package/dist/shared/wrapper-image-props.js +75 -0
- package/dist/site-code-model/constants.d.ts +107 -0
- package/dist/site-code-model/constants.js +102 -0
- package/dist/site-code-model/index.d.ts +3 -0
- package/dist/site-code-model/index.js +4 -0
- package/dist/site-code-model/manifest-values.d.ts +15 -0
- package/dist/site-code-model/manifest-values.js +88 -0
- package/dist/site-code-model/read-module.d.ts +4 -0
- package/dist/site-code-model/read-module.js +267 -0
- package/dist/site-code-model/read-site-code-model.d.ts +9 -0
- package/dist/site-code-model/read-site-code-model.js +42 -0
- package/dist/site-code-model/source-locations.d.ts +5 -0
- package/dist/site-code-model/source-locations.js +95 -0
- package/dist/site-code-model/types.d.ts +102 -0
- package/dist/site-code-model/types.js +1 -0
- package/dist/testing.d.ts +24 -0
- package/dist/testing.js +61 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.js +1 -0
- package/dist/validate.d.ts +7 -0
- package/dist/validate.js +180 -0
- package/package.json +59 -3
package/README.md
CHANGED
|
@@ -1,3 +1,193 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wix/validation-infra
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Write site validation rules in any repo and run them on an in-memory site. Page rules get typed syntax-tree callbacks
|
|
4
|
+
and `rulesApi` (components, CSS, theme and catalog). Project rules get the site's file list and `readFile`. Site-code
|
|
5
|
+
rules get the site code's extensions, through the site-code model, and the same project.
|
|
6
|
+
The engine performs no disk, git or process operations.
|
|
7
|
+
|
|
8
|
+
`validate({ experimentalPopups: true, ... })` passes the caller's popup feature flag to project rules as
|
|
9
|
+
`project.experimentalPopups` (default false). No environment variable is read by this package.
|
|
10
|
+
`rulesApi.isPackageInstalled(name)` checks a package's manifest in `validate()`'s `packageFiles`
|
|
11
|
+
([Package files](#package-files)), cached per page; `<name>/package.json` is found even when the package does not export that path.
|
|
12
|
+
`rulesApi.getImagePaths(node)` reads `<wrapper>/definition.ts` there too, beside the wrapper
|
|
13
|
+
module. It recognizes the image/video declarations upload-media uses, including nested arrays and ref elements.
|
|
14
|
+
`rulesApi.getComponentContract(node)` reads a wrapper's `<wrapper>/component-contract.json`, else `<wrapper>/css`, and
|
|
15
|
+
InternalContainer's `@wix/origami-system-components/InternalContainer/component-contract.json` (a package export), from the
|
|
16
|
+
same files; null when neither resolves.
|
|
17
|
+
`rulesApi.isOutsideWixMedia(uri)` checks remote URLs and site-relative image files in memory.
|
|
18
|
+
`rulesApi.isVariableDefined(name)` tells whether a custom property is defined for the page: theme.css's `:root`, the runtime's
|
|
19
|
+
names, or the page's own CSS and applied mixins (always, with no theme.css). A project rule builds the same check with `createVariableCheck`.
|
|
20
|
+
`rulesApi.getStateTransitions()` reads the page's `servicesConfigs/interactions.ts` through `readFile`; `findPages` lints a page
|
|
21
|
+
when that file changes (not one under `_generated`).
|
|
22
|
+
|
|
23
|
+
## A team's rule package
|
|
24
|
+
|
|
25
|
+
For example, `@wix/lint-dm` depends on `@wix/validation-infra` and exports a single list:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { defineRule, defineProjectRule, type AnyRule } from '@wix/validation-infra'
|
|
29
|
+
|
|
30
|
+
const sectionId = defineRule({
|
|
31
|
+
name: 'dm-section-id',
|
|
32
|
+
checks: ['missing_id'],
|
|
33
|
+
editors: 'all',
|
|
34
|
+
callbacks: {
|
|
35
|
+
JSXElement: ({ node, rulesApi, report }) => {
|
|
36
|
+
if (rulesApi.getType(node) === 'section' && rulesApi.getId(node) === null)
|
|
37
|
+
report({ check: 'missing_id', loc: node.loc, message: 'write a section id' })
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const config = defineProjectRule({
|
|
43
|
+
name: 'dm-config',
|
|
44
|
+
checks: ['missing_config'],
|
|
45
|
+
editors: ['studio2'],
|
|
46
|
+
check: project => project.readFile('dm.json') === undefined
|
|
47
|
+
? [{ check: 'missing_config', file: 'dm.json', message: 'write dm.json' }] : [],
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export { sectionId, config } // a shared rule can take a named rule's name to join it
|
|
51
|
+
export const RULES: readonly AnyRule[] = [sectionId, config]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A rule with the same name and scope as another package's rule is combined with it (see below). `checks` restricts which check keys a rule can report; callback names and nodes
|
|
55
|
+
are typed too. Severity defaults to `error`; use `severity: 'warn'` for advice. An optional `tags` array labels rules.
|
|
56
|
+
Rules may declare the infra as a peer dependency and install it as a dev dependency for authoring and tests.
|
|
57
|
+
Give each source folder an exports-only `index.ts`. Re-export named public rules and needed types through each
|
|
58
|
+
parent index to `src/index.ts`, so another package can import them from the package root.
|
|
59
|
+
Publish compiled JavaScript and declarations with an ESM `exports` entry pointing to them. Run TypeScript and your
|
|
60
|
+
normal source linter in the team's repo; use the testing entry to test the rules' behavior.
|
|
61
|
+
|
|
62
|
+
## The site code
|
|
63
|
+
|
|
64
|
+
A site-code rule checks the site code (`packages/site-code/src`). Its `check` gets the site-code model, ready-made: every
|
|
65
|
+
extension the site registers, its manifest as the Wix CLI builds it, and where each value is written. It also gets the
|
|
66
|
+
project (`files`, `readFile`), as a project rule does, and returns the same problems. Nothing runs: a value the model
|
|
67
|
+
doesn't read is `UNKNOWN`, and a rule checks nothing about it. `validate` reads the model once, only when a site-code rule
|
|
68
|
+
runs, and runs the site-code rules after the project rules, on the whole site (`only` doesn't narrow them).
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { defineSiteCodeRule, UNKNOWN } from '@wix/validation-infra'
|
|
72
|
+
|
|
73
|
+
const extensionTypes = defineSiteCodeRule({
|
|
74
|
+
name: 'velo-extension-types',
|
|
75
|
+
checks: ['duplicate_type'],
|
|
76
|
+
editors: 'all',
|
|
77
|
+
check: ({ extensions }) => {
|
|
78
|
+
if (extensions === UNKNOWN) return []
|
|
79
|
+
return extensions
|
|
80
|
+
.filter((extension, index) => extension.type !== UNKNOWN && extensions.findIndex(({ type }) => type === extension.type) < index)
|
|
81
|
+
.map(extension => ({ check: 'duplicate_type', ...extension.fixAt('type'), message: `give ${String(extension.type)} its own type` }))
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
An `Extension` has its `kind`, `type`, `file` and `manifest`, plus `where(path)` (where the value at a dotted path is
|
|
87
|
+
written) and `fixAt(path)` (where to fix it: a generated value is fixed in the file it is generated from). See
|
|
88
|
+
[docs/parts.md](../../docs/parts.md) ("Site-code model").
|
|
89
|
+
|
|
90
|
+
## Test in the team's repo
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import assert from 'node:assert/strict'
|
|
94
|
+
import { lint, page, checkProject, checkSiteCode } from '@wix/validation-infra/testing'
|
|
95
|
+
|
|
96
|
+
const structure = `import { Section } from '@wix/origami-system-components/Section'
|
|
97
|
+
export const Structure = () => <Section />`
|
|
98
|
+
assert.deepEqual(lint(page(structure), [sectionId]), ['dm-section-id: write a section id (line 2)'])
|
|
99
|
+
assert.deepEqual(checkProject(config, {}), ['dm.json: write dm.json'])
|
|
100
|
+
assert.deepEqual(checkSiteCode(extensionTypes, {}), []) // no site code: nothing to check
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`page(structure, layoutCss?, styleCss?)` makes an in-memory page. Spread `theme(css)` into its file map for theme
|
|
104
|
+
tests. `lintWithEslint(rule, structures, siteFiles?)` returns each structure's reported rule names and lines.
|
|
105
|
+
`TEST_PAGE` and `SITE_MANIFEST` are exported fixtures. Page test helpers throw for parse failures and crashed rules.
|
|
106
|
+
|
|
107
|
+
## Run any rule packages
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { validate } from '@wix/validation-infra'
|
|
111
|
+
import { RULES as DM_RULES } from '@wix/lint-dm'
|
|
112
|
+
import { RULES as VELO_RULES } from '@wix/lint-velo'
|
|
113
|
+
|
|
114
|
+
const problems = validate({
|
|
115
|
+
files, // Record<path from site root, text>, including CSS/theme/catalog the rules need
|
|
116
|
+
rules: [...DM_RULES, ...VELO_RULES],
|
|
117
|
+
editor: 'studio2', // optional: matching rules plus editors: 'all'; omitted runs every rule
|
|
118
|
+
only: [jsxFile], // optional: page files to lint; project and site-code rules still see the whole site
|
|
119
|
+
catalog, // optional: parsed component catalog, or a reader; otherwise parsed from files on first use
|
|
120
|
+
companions, // optional: the site code's companion registrations' kinds, or a reader called on first use
|
|
121
|
+
root, // optional: the site's folder, only as ESLint names a page (crash messages, context.filename)
|
|
122
|
+
fonts, // optional: font id → weights; otherwise the bundled catalog
|
|
123
|
+
onEvent: event => {}, // optional: rule/page timing events
|
|
124
|
+
})
|
|
125
|
+
// → { rule, check, severity, file, line, column, message }[]
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Rules of one scope with one name, from any package, are combined into one rule: page rules run all their callbacks on
|
|
129
|
+
the same nodes, project and site-code rules all their checks. Each part keeps its own `severity` and `editors`, and every problem
|
|
130
|
+
shows the shared name. Rules of two scopes with one name stay two rules. One check key in two parts of a
|
|
131
|
+
rule throws before execution. Project rules run first, then site-code rules (each with a `site_code_rule` event, as a
|
|
132
|
+
project rule has a `project_rule` one), then page rules through ESLint's in-process
|
|
133
|
+
`Linter`, each rule name one ESLint rule `origami/<name>`. A page's problems come in ESLint's order (by line, then column;
|
|
134
|
+
at one place, in the order reported), and a site's `eslint-disable` comments apply, as in origami-lint master. A comment
|
|
135
|
+
that names a rule ESLint doesn't have is a problem under that name with `check: DIRECTIVE`. A structure that cannot parse
|
|
136
|
+
reports `rule: 'parse'`. A crashed rule reports `check: CRASHED` with error severity; `PAGE_RULES` identifies a crash that
|
|
137
|
+
stopped page rules, the page's only problem. The catalog and the companions are read when a rule first asks about a tag
|
|
138
|
+
from an app's wrapper or from the site code: malformed catalog JSON crashes only such a page.
|
|
139
|
+
`validateInFoundOrder` (from `/testing`) gives each page's problems in the order its rules found them, as `lint()` shows them.
|
|
140
|
+
The top-level `Problem` type is a validation result; rule callbacks report through the generic `Report<Check>` type.
|
|
141
|
+
|
|
142
|
+
`@wix/validation-rules` owns shared rules and the combined list and exposes `validate({ files, ... })` with it already selected.
|
|
143
|
+
It is the entry for site consumers such as origami-lint, editor agents and import servers. The infra also exports
|
|
144
|
+
the shared syntax/CSS helpers, model types, site path constants and `posixPath` needed by external rules.
|
|
145
|
+
|
|
146
|
+
The runner uses ESLint and TypeScript; browser bundling is not verified. A server endpoint around `validate()` is
|
|
147
|
+
an option for a browser consumer. The repository's `yarn test:package` checks the npm package boundary without
|
|
148
|
+
publishing. Actual Wix export/import behavior is checked separately with the real-site tooling.
|
|
149
|
+
|
|
150
|
+
## Package files
|
|
151
|
+
|
|
152
|
+
The rules read files of the site's installed packages: a wrapper's contract, definition and package manifest, and
|
|
153
|
+
InternalContainer's contract from `@wix/origami-system-components`. `validate()` reads no disk, so the caller gives
|
|
154
|
+
them as `packageFiles`, next to `files` and in the same shape: each file's text by its path from the site root, as it is on disk.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
validate({
|
|
158
|
+
files,
|
|
159
|
+
rules,
|
|
160
|
+
packageFiles: {
|
|
161
|
+
'node_modules/@origami-wrappers/wixEditorElements/package.json': '{"exports":{"./*":"./*/index.ts","./*/css":"./*/defaults.css"}}',
|
|
162
|
+
'node_modules/@origami-wrappers/wixEditorElements/button/component-contract.json': '…',
|
|
163
|
+
'node_modules/@origami-wrappers/wixEditorElements/button/defaults.css': '…',
|
|
164
|
+
'node_modules/@origami-wrappers/wixEditorElements/button/definition.ts': '…',
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Give every file under each installed `@origami-wrappers/<app>` package and under `@wix/origami-system-components`, from
|
|
170
|
+
every `node_modules` a page can see (the site root's, and `packages/site-app`'s when it has one). A service is sent them
|
|
171
|
+
by its client; a caller on the site's machine reads them first:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
const packageFiles: Record<string, string> = {}
|
|
175
|
+
function readFolder(folder: string) {
|
|
176
|
+
for (const name of readdirSync(path.join(siteRoot, folder))) {
|
|
177
|
+
const file = `${folder}/${name}`
|
|
178
|
+
if (name === 'node_modules') continue
|
|
179
|
+
if (statSync(path.join(siteRoot, file)).isDirectory()) readFolder(file)
|
|
180
|
+
else packageFiles[file] = readFileSync(path.join(siteRoot, file), 'utf8')
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// The site root's node_modules; the same for `packages/site-app/node_modules` when the site has one.
|
|
184
|
+
for (const name of ['@origami-wrappers', '@wix/origami-system-components'])
|
|
185
|
+
if (existsSync(path.join(siteRoot, 'node_modules', name))) readFolder(`node_modules/${name}`)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
`validate()` finds a package in them as node does: the first `node_modules/<name>` above the page with a `package.json`
|
|
189
|
+
(a folder without one, as an interrupted install leaves it, is not installed). A request goes through that
|
|
190
|
+
`package.json`'s `exports`; `<wrapper>/definition.ts` is read beside the module the wrapper resolves to;
|
|
191
|
+
`<name>/package.json` is read even when not exported. A package that is not in `packageFiles` is not installed: without
|
|
192
|
+
them, no package is installed, no tag has a contract, and the image paths a wrapper's definition declares cannot be checked.
|
|
193
|
+
What is missing or does not parse is not found; nothing throws.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Linter } from 'eslint';
|
|
2
|
+
/** The check of the one problem a rule that threw reports, on the site root. */
|
|
3
|
+
export declare const CRASHED = "crashed";
|
|
4
|
+
/** The `rule` of that problem when the page rules as a whole couldn't finish on a page (ESLint stopped). */
|
|
5
|
+
export declare const PAGE_RULES = "page-rules";
|
|
6
|
+
/** The check of a problem ESLint itself finds in a structure file's ESLint comment: `// eslint-disable-next-line react-hooks/exhaustive-deps`
|
|
7
|
+
* names a rule ESLint doesn't have. */
|
|
8
|
+
export declare const DIRECTIVE = "directive";
|
|
9
|
+
/** The ESLint plugin the page rules are: each rule is `origami/<name>`, as a site's `eslint-disable` comment names it. */
|
|
10
|
+
export declare const PLUGIN = "origami";
|
|
11
|
+
/** How ESLint reads a structure file: typescript-eslint's parser reads .jsx and .tsx alike; its tree is the one the rules' types are. */
|
|
12
|
+
export declare const STRUCTURE_FILES: Linter.Config;
|
|
13
|
+
/** The page folder testing.ts's `page()` writes a page to. */
|
|
14
|
+
export declare const TEST_PAGE = "packages/site-app/src/pages/test";
|
|
15
|
+
/** The export's site package, as master's packageJson.ts and the Express adapter write it, with one wrapper, shortened. */
|
|
16
|
+
export declare const SITE_MANIFEST: {
|
|
17
|
+
name: string;
|
|
18
|
+
scripts: {
|
|
19
|
+
dev: string;
|
|
20
|
+
build: string;
|
|
21
|
+
start: string;
|
|
22
|
+
};
|
|
23
|
+
dependencies: Record<string, string | undefined>;
|
|
24
|
+
devDependencies: Record<string, string | undefined>;
|
|
25
|
+
origami: {
|
|
26
|
+
adapter: string;
|
|
27
|
+
layout: number;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// validate()'s fixed values: the problem a run that couldn't finish reports, and how ESLint reads a structure file; and the
|
|
2
|
+
// in-memory site testing.ts builds a rule's tests on.
|
|
3
|
+
import * as parser from '@typescript-eslint/parser';
|
|
4
|
+
/** The check of the one problem a rule that threw reports, on the site root. */
|
|
5
|
+
export const CRASHED = 'crashed';
|
|
6
|
+
/** The `rule` of that problem when the page rules as a whole couldn't finish on a page (ESLint stopped). */
|
|
7
|
+
export const PAGE_RULES = 'page-rules';
|
|
8
|
+
/** The check of a problem ESLint itself finds in a structure file's ESLint comment: `// eslint-disable-next-line react-hooks/exhaustive-deps`
|
|
9
|
+
* names a rule ESLint doesn't have. */
|
|
10
|
+
export const DIRECTIVE = 'directive';
|
|
11
|
+
/** The ESLint plugin the page rules are: each rule is `origami/<name>`, as a site's `eslint-disable` comment names it. */
|
|
12
|
+
export const PLUGIN = 'origami';
|
|
13
|
+
/** How ESLint reads a structure file: typescript-eslint's parser reads .jsx and .tsx alike; its tree is the one the rules' types are. */
|
|
14
|
+
export const STRUCTURE_FILES = {
|
|
15
|
+
files: ['**/*.jsx', '**/*.tsx'],
|
|
16
|
+
languageOptions: { parser, parserOptions: { ecmaFeatures: { jsx: true } } },
|
|
17
|
+
// An `eslint-disable` comment the site has for its own tools isn't a problem of the page.
|
|
18
|
+
linterOptions: { reportUnusedDisableDirectives: 'off' },
|
|
19
|
+
};
|
|
20
|
+
// ── a rule's tests (testing.ts) ──
|
|
21
|
+
/** The page folder testing.ts's `page()` writes a page to. */
|
|
22
|
+
export const TEST_PAGE = 'packages/site-app/src/pages/test';
|
|
23
|
+
/** The export's site package, as master's packageJson.ts and the Express adapter write it, with one wrapper, shortened. */
|
|
24
|
+
export const SITE_MANIFEST = {
|
|
25
|
+
name: '@site/site-app',
|
|
26
|
+
scripts: { dev: 'origami-site-express dev', build: 'origami-site-express build', start: 'origami-site-express start' },
|
|
27
|
+
dependencies: {
|
|
28
|
+
'@origami-wrappers/WixCMS': 'https://example.com/WixCMS/components.tgz',
|
|
29
|
+
'@wix/origami-auth': '^1.0.1',
|
|
30
|
+
'@wix/origami-runtime': 'latest',
|
|
31
|
+
'@wix/origami-services': '^1.0.0',
|
|
32
|
+
'@wix/origami-site': '^1.0.0',
|
|
33
|
+
'@wix/origami-site-express': '^1.0.0',
|
|
34
|
+
'@wix/origami-system-components': '^1.0.0',
|
|
35
|
+
'@wix/sdk': '^1.21.13',
|
|
36
|
+
'@wix/services-manager': '^1.0.7',
|
|
37
|
+
'@wix/services-manager-react': '^1.0.8',
|
|
38
|
+
'@wix/site-service-interactions': '^1.0.45',
|
|
39
|
+
'@wix/thunderbolt-common-css': '^1.20.0',
|
|
40
|
+
react: '^18.3.1',
|
|
41
|
+
'react-dom': '^18.3.1',
|
|
42
|
+
},
|
|
43
|
+
devDependencies: { '@wix/origami-skills': '^1.0.0' },
|
|
44
|
+
origami: { adapter: '@wix/origami-site-express', layout: 2 },
|
|
45
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { validate } from './validate.ts';
|
|
2
|
+
export { CRASHED, DIRECTIVE, PAGE_RULES } from './constants.ts';
|
|
3
|
+
export type { AnyRule, Problem, ValidateEvent, ValidateInput } from './types.ts';
|
|
4
|
+
export type { Files, FontCatalog, Loc } from './shared/index.ts';
|
|
5
|
+
export * from './rules/index.ts';
|
|
6
|
+
export type { CallbackName, CallbackNode } from './rules-adapter/index.ts';
|
|
7
|
+
export * from './rules-api/index.ts';
|
|
8
|
+
export * from './page-model/index.ts';
|
|
9
|
+
export * from './project-model/index.ts';
|
|
10
|
+
export * from './site-code-model/index.ts';
|
|
11
|
+
export { COMPONENT_CATALOG_FILE, GLOBAL_COMPONENTS, INTERACTIONS_CONFIG, GLOBAL_COMPONENTS_FOLDER, GLOBAL_FOLDER, GLOBAL_MENUS, GLOBAL_MENUS_FOLDER, PAGES_FOLDER, POPUPS_FOLDER, SITE_APP, SITE_CODE, SITE_CODE_SRC, SITE_PACKAGE_JSON, SITE_SRC, THEME_FILE, WORKSPACE_PACKAGES, createSrcReaddir, posixPath, readThemeTokens, walk } from './shared/index.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @wix/validation-infra: write rules in any repo, and run them on a site. Each part's index.ts is its door; this file opens them to the package.
|
|
2
|
+
// Running rules on a site. `Problem` here is validate()'s; it wins over the rules API's `Problem<Check>` (a rule reports through `Report`).
|
|
3
|
+
export { validate } from "./validate.js";
|
|
4
|
+
export { CRASHED, DIRECTIVE, PAGE_RULES } from "./constants.js";
|
|
5
|
+
// Writing a rule: a page rule, a project rule, and what their callbacks get.
|
|
6
|
+
export * from "./rules/index.js";
|
|
7
|
+
// What a rule reads: the rules API, the page model, the project model, the site-code model, where files sit in the site, posix paths,
|
|
8
|
+
// and every node of a file a rule parses (walk).
|
|
9
|
+
export * from "./rules-api/index.js";
|
|
10
|
+
export * from "./page-model/index.js";
|
|
11
|
+
export * from "./project-model/index.js";
|
|
12
|
+
export * from "./site-code-model/index.js";
|
|
13
|
+
export { COMPONENT_CATALOG_FILE, GLOBAL_COMPONENTS, INTERACTIONS_CONFIG, GLOBAL_COMPONENTS_FOLDER, GLOBAL_FOLDER, GLOBAL_MENUS, GLOBAL_MENUS_FOLDER, PAGES_FOLDER, POPUPS_FOLDER, SITE_APP, SITE_CODE, SITE_CODE_SRC, SITE_PACKAGE_JSON, SITE_SRC, THEME_FILE, WORKSPACE_PACKAGES, createSrcReaddir, posixPath, readThemeTokens, walk } from "./shared/index.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** A package's manifest, read to tell it is installed; a wrapper's package need not export it. */
|
|
2
|
+
export declare const PACKAGE_JSON = "package.json";
|
|
3
|
+
/** A wrapper's definition, beside its module (`<app>/<name>/index.ts` → `<app>/<name>/definition.ts`); not a package export. */
|
|
4
|
+
export declare const DEFINITION_FILE = "definition.ts";
|
|
5
|
+
/** The `exports` conditions an ESM import in node matches; the first of them in the manifest's order wins, as in node. */
|
|
6
|
+
export declare const EXPORT_CONDITIONS: readonly string[];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** A package's manifest, read to tell it is installed; a wrapper's package need not export it. */
|
|
2
|
+
export const PACKAGE_JSON = 'package.json';
|
|
3
|
+
/** A wrapper's definition, beside its module (`<app>/<name>/index.ts` → `<app>/<name>/definition.ts`); not a package export. */
|
|
4
|
+
export const DEFINITION_FILE = 'definition.ts';
|
|
5
|
+
/** The `exports` conditions an ESM import in node matches; the first of them in the manifest's order wins, as in node. */
|
|
6
|
+
export const EXPORT_CONDITIONS = ['node', 'import', 'default'];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { readPackageFileFromFiles } from './read-from-files.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { readPackageFileFromFiles } from "./read-from-files.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Files, ReadPackageFile } from '../shared/index.ts';
|
|
2
|
+
/** validate()'s reader of its `packageFiles`: by path from the site root as its `files` are (`node_modules/@origami-wrappers/wixEditorElements/
|
|
3
|
+
* package.json`, `…/button/defaults.css`): the package is the first `<folder>/node_modules/<name>` above the file that has its package.json, as
|
|
4
|
+
* node finds it; a request resolves through that package.json's `exports` (`"./*\/css": "./*\/defaults.css"`), or as its path without one; a
|
|
5
|
+
* wrapper's definition.ts sits beside the module its request resolves to. Null for a file not in `files`, never a throw. */
|
|
6
|
+
export declare function readPackageFileFromFiles(files: Files): ReadPackageFile;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { DEFINITION_FILE, EXPORT_CONDITIONS, PACKAGE_JSON } from "./constants.js";
|
|
2
|
+
import { readPackageRequest } from "./utils.js";
|
|
3
|
+
/** validate()'s reader of its `packageFiles`: by path from the site root as its `files` are (`node_modules/@origami-wrappers/wixEditorElements/
|
|
4
|
+
* package.json`, `…/button/defaults.css`): the package is the first `<folder>/node_modules/<name>` above the file that has its package.json, as
|
|
5
|
+
* node finds it; a request resolves through that package.json's `exports` (`"./*\/css": "./*\/defaults.css"`), or as its path without one; a
|
|
6
|
+
* wrapper's definition.ts sits beside the module its request resolves to. Null for a file not in `files`, never a throw. */
|
|
7
|
+
export function readPackageFileFromFiles(files) {
|
|
8
|
+
const read = (file) => (Object.hasOwn(files, file) ? { file, text: files[file] } : null);
|
|
9
|
+
return (request, fromFile) => {
|
|
10
|
+
const { name, kind, subpath } = readPackageRequest(request);
|
|
11
|
+
const folder = findPackageFolder(name, fromFile, files);
|
|
12
|
+
if (folder === null)
|
|
13
|
+
return null;
|
|
14
|
+
if (kind === 'manifest')
|
|
15
|
+
return read(`${folder}/${PACKAGE_JSON}`);
|
|
16
|
+
const resolved = resolveExport(readExports(files[`${folder}/${PACKAGE_JSON}`]), subpath);
|
|
17
|
+
if (resolved === null)
|
|
18
|
+
return null;
|
|
19
|
+
const file = `${folder}/${resolved}`;
|
|
20
|
+
return read(kind === 'definition' ? `${file.slice(0, file.lastIndexOf('/'))}/${DEFINITION_FILE}` : file);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A package.json's `exports`; null for one that doesn't parse, as node then resolves nothing from it. */
|
|
24
|
+
function readExports(manifest) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(manifest).exports;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The first `<folder>/node_modules/<name>` with a package.json, from the file's folder up to the root; null when none. */
|
|
33
|
+
function findPackageFolder(name, fromFile, files) {
|
|
34
|
+
for (let folder = fromFile.slice(0, Math.max(fromFile.lastIndexOf('/'), 0));; folder = folder.slice(0, Math.max(folder.lastIndexOf('/'), 0))) {
|
|
35
|
+
const found = `${folder ? `${folder}/` : ''}node_modules/${name}`;
|
|
36
|
+
if (Object.hasOwn(files, `${found}/${PACKAGE_JSON}`))
|
|
37
|
+
return found;
|
|
38
|
+
if (!folder)
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// ponytail: exports as wrapper and system packages write them (a string, subpath keys, one `*` per pattern, the conditions in
|
|
43
|
+
// EXPORT_CONDITIONS); no extension or index.js guessing for a package without exports. Add when a package that needs it is sent.
|
|
44
|
+
/** The path in the package (no `./`) that `exports` maps `subpath` (`button/css`, '' for the package itself) to; the subpath itself without
|
|
45
|
+
* exports; null when the package doesn't export it. */
|
|
46
|
+
function resolveExport(exports, subpath) {
|
|
47
|
+
if (exports === undefined)
|
|
48
|
+
return subpath || null;
|
|
49
|
+
if (exports === null)
|
|
50
|
+
return null;
|
|
51
|
+
const key = subpath ? `./${subpath}` : '.';
|
|
52
|
+
const map = isSubpathMap(exports) ? exports : { '.': exports };
|
|
53
|
+
if (Object.hasOwn(map, key))
|
|
54
|
+
return readTarget(map[key], '');
|
|
55
|
+
const pattern = Object.keys(map)
|
|
56
|
+
.filter((candidate) => {
|
|
57
|
+
const [prefix, suffix, extra] = candidate.split('*');
|
|
58
|
+
return suffix !== undefined && extra === undefined && key.startsWith(prefix) && key.endsWith(suffix) && key.length >= prefix.length + suffix.length;
|
|
59
|
+
})
|
|
60
|
+
// node's order: the longest prefix, then the longest key
|
|
61
|
+
.sort((a, b) => b.indexOf('*') - a.indexOf('*') || b.length - a.length)[0];
|
|
62
|
+
if (pattern === undefined)
|
|
63
|
+
return null;
|
|
64
|
+
const [prefix, suffix] = pattern.split('*');
|
|
65
|
+
return readTarget(map[pattern], key.slice(prefix.length, key.length - suffix.length));
|
|
66
|
+
}
|
|
67
|
+
const isSubpathMap = (exports) => typeof exports === 'object' && exports !== null && !Array.isArray(exports) && Object.keys(exports).some((key) => key.startsWith('.'));
|
|
68
|
+
/** A target's path, its `*` the matched part; for conditions, the first key in the manifest's order that is one of EXPORT_CONDITIONS; null for none (or null, which hides a path). */
|
|
69
|
+
function readTarget(target, match) {
|
|
70
|
+
if (typeof target === 'string')
|
|
71
|
+
return target.startsWith('./') ? target.slice(2).replaceAll('*', match) : null;
|
|
72
|
+
if (typeof target !== 'object' || target === null || Array.isArray(target))
|
|
73
|
+
return null;
|
|
74
|
+
const condition = Object.keys(target).find((name) => EXPORT_CONDITIONS.includes(name));
|
|
75
|
+
return condition === undefined ? null : readTarget(target[condition], match);
|
|
76
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** A package request, read by readPackageRequest: the package's name; `manifest`: its package.json; `definition`: the definition.ts beside the
|
|
2
|
+
* module `subpath` resolves to; `export`: the file `subpath` resolves to. */
|
|
3
|
+
export interface PackageRequest {
|
|
4
|
+
name: string;
|
|
5
|
+
kind: 'manifest' | 'definition' | 'export';
|
|
6
|
+
subpath: string;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PackageRequest } from './types.ts';
|
|
2
|
+
/** `@origami-wrappers/wixEditorElements/button/css` → its package, `@origami-wrappers/wixEditorElements`, and `button/css`; what it asks for:
|
|
3
|
+
* the package's manifest, a definition beside a module, or a package export. */
|
|
4
|
+
export declare function readPackageRequest(request: string): PackageRequest;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// A package request split into its package and the path inside it.
|
|
2
|
+
import { DEFINITION_FILE, PACKAGE_JSON } from "./constants.js";
|
|
3
|
+
/** `@origami-wrappers/wixEditorElements/button/css` → its package, `@origami-wrappers/wixEditorElements`, and `button/css`; what it asks for:
|
|
4
|
+
* the package's manifest, a definition beside a module, or a package export. */
|
|
5
|
+
export function readPackageRequest(request) {
|
|
6
|
+
const parts = request.split('/');
|
|
7
|
+
const length = request.startsWith('@') ? 2 : 1;
|
|
8
|
+
const name = parts.slice(0, length).join('/');
|
|
9
|
+
const subpath = parts.slice(length).join('/');
|
|
10
|
+
if (subpath === PACKAGE_JSON)
|
|
11
|
+
return { name, kind: 'manifest', subpath };
|
|
12
|
+
if (subpath === DEFINITION_FILE || subpath.endsWith(`/${DEFINITION_FILE}`))
|
|
13
|
+
return { name, kind: 'definition', subpath: subpath.slice(0, -DEFINITION_FILE.length).replace(/\/$/, '') };
|
|
14
|
+
return { name, kind: 'export', subpath };
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// A page's files (structure.jsx or .tsx + layout.css + style.css, and the theme) → PageModel: its components, each with its CSS.
|
|
2
|
+
import { connectCssToComponents } from "./connect-css.js";
|
|
3
|
+
import { readCss } from "./read-css.js";
|
|
4
|
+
import { readStructure } from "./read-structure.js";
|
|
5
|
+
import { GLOBAL_COMPONENTS_FOLDER } from "../shared/index.js";
|
|
6
|
+
export function buildPageModel({ jsxFile, text, program }, readFile) {
|
|
7
|
+
const page = jsxFile.slice(0, jsxFile.lastIndexOf('/'));
|
|
8
|
+
const structure = readStructure(jsxFile, text, program);
|
|
9
|
+
const css = readCss(page, readFile);
|
|
10
|
+
connectCssToComponents(structure.components, css);
|
|
11
|
+
return { path: page, kind: page.startsWith(`${GLOBAL_COMPONENTS_FOLDER}/`) ? 'global' : 'page', jsxFile, ...structure, css };
|
|
12
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// THE place that ties CSS to components: each component takes the CSS of each of its class names (`.x`, as the import
|
|
2
|
+
// matches it since converter 0.63). Class names no component took become unconnectedCss.
|
|
3
|
+
import { CSS_FILES, CSS_KINDS } from "./constants.js";
|
|
4
|
+
/** Two components with the same class name both take its CSS, as in a browser. */
|
|
5
|
+
export function connectCssToComponents(components, css) {
|
|
6
|
+
const connected = new Set();
|
|
7
|
+
for (const component of Object.values(components)) {
|
|
8
|
+
const taken = component.classNames.filter((name) => css.byClassName[name]);
|
|
9
|
+
if (!taken.length)
|
|
10
|
+
continue;
|
|
11
|
+
component.css = joinCss(taken.map((name) => css.byClassName[name]));
|
|
12
|
+
for (const name of taken)
|
|
13
|
+
connected.add(name);
|
|
14
|
+
}
|
|
15
|
+
moveUnconnected(css, connected);
|
|
16
|
+
}
|
|
17
|
+
/** One component's CSS from several class names: per kind and property, the lines of all of them in the order they're written,
|
|
18
|
+
* as the cascade reads them (`.card` then `.wide` in the file: `.wide` wins, whatever the className's order). A line that two of
|
|
19
|
+
* its classes both reach (`.a, .b { … }` on className="a b") is one line, as the browser applies it once. */
|
|
20
|
+
function joinCss(all) {
|
|
21
|
+
if (all.length === 1)
|
|
22
|
+
return all[0];
|
|
23
|
+
return Object.fromEntries(CSS_KINDS.map((kind) => {
|
|
24
|
+
const joined = Object.create(null);
|
|
25
|
+
for (const componentCss of all)
|
|
26
|
+
for (const [property, lines] of Object.entries(componentCss[kind]))
|
|
27
|
+
(joined[property] ??= []).push(...lines);
|
|
28
|
+
for (const [property, lines] of Object.entries(joined))
|
|
29
|
+
joined[property] = lines.sort(byPlace).filter((line, index) => index === 0 || !isSameLine(lines[index - 1], line));
|
|
30
|
+
return [kind, joined];
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
const byPlace = (a, b) => CSS_FILES.indexOf(a.file) - CSS_FILES.indexOf(b.file) || a.line - b.line || a.column - b.column;
|
|
34
|
+
/** The same line of the file, reached through the same selector and media. */
|
|
35
|
+
const isSameLine = (a, b) => byPlace(a, b) === 0 && a.selector === b.selector && a.media === b.media;
|
|
36
|
+
function moveUnconnected(css, connected) {
|
|
37
|
+
for (const [name, componentCss] of Object.entries(css.byClassName)) {
|
|
38
|
+
if (connected.has(name))
|
|
39
|
+
continue;
|
|
40
|
+
css.unconnectedCss[`.${name}`] = componentCss;
|
|
41
|
+
delete css.byClassName[name];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Loc } from '../shared/index.ts';
|
|
2
|
+
import type { CssFile, CssKind } from './types.ts';
|
|
3
|
+
/** layout.css, style.css, and what a component gets from the theme through @apply. */
|
|
4
|
+
export declare const CSS_KINDS: readonly ["layout", "style", "theme"];
|
|
5
|
+
/** A page's own CSS files, in the order they're read; with the structure file, the files that make a page (pages.ts). */
|
|
6
|
+
export declare const CSS_FILES: readonly ["layout.css", "style.css"];
|
|
7
|
+
/** The kind of each of a page's own CSS files. */
|
|
8
|
+
export declare const KIND_OF_FILE: Record<CssFile, CssKind>;
|
|
9
|
+
export declare const FILE_START: Loc;
|
|
10
|
+
/** The folders pages sit in: the site's pages, and the global components. */
|
|
11
|
+
export declare const PAGE_ROOTS: readonly string[];
|
|
12
|
+
/** A page's structure file: structure.tsx, or structure.jsx (exports). structure.tsx wins if both exist. */
|
|
13
|
+
export declare const JSX_FILES: readonly string[];
|
|
14
|
+
/** Written by the site generator inside a page folder; never a page itself. */
|
|
15
|
+
export declare const GENERATED_FOLDER = "_generated";
|
|
16
|
+
/** A change to one of these is a change to every page. */
|
|
17
|
+
export declare const EVERY_PAGE_FILES: readonly string[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The page model's fixed values: a page's CSS kinds and files, the files that make a page and where pages sit, and how a selector names its owner.
|
|
2
|
+
import { GLOBAL_COMPONENTS_FOLDER, PAGES_FOLDER, SITE_PACKAGE_JSON, THEME_FILE } from "../shared/index.js";
|
|
3
|
+
/** layout.css, style.css, and what a component gets from the theme through @apply. */
|
|
4
|
+
export const CSS_KINDS = ['layout', 'style', 'theme'];
|
|
5
|
+
/** A page's own CSS files, in the order they're read; with the structure file, the files that make a page (pages.ts). */
|
|
6
|
+
export const CSS_FILES = ['layout.css', 'style.css'];
|
|
7
|
+
/** The kind of each of a page's own CSS files. */
|
|
8
|
+
export const KIND_OF_FILE = { 'layout.css': 'layout', 'style.css': 'style' };
|
|
9
|
+
export const FILE_START = { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } };
|
|
10
|
+
/** The folders pages sit in: the site's pages, and the global components. */
|
|
11
|
+
export const PAGE_ROOTS = [PAGES_FOLDER, GLOBAL_COMPONENTS_FOLDER];
|
|
12
|
+
/** A page's structure file: structure.tsx, or structure.jsx (exports). structure.tsx wins if both exist. */
|
|
13
|
+
export const JSX_FILES = ['structure.tsx', 'structure.jsx'];
|
|
14
|
+
/** Written by the site generator inside a page folder; never a page itself. */
|
|
15
|
+
export const GENERATED_FOLDER = '_generated';
|
|
16
|
+
/** A change to one of these is a change to every page. */
|
|
17
|
+
export const EVERY_PAGE_FILES = [THEME_FILE, SITE_PACKAGE_JSON];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// CSS selectors: joining a nested rule to its parent. Which component a selector belongs to is shared/css.ts's parseComponentSelector.
|
|
2
|
+
/** `&` is the parent selector; a child without `&` is a descendant of it. */
|
|
3
|
+
export function nestSelectors(parents, children) {
|
|
4
|
+
if (!parents.length)
|
|
5
|
+
return children;
|
|
6
|
+
return parents.flatMap((parent) => children.map((child) => (child.includes('&') ? child.replaceAll('&', parent) : `${parent} ${child}`)));
|
|
7
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { buildPageModel } from './build-page-model.ts';
|
|
2
|
+
export { findPages } from './pages.ts';
|
|
3
|
+
export { CSS_FILES, FILE_START, JSX_FILES } from './constants.ts';
|
|
4
|
+
export type { Component, ComponentCss, CssByProperty, CssDeclaration, CssError, CssFile, CssKind, PageCss, PageModel } from './types.ts';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// A page is a folder with structure.jsx (or structure.tsx) + layout.css + style.css: a site page, or a global component like the header.
|
|
2
|
+
// Its servicesConfigs/interactions.ts is the page's too (rulesApi.getStateTransitions). Every page also reads the site's theme.css, for the mixins its CSS @applies, and its wrappers' contracts (component-contract.json, or defaults.css; the site app's package.json pins them).
|
|
3
|
+
import { INTERACTIONS_CONFIG } from "../shared/index.js";
|
|
4
|
+
import { CSS_FILES, EVERY_PAGE_FILES, GENERATED_FOLDER, JSX_FILES, PAGE_ROOTS } from "./constants.js";
|
|
5
|
+
/** The structure files of the pages these files belong to (every page when the theme or the site app's package.json is one of them), among currentFiles. */
|
|
6
|
+
export function findPages(files, currentFiles) {
|
|
7
|
+
const existing = new Set(currentFiles);
|
|
8
|
+
const pageFilesToCheck = files.some((file) => EVERY_PAGE_FILES.includes(file)) ? currentFiles : files;
|
|
9
|
+
const pages = new Set(pageFilesToCheck.map(pageOfFile).filter((page) => page !== null));
|
|
10
|
+
return [...pages].map((page) => JSX_FILES.map((name) => `${page}/${name}`).find((file) => existing.has(file))).filter((file) => file !== undefined);
|
|
11
|
+
}
|
|
12
|
+
/** 'packages/site-app/src/pages/about-us/team/style.css' → 'packages/site-app/src/pages/about-us/team'; its servicesConfigs/interactions.ts
|
|
13
|
+
* too (style-properties-allowed reads its state transitions); other files → null. Pages can be nested. */
|
|
14
|
+
function pageOfFile(file) {
|
|
15
|
+
const page = file.endsWith(`/${INTERACTIONS_CONFIG}`) ? file.slice(0, -INTERACTIONS_CONFIG.length - 1) : file.slice(0, file.lastIndexOf('/'));
|
|
16
|
+
const isInPageFolder = PAGE_ROOTS.some((root) => page.startsWith(`${root}/`)) && !page.split('/').includes(GENERATED_FOLDER);
|
|
17
|
+
return isInPageFolder && [...JSX_FILES, ...CSS_FILES, INTERACTIONS_CONFIG].map((name) => `${page}/${name}`).includes(file) ? page : null;
|
|
18
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PageCss } from './types.ts';
|
|
2
|
+
import { type ReadFile } from '../shared/index.ts';
|
|
3
|
+
/** theme.css is read and parsed once: its tree feeds the mixins `@apply` puts in, and rulesApi reads its tokens and breakpoints. */
|
|
4
|
+
export declare function readCss(page: string, readFile: ReadFile): PageCss;
|