@astryxdesign/cli 0.7.4 → 0.7.5-canary.07605c2
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 +18 -18
- package/api/component/_adapter.mjs +16 -1
- package/api/component/component.test.mjs +31 -0
- package/api/discover/_adapter.d.mts +1 -1
- package/api/docs/_adapter.d.mts +5 -2
- package/api/docs/_adapter.mjs +24 -16
- package/api/docs/docs.test.mjs +18 -0
- package/api/docs/integrationDocs.test.mjs +2 -2
- package/api/experimental.d.mts +5 -0
- package/api/experimental.mjs +15 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/icon/_adapter.d.mts +3 -2
- package/api/icon/_adapter.mjs +16 -3
- package/api/icon/_authoring.d.mts +5 -1
- package/api/icon/_authoring.mjs +478 -63
- package/api/icon/_options.d.mts +11 -0
- package/api/icon/_options.mjs +39 -4
- package/api/icon/_records.d.mts +19 -0
- package/api/icon/_records.mjs +25 -0
- package/api/icon/add/add.d.mts +3 -3
- package/api/icon/add/add.mjs +227 -14
- package/api/icon/all-or-nothing.test.mjs +283 -0
- package/api/icon/detail/detail.mjs +2 -13
- package/api/icon/error-codes.test.mjs +14 -7
- package/api/icon/icon.d.mts +2 -2
- package/api/icon/icon.doc.mjs +30 -2
- package/api/icon/icon.mjs +57 -8
- package/api/icon/icon.test.mjs +133 -1
- package/api/icon/icon.type.d.mts +126 -1
- package/api/icon/icon.type.mjs +67 -3
- package/api/icon/iconAdd.doc.mjs +22 -6
- package/api/icon/iconMap.doc.mjs +24 -8
- package/api/icon/iconNewDefinition.doc.mjs +2 -2
- package/api/icon/iconNewSet.doc.mjs +30 -2
- package/api/icon/iconUse.doc.mjs +4 -1
- package/api/icon/map/map.d.mts +4 -4
- package/api/icon/map/map.mjs +134 -9
- package/api/icon/new-definition/new-definition.mjs +6 -2
- package/api/icon/new-set/new-set.mjs +2 -2
- package/api/icon/new-set/new-set.test.mjs +60 -10
- package/api/icon/option-pairs.test.mjs +368 -0
- package/api/icon/search/search.d.mts +10 -0
- package/api/icon/search/search.mjs +55 -14
- package/api/icon/use/use.mjs +2 -13
- package/api/integration/add-helpers.d.mts +15 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
- package/api/integration/pack-check.mjs +10 -4
- package/api/integration/pack-check.test.mjs +5 -1
- package/api/integration/validate-integration.type.d.mts +3 -2
- package/api/integration/validate-integration.type.mjs +3 -2
- package/api/render/_adapter.d.mts +28 -0
- package/api/render/_adapter.mjs +540 -0
- package/api/render/render.d.mts +15 -0
- package/api/render/render.doc.d.mts +9 -0
- package/api/render/render.doc.mjs +112 -0
- package/api/render/render.mjs +113 -0
- package/api/render/render.test.mjs +1155 -0
- package/api/render/render.type.d.mts +74 -0
- package/api/render/render.type.mjs +64 -0
- package/api/search/_adapter.test.mjs +21 -2
- package/api/search/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.mjs +3 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- package/api/template/unknown-name.test.mjs +63 -0
- package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +123 -70
- package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +19 -77
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-definitions.doc.mjs +2 -2
- package/assets/docs/tree/icon-set-files.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +50 -20
- package/assets/docs/tree/render-pages.doc.mjs +94 -0
- package/assets/icons/definitions/activity.doc.mjs +1 -1
- package/assets/icons/definitions/alignCenter.doc.mjs +9 -0
- package/assets/icons/definitions/alignEnd.doc.mjs +10 -0
- package/assets/icons/definitions/alignStart.doc.mjs +10 -0
- package/assets/icons/definitions/archive.doc.mjs +1 -1
- package/assets/icons/definitions/arrowsUpDown.doc.mjs +1 -1
- package/assets/icons/definitions/attach.doc.mjs +1 -1
- package/assets/icons/definitions/audio.doc.mjs +1 -1
- package/assets/icons/definitions/cart.doc.mjs +1 -1
- package/assets/icons/definitions/checkDouble.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsLeft.doc.mjs +1 -1
- package/assets/icons/definitions/chevronsRight.doc.mjs +1 -1
- package/assets/icons/definitions/close.doc.mjs +1 -1
- package/assets/icons/definitions/collapse.doc.mjs +1 -1
- package/assets/icons/definitions/compose.doc.mjs +1 -1
- package/assets/icons/definitions/conversation.doc.mjs +1 -1
- package/assets/icons/definitions/copy.doc.mjs +1 -1
- package/assets/icons/definitions/cpu.doc.mjs +9 -0
- package/assets/icons/definitions/database.doc.mjs +1 -1
- package/assets/icons/definitions/desktop.doc.mjs +1 -1
- package/assets/icons/definitions/discount.doc.mjs +1 -1
- package/assets/icons/definitions/download.doc.mjs +1 -1
- package/assets/icons/definitions/emoji.doc.mjs +1 -1
- package/assets/icons/definitions/error.doc.mjs +1 -1
- package/assets/icons/definitions/expand.doc.mjs +1 -1
- package/assets/icons/definitions/experiment.doc.mjs +1 -1
- package/assets/icons/definitions/externalLink.doc.mjs +7 -1
- package/assets/icons/definitions/eyeSlash.doc.mjs +1 -1
- package/assets/icons/definitions/flipHorizontal.doc.mjs +9 -0
- package/assets/icons/definitions/flipVertical.doc.mjs +9 -0
- package/assets/icons/definitions/folderAdd.doc.mjs +1 -1
- package/assets/icons/definitions/gitBranch.doc.mjs +9 -0
- package/assets/icons/definitions/gitFork.doc.mjs +9 -0
- package/assets/icons/definitions/info.doc.mjs +1 -1
- package/assets/icons/definitions/location.doc.mjs +1 -1
- package/assets/icons/definitions/mention.doc.mjs +1 -1
- package/assets/icons/definitions/menu.doc.mjs +1 -1
- package/assets/icons/definitions/microphone.doc.mjs +1 -1
- package/assets/icons/definitions/mobile.doc.mjs +1 -1
- package/assets/icons/definitions/money.doc.mjs +1 -1
- package/assets/icons/definitions/moreHorizontal.doc.mjs +1 -1
- package/assets/icons/definitions/notificationActive.doc.mjs +1 -1
- package/assets/icons/definitions/notificationOff.doc.mjs +1 -1
- package/assets/icons/definitions/panelLeft.doc.mjs +10 -0
- package/assets/icons/definitions/panelRight.doc.mjs +10 -0
- package/assets/icons/definitions/raiseHand.doc.mjs +1 -1
- package/assets/icons/definitions/redo.doc.mjs +1 -1
- package/assets/icons/definitions/refresh.doc.mjs +1 -1
- package/assets/icons/definitions/rotate.doc.mjs +9 -0
- package/assets/icons/definitions/scale.doc.mjs +10 -0
- package/assets/icons/definitions/search.doc.mjs +1 -1
- package/assets/icons/definitions/send.doc.mjs +1 -1
- package/assets/icons/definitions/settings.doc.mjs +1 -1
- package/assets/icons/definitions/star.doc.mjs +1 -1
- package/assets/icons/definitions/stop.doc.mjs +1 -1
- package/assets/icons/definitions/success.doc.mjs +1 -1
- package/assets/icons/definitions/textCase.doc.mjs +9 -0
- package/assets/icons/definitions/thumbsDown.doc.mjs +1 -1
- package/assets/icons/definitions/thumbsUp.doc.mjs +1 -1
- package/assets/icons/definitions/trendDown.doc.mjs +7 -1
- package/assets/icons/definitions/trendUp.doc.mjs +1 -1
- package/assets/icons/definitions/undo.doc.mjs +1 -1
- package/assets/icons/definitions/upload.doc.mjs +1 -1
- package/assets/icons/definitions/userAdd.doc.mjs +1 -1
- package/assets/icons/definitions/warning.doc.mjs +1 -1
- package/assets/templates/blocks/components/Tokenizer/TokenizerClear.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +30 -30
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerIcon.tsx +15 -14
- package/assets/templates/blocks/components/Tokenizer/TokenizerMaxEntries.tsx +17 -16
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +29 -29
- package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +9 -8
- package/assets/templates/blocks/components/Tokenizer/TokenizerStates.tsx +39 -41
- package/assets/templates/pages/canvas-editor/template.doc.mjs +9 -0
- package/assets/templates/pages/form-wizard-inline/template.doc.mjs +6 -1
- package/assets/templates/pages/table-comparison/template.doc.mjs +1 -0
- package/assets/templates/pages/table-tree/template.doc.mjs +3 -0
- package/authoring/debug/parse.d.mts +2 -2
- package/authoring/doctypes/icon-definition/icon-definition.doc.mjs +4 -4
- package/authoring/doctypes/icon-set/icon-set-data.doc.mjs +6 -0
- package/authoring/doctypes/icon-set/parse.d.mts +1 -0
- package/authoring/doctypes/icon-set/parse.mjs +1 -0
- package/authoring/doctypes/icon-set/type.ts +5 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +2 -1
- package/clients/cli/commands/doctor.mjs +16 -17
- package/clients/cli/commands/experimental/icon.mjs +167 -35
- package/clients/cli/commands/experimental/index.mjs +2 -0
- package/clients/cli/commands/experimental/render.mjs +113 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +14 -4
- package/clients/cli/commands/experimental-icon-map.doc.mjs +12 -2
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +28 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +18 -2
- package/clients/cli/commands/experimental-render.doc.mjs +60 -0
- package/clients/cli/commands/experimental.doc.mjs +4 -2
- package/clients/cli/commands/icon-authoring.test.mjs +1823 -13
- package/clients/cli/commands/icon-options-wiring.test.mjs +158 -0
- package/clients/cli/commands/icon.test.mjs +1 -1
- package/clients/cli/commands/template.mjs +4 -1
- package/clients/cli/commands/text-json-parity.test.mjs +46 -0
- package/clients/cli/index.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +2 -1
- package/foundation/discovery/component-discovery.export-names.test.mjs +41 -0
- package/foundation/discovery/component-discovery.mjs +13 -7
- package/foundation/discovery/icon-set-checks.d.mts +43 -9
- package/foundation/discovery/icon-set-checks.mjs +296 -21
- package/foundation/discovery/icon-set-checks.test.mjs +125 -12
- package/foundation/discovery/icon-set-writer.d.mts +192 -3
- package/foundation/discovery/icon-set-writer.mjs +608 -34
- package/foundation/discovery/icon-set-writer.test.mjs +26 -0
- package/foundation/discovery/module-exports.mjs +82 -25
- package/foundation/discovery/module-exports.test.mjs +38 -1
- package/foundation/discovery/package-exports.d.mts +13 -0
- package/foundation/discovery/package-exports.mjs +40 -1
- package/foundation/discovery/template-icons.d.mts +6 -0
- package/foundation/discovery/template-icons.mjs +98 -10
- package/foundation/discovery/template-icons.test.mjs +103 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +20 -5
- package/foundation/doc-compiler/import.mjs +81 -8
- package/foundation/doc-compiler/read.d.mts +4 -3
- package/foundation/doc-compiler/read.mjs +15 -10
- package/foundation/doc-compiler/read.test.mjs +59 -1
- package/foundation/doc-compiler/tree.mjs +3 -1
- package/foundation/fs/module-loader.d.mts +3 -2
- package/foundation/fs/module-loader.mjs +3 -2
- package/foundation/fs/publish-file-eperm-mutation.test.mjs +2 -0
- package/foundation/fs/read-record.d.mts +27 -0
- package/foundation/fs/read-record.mjs +83 -15
- package/foundation/fs/read-record.test.mjs +88 -1
- package/foundation/render/assets.d.mts +31 -0
- package/foundation/render/assets.mjs +102 -0
- package/foundation/render/component-map.d.mts +52 -0
- package/foundation/render/component-map.mjs +141 -0
- package/foundation/render/css-cut.d.mts +29 -0
- package/foundation/render/css-cut.mjs +144 -0
- package/foundation/render/embedded-files.d.mts +28 -0
- package/foundation/render/embedded-files.mjs +108 -0
- package/foundation/render/embedded-imports.d.mts +61 -0
- package/foundation/render/embedded-imports.mjs +315 -0
- package/foundation/render/html-reader.d.mts +37 -0
- package/foundation/render/html-reader.mjs +121 -0
- package/foundation/render/page-worker.d.mts +7 -0
- package/foundation/render/page-worker.mjs +519 -0
- package/foundation/render/rebuild.d.mts +19 -0
- package/foundation/render/rebuild.mjs +269 -0
- package/foundation/render/render-page.d.mts +85 -0
- package/foundation/render/render-page.mjs +612 -0
- package/foundation/render/render.test.mjs +117 -0
- package/foundation/render/source-block.d.mts +52 -0
- package/foundation/render/source-block.mjs +106 -0
- package/foundation/response/error-codes.d.mts +10 -1
- package/foundation/response/error-codes.doc.mjs +56 -0
- package/foundation/response/error-codes.mjs +61 -0
- package/foundation/response/response-types.doc.mjs +38 -1
- package/foundation/text/string-utils.mjs +33 -0
- package/foundation/text/string-utils.test.mjs +17 -0
- package/foundation/text/suggest-order.d.mts +17 -3
- package/foundation/text/suggest-order.mjs +58 -13
- package/foundation/text/suggest-order.test.mjs +53 -0
- package/package.json +14 -9
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Which embedded files the page itself reaches, for `astryx
|
|
5
|
+
* experimental render --reverse`. A received file is untrusted, so its list
|
|
6
|
+
* of files is not: a file is written only when the embedded page reaches it
|
|
7
|
+
* through its own imports (static, re-exports, `import()` and `require()` of
|
|
8
|
+
* a string), followed module by module through the embedded tree. Relative
|
|
9
|
+
* imports resolve inside the tree; an alias import resolves through the
|
|
10
|
+
* receiving project's own `tsconfig.json` or `jsconfig.json` paths, never the
|
|
11
|
+
* sender's.
|
|
12
|
+
*
|
|
13
|
+
* @position foundation/render — used by the render subject's adapter
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import * as fs from 'node:fs';
|
|
17
|
+
import * as path from 'node:path';
|
|
18
|
+
import {parse} from '@babel/parser';
|
|
19
|
+
import {PAGE_EXTENSIONS} from './embedded-files.mjs';
|
|
20
|
+
import postcss from 'postcss';
|
|
21
|
+
import {isAssetPath} from './assets.mjs';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* What a stylesheet's `@import` rules load, read with postcss so comments
|
|
25
|
+
* are ignored.
|
|
26
|
+
* @param {string} css
|
|
27
|
+
* @returns {string[]}
|
|
28
|
+
*/
|
|
29
|
+
function cssImports(css) {
|
|
30
|
+
/** @type {string[]} */
|
|
31
|
+
const out = [];
|
|
32
|
+
try {
|
|
33
|
+
postcss.parse(css).walkAtRules(/^import$/i, rule => {
|
|
34
|
+
const m = /^\s*(?:url\(\s*(['"]?)([\s\S]*?)\1\s*\)|(['"])([\s\S]*?)\3)/.exec(rule.params);
|
|
35
|
+
const url = m ? (m[2] ?? m[4]) : '';
|
|
36
|
+
if (url) out.push(url);
|
|
37
|
+
});
|
|
38
|
+
} catch {
|
|
39
|
+
// Not valid CSS: it imports nothing a restore needs.
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Extensions an extensionless import may stand for, in resolution order. */
|
|
45
|
+
const RESOLVE_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The string specifiers one module imports.
|
|
49
|
+
* @param {string} source
|
|
50
|
+
* @param {string} file for the parser's TypeScript/JSX choice
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export function importSpecifiers(source, file) {
|
|
54
|
+
return scanModule(source, file).imports;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Relative asset paths in a piece of text: a `./`/`../` path to an image or
|
|
59
|
+
* font, bare or inside `url(...)`.
|
|
60
|
+
* @param {string} text
|
|
61
|
+
* @returns {string[]}
|
|
62
|
+
*/
|
|
63
|
+
export function assetRefsIn(text) {
|
|
64
|
+
/** @type {string[]} */
|
|
65
|
+
const out = [];
|
|
66
|
+
for (const m of text.matchAll(/(?:^|url\(\s*['"]?)(\.\.?\/[^'")\s]+)/g)) {
|
|
67
|
+
const ref = m[1].split(/[?#]/)[0];
|
|
68
|
+
if (isAssetPath(ref)) out.push(ref);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One module's imports, and the relative asset paths its strings name
|
|
75
|
+
* (`<img src="./logo.png">`, `url(./bg.png)` in a style).
|
|
76
|
+
* @param {string} source
|
|
77
|
+
* @param {string} file
|
|
78
|
+
* @returns {{imports: string[], assets: string[]}}
|
|
79
|
+
*/
|
|
80
|
+
function scanModule(source, file) {
|
|
81
|
+
/** @type {string[]} */
|
|
82
|
+
const assets = [];
|
|
83
|
+
/** @type {any} */
|
|
84
|
+
let ast;
|
|
85
|
+
try {
|
|
86
|
+
ast = parse(source, {
|
|
87
|
+
sourceType: 'module',
|
|
88
|
+
allowReturnOutsideFunction: true,
|
|
89
|
+
errorRecovery: true,
|
|
90
|
+
plugins: /\.(tsx?|mts|cts)$/.test(file) ? ['jsx', 'typescript'] : ['jsx'],
|
|
91
|
+
});
|
|
92
|
+
} catch {
|
|
93
|
+
return {imports: [], assets: []};
|
|
94
|
+
}
|
|
95
|
+
/** @type {string[]} */
|
|
96
|
+
const out = [];
|
|
97
|
+
/** @param {any} node */
|
|
98
|
+
const visit = node => {
|
|
99
|
+
if (!node || typeof node.type !== 'string') return;
|
|
100
|
+
if (node.type === 'StringLiteral') assets.push(...assetRefsIn(node.value));
|
|
101
|
+
else if (node.type === 'TemplateElement') assets.push(...assetRefsIn(node.value?.cooked ?? ''));
|
|
102
|
+
if (
|
|
103
|
+
(node.type === 'ImportDeclaration' ||
|
|
104
|
+
node.type === 'ExportNamedDeclaration' ||
|
|
105
|
+
node.type === 'ExportAllDeclaration') &&
|
|
106
|
+
node.source?.type === 'StringLiteral'
|
|
107
|
+
) {
|
|
108
|
+
out.push(node.source.value);
|
|
109
|
+
} else if (
|
|
110
|
+
node.type === 'ImportExpression' &&
|
|
111
|
+
node.source?.type === 'StringLiteral'
|
|
112
|
+
) {
|
|
113
|
+
out.push(node.source.value);
|
|
114
|
+
} else if (
|
|
115
|
+
node.type === 'CallExpression' &&
|
|
116
|
+
node.arguments?.[0]?.type === 'StringLiteral' &&
|
|
117
|
+
(node.callee?.type === 'Import' ||
|
|
118
|
+
(node.callee?.type === 'Identifier' && node.callee.name === 'require'))
|
|
119
|
+
) {
|
|
120
|
+
out.push(node.arguments[0].value);
|
|
121
|
+
}
|
|
122
|
+
for (const key of Object.keys(node)) {
|
|
123
|
+
if (key === 'loc' || key === 'start' || key === 'end' || key === 'extra') continue;
|
|
124
|
+
const value = node[key];
|
|
125
|
+
if (Array.isArray(value)) value.forEach(visit);
|
|
126
|
+
else if (value && typeof value === 'object' && typeof value.type === 'string') visit(value);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
visit(ast.program);
|
|
130
|
+
return {imports: out, assets};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Path aliases (`compilerOptions.paths`) of the project that holds `dir`, as
|
|
135
|
+
* absolute prefixes: `"@/*": ["./src/*"]` becomes `["@/", "<root>/src/"]`.
|
|
136
|
+
* @param {string} dir
|
|
137
|
+
* @returns {[string, string][]}
|
|
138
|
+
*/
|
|
139
|
+
export function receivingAliases(dir) {
|
|
140
|
+
let d = dir;
|
|
141
|
+
while (!fs.existsSync(d) && path.dirname(d) !== d) d = path.dirname(d);
|
|
142
|
+
for (;;) {
|
|
143
|
+
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
|
144
|
+
const file = path.join(d, name);
|
|
145
|
+
if (!fs.existsSync(file)) continue;
|
|
146
|
+
try {
|
|
147
|
+
const text = fs
|
|
148
|
+
.readFileSync(file, 'utf8')
|
|
149
|
+
.replace(/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, str) => str ?? '')
|
|
150
|
+
.replace(/,(\s*[}\]])/g, '$1');
|
|
151
|
+
const options = JSON.parse(text)?.compilerOptions ?? {};
|
|
152
|
+
const base = path.resolve(d, typeof options.baseUrl === 'string' ? options.baseUrl : '.');
|
|
153
|
+
/** @type {[string, string][]} */
|
|
154
|
+
const out = [];
|
|
155
|
+
for (const [key, targets] of Object.entries(options.paths ?? {})) {
|
|
156
|
+
const target = Array.isArray(targets) ? targets[0] : undefined;
|
|
157
|
+
if (typeof target !== 'string') continue;
|
|
158
|
+
if (key.endsWith('/*') && target.endsWith('/*')) {
|
|
159
|
+
out.push([key.slice(0, -1), `${path.resolve(base, target.slice(0, -2))}${path.sep}`]);
|
|
160
|
+
} else if (!key.includes('*') && !target.includes('*')) {
|
|
161
|
+
out.push([key, path.resolve(base, target)]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
} catch {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (fs.existsSync(path.join(d, 'package.json'))) return [];
|
|
170
|
+
const up = path.dirname(d);
|
|
171
|
+
if (up === d) return [];
|
|
172
|
+
d = up;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @typedef {object} Reach
|
|
178
|
+
* @property {boolean} relative - Reached by a relative import (or it is the
|
|
179
|
+
* page), so it goes where the page's layout puts it.
|
|
180
|
+
* @property {Set<string>} aliased - Absolute paths a receiving-project alias
|
|
181
|
+
* import names for it, so it also goes there.
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The embedded files the page reaches through its imports.
|
|
186
|
+
* @param {{path: string, base64: string}[]} files the embedded files, page
|
|
187
|
+
* first; every path appears once (callers refuse duplicates first)
|
|
188
|
+
* @param {{aliases?: [string, string][]}} [options] the receiving project's
|
|
189
|
+
* path aliases, absolute (see receivingAliases)
|
|
190
|
+
* @returns {{reached: Map<string, Reach>, missing: string[]}} the reached
|
|
191
|
+
* tree paths, and the relative imports the file does not carry
|
|
192
|
+
*/
|
|
193
|
+
export function reachableFiles(files, {aliases = []} = {}) {
|
|
194
|
+
const byPath = new Map(files.map(f => [f.path, f]));
|
|
195
|
+
/** @type {Map<string, Reach>} */
|
|
196
|
+
const reached = new Map();
|
|
197
|
+
/** @type {Set<string>} */
|
|
198
|
+
const missing = new Set();
|
|
199
|
+
/** @param {string} candidate tree path, `/`-separated */
|
|
200
|
+
const resolveIn = candidate => {
|
|
201
|
+
if (byPath.has(candidate)) return candidate;
|
|
202
|
+
for (const ext of RESOLVE_EXTENSIONS) if (byPath.has(candidate + ext)) return candidate + ext;
|
|
203
|
+
for (const ext of RESOLVE_EXTENSIONS) if (byPath.has(`${candidate}/index${ext}`)) return `${candidate}/index${ext}`;
|
|
204
|
+
return null;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* The one tree file an alias import names: the file whose path ends with
|
|
208
|
+
* the import's rest (`@/components/Header` -> `.../components/Header.tsx`).
|
|
209
|
+
* @param {string} rest
|
|
210
|
+
*/
|
|
211
|
+
const bySuffix = rest => {
|
|
212
|
+
/** @type {{path: string, tail: string}[]} */
|
|
213
|
+
const hits = [];
|
|
214
|
+
for (const p of byPath.keys()) {
|
|
215
|
+
for (const tail of [rest, ...RESOLVE_EXTENSIONS.map(e => rest + e), ...RESOLVE_EXTENSIONS.map(e => `${rest}/index${e}`)]) {
|
|
216
|
+
if (p === tail || p.endsWith(`/${tail}`)) {
|
|
217
|
+
hits.push({path: p, tail});
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return hits.length === 1 ? hits[0] : null;
|
|
223
|
+
};
|
|
224
|
+
/** @param {string} p */
|
|
225
|
+
const reach = p => {
|
|
226
|
+
let r = reached.get(p);
|
|
227
|
+
const fresh = !r;
|
|
228
|
+
if (!r) {
|
|
229
|
+
r = {relative: false, aliased: new Set()};
|
|
230
|
+
reached.set(p, r);
|
|
231
|
+
}
|
|
232
|
+
return {r, fresh};
|
|
233
|
+
};
|
|
234
|
+
/** @type {string[]} */
|
|
235
|
+
const queue = [];
|
|
236
|
+
if (files.length > 0) {
|
|
237
|
+
reach(files[0].path).r.relative = true;
|
|
238
|
+
queue.push(files[0].path);
|
|
239
|
+
}
|
|
240
|
+
while (queue.length > 0) {
|
|
241
|
+
const current = /** @type {string} */ (queue.shift());
|
|
242
|
+
const ext = path.posix.extname(current).toLowerCase();
|
|
243
|
+
const isModule = PAGE_EXTENSIONS.includes(ext);
|
|
244
|
+
if (!isModule && ext !== '.css') continue;
|
|
245
|
+
const entry = byPath.get(current);
|
|
246
|
+
if (!entry) continue;
|
|
247
|
+
const source = Buffer.from(entry.base64, 'base64').toString('utf8');
|
|
248
|
+
// Images and fonts a module or stylesheet names by a relative path are
|
|
249
|
+
// part of the page too.
|
|
250
|
+
const scanned = isModule
|
|
251
|
+
? scanModule(source, current)
|
|
252
|
+
: {
|
|
253
|
+
// A stylesheet's own `@import`s count as imports.
|
|
254
|
+
// A stylesheet's own relative `@import`s count as imports (read
|
|
255
|
+
// with postcss, so a commented-out one does not); a package one
|
|
256
|
+
// (`@scope/pkg/x.css`) or a remote one is not the page's file.
|
|
257
|
+
imports: cssImports(source)
|
|
258
|
+
.filter(ref => !/^([a-z]+:)?\/\//i.test(ref) && !/^[@~]/.test(ref))
|
|
259
|
+
// A bare name (`theme.css`) is the sheet beside it when the file
|
|
260
|
+
// carries one, else a package's.
|
|
261
|
+
.filter(ref => ref.startsWith('.') || byPath.has(path.posix.normalize(path.posix.join(path.posix.dirname(current), ref))))
|
|
262
|
+
.map(ref => (ref.startsWith('.') ? ref : `./${ref}`)),
|
|
263
|
+
assets: assetRefsIn(source),
|
|
264
|
+
};
|
|
265
|
+
for (const ref of scanned.assets) {
|
|
266
|
+
const joined = path.posix.normalize(path.posix.join(path.posix.dirname(current), ref));
|
|
267
|
+
if (joined.startsWith('..')) continue;
|
|
268
|
+
if (!byPath.has(joined)) {
|
|
269
|
+
missing.add(joined);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
reach(joined).r.relative = true;
|
|
273
|
+
}
|
|
274
|
+
for (const spec of scanned.imports) {
|
|
275
|
+
const bare = spec.split('?')[0];
|
|
276
|
+
if (bare.startsWith('./') || bare.startsWith('../')) {
|
|
277
|
+
const joined = path.posix.normalize(path.posix.join(path.posix.dirname(current), bare));
|
|
278
|
+
const target = joined.startsWith('..') ? null : resolveIn(joined);
|
|
279
|
+
if (!target) {
|
|
280
|
+
missing.add(joined);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const {r, fresh} = reach(target);
|
|
284
|
+
r.relative = true;
|
|
285
|
+
if (fresh) queue.push(target);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
for (const [key, aliasTarget] of aliases) {
|
|
289
|
+
const named = key.endsWith('/') ? bare.startsWith(key) : bare === key;
|
|
290
|
+
if (!named) continue;
|
|
291
|
+
// A wildcard alias names a path under its target; an exact alias
|
|
292
|
+
// (`"@theme": ["./src/theme.ts"]`) names its target file itself.
|
|
293
|
+
const exactExt = key.endsWith('/') ? '' : path.extname(aliasTarget);
|
|
294
|
+
const rest = key.endsWith('/')
|
|
295
|
+
? bare.slice(key.length)
|
|
296
|
+
: path.basename(aliasTarget, exactExt);
|
|
297
|
+
const hit = rest ? bySuffix(rest) : null;
|
|
298
|
+
if (!hit) break;
|
|
299
|
+
const target = hit.path;
|
|
300
|
+
// The alias names the module without its extension or index file;
|
|
301
|
+
// the file keeps both: `@/c/Header` -> `<alias target>/c/Header.tsx`.
|
|
302
|
+
const abs = key.endsWith('/')
|
|
303
|
+
? aliasTarget + hit.tail
|
|
304
|
+
: exactExt
|
|
305
|
+
? aliasTarget
|
|
306
|
+
: aliasTarget + hit.tail.slice(rest.length);
|
|
307
|
+
const {r, fresh} = reach(target);
|
|
308
|
+
r.aliased.add(abs);
|
|
309
|
+
if (fresh) queue.push(target);
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return {reached, missing: [...missing].sort()};
|
|
315
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A small, forgiving HTML parser: enough for rendered markup.
|
|
6
|
+
* @param {string} html
|
|
7
|
+
* @returns {ElementNode}
|
|
8
|
+
*/
|
|
9
|
+
export function parseHtml(html: string): ElementNode;
|
|
10
|
+
/** @param {ElementNode} n @param {string} tag @returns {ElementNode | undefined} */
|
|
11
|
+
export function find(n: ElementNode, tag: string): ElementNode | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* @file A small, forgiving HTML reader for `astryx experimental render
|
|
14
|
+
* --reverse`: enough for rendered markup, plus the helpers that ask it which
|
|
15
|
+
* Astryx component an element came from.
|
|
16
|
+
*
|
|
17
|
+
* @position foundation/render — used by rebuild.mjs and the render subject
|
|
18
|
+
*/
|
|
19
|
+
/** @typedef {{tag: string, attrs: Record<string, string>, children: Node[]}} ElementNode */
|
|
20
|
+
/** @typedef {ElementNode | {text: string}} Node */
|
|
21
|
+
export const VOID: Set<string>;
|
|
22
|
+
export function decode(s: string): string;
|
|
23
|
+
export function isEl(n: Node): n is ElementNode;
|
|
24
|
+
/** Elements whose children React refuses or treats as a value. */
|
|
25
|
+
export const FORM_TAGS: Set<string>;
|
|
26
|
+
export function hasTag(n: ElementNode, tags: Set<string>): boolean;
|
|
27
|
+
export function textOf(n: Node): string;
|
|
28
|
+
export function astryxClass(n: ElementNode): string | undefined;
|
|
29
|
+
export function isAstryxMarkup(html: string): boolean;
|
|
30
|
+
export type ElementNode = {
|
|
31
|
+
tag: string;
|
|
32
|
+
attrs: Record<string, string>;
|
|
33
|
+
children: Node[];
|
|
34
|
+
};
|
|
35
|
+
export type Node = ElementNode | {
|
|
36
|
+
text: string;
|
|
37
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file A small, forgiving HTML reader for `astryx experimental render
|
|
5
|
+
* --reverse`: enough for rendered markup, plus the helpers that ask it which
|
|
6
|
+
* Astryx component an element came from.
|
|
7
|
+
*
|
|
8
|
+
* @position foundation/render — used by rebuild.mjs and the render subject
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** @typedef {{tag: string, attrs: Record<string, string>, children: Node[]}} ElementNode */
|
|
12
|
+
/** @typedef {ElementNode | {text: string}} Node */
|
|
13
|
+
|
|
14
|
+
export const VOID = new Set([
|
|
15
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta',
|
|
16
|
+
'source', 'track', 'wbr',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/** @param {string} s */
|
|
20
|
+
export const decode = s =>
|
|
21
|
+
s
|
|
22
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
|
|
23
|
+
.replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(Number(d)))
|
|
24
|
+
.replace(/"/g, '"')
|
|
25
|
+
.replace(/</g, '<')
|
|
26
|
+
.replace(/>/g, '>')
|
|
27
|
+
.replace(/ /g, '\u00a0')
|
|
28
|
+
.replace(/&/g, '&');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A small, forgiving HTML parser: enough for rendered markup.
|
|
32
|
+
* @param {string} html
|
|
33
|
+
* @returns {ElementNode}
|
|
34
|
+
*/
|
|
35
|
+
export function parseHtml(html) {
|
|
36
|
+
/** @type {ElementNode} */
|
|
37
|
+
const root = {tag: '#root', attrs: {}, children: []};
|
|
38
|
+
/** @type {ElementNode[]} */
|
|
39
|
+
const stack = [root];
|
|
40
|
+
const re = /<!--[\s\S]*?-->|<!doctype[^>]*>|<\/([a-zA-Z][\w:-]*)\s*>|<([a-zA-Z][\w:-]*)((?:\s+[^\s"'>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'>]+))?)*)\s*(\/?)>|([^<]+|<)/gi;
|
|
41
|
+
/** @type {RegExpExecArray | null} */
|
|
42
|
+
let m;
|
|
43
|
+
while ((m = re.exec(html))) {
|
|
44
|
+
const top = stack[stack.length - 1];
|
|
45
|
+
if (m[1]) {
|
|
46
|
+
const tag = m[1].toLowerCase();
|
|
47
|
+
const at = stack.map(n => n.tag).lastIndexOf(tag);
|
|
48
|
+
if (at > 0) stack.length = at;
|
|
49
|
+
} else if (m[2]) {
|
|
50
|
+
const tag = m[2].toLowerCase();
|
|
51
|
+
/** @type {Record<string, string>} */
|
|
52
|
+
const attrs = {};
|
|
53
|
+
for (const a of (m[3] ?? '').matchAll(/([^\s"'>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g)) {
|
|
54
|
+
attrs[a[1].toLowerCase()] = decode(a[2] ?? a[3] ?? a[4] ?? '');
|
|
55
|
+
}
|
|
56
|
+
/** @type {ElementNode} */
|
|
57
|
+
const node = {tag, attrs, children: []};
|
|
58
|
+
top.children.push(node);
|
|
59
|
+
if (tag === 'script' || tag === 'style' || tag === 'textarea' || tag === 'title') {
|
|
60
|
+
const end = html.toLowerCase().indexOf(`</${tag}`, re.lastIndex);
|
|
61
|
+
const stop = end < 0 ? html.length : end;
|
|
62
|
+
node.children.push({text: html.slice(re.lastIndex, stop)});
|
|
63
|
+
re.lastIndex = stop;
|
|
64
|
+
stack.push(node);
|
|
65
|
+
} else if (!VOID.has(tag) && !m[4]) {
|
|
66
|
+
stack.push(node);
|
|
67
|
+
}
|
|
68
|
+
} else if (m[5]) {
|
|
69
|
+
top.children.push({text: decode(m[5])});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return root;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @param {Node} n @returns {n is ElementNode} */
|
|
76
|
+
export const isEl = n => 'tag' in n;
|
|
77
|
+
|
|
78
|
+
/** Elements whose children React refuses or treats as a value. */
|
|
79
|
+
export const FORM_TAGS = new Set(['input', 'textarea', 'select']);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Whether an element draws one of `tags` itself, not inside another Astryx
|
|
83
|
+
* component it contains.
|
|
84
|
+
* @param {ElementNode} n @param {Set<string>} tags @returns {boolean}
|
|
85
|
+
*/
|
|
86
|
+
export const hasTag = (n, tags) =>
|
|
87
|
+
tags.has(n.tag) ||
|
|
88
|
+
n.children.some(c => isEl(c) && !astryxClass(c) && hasTag(c, tags));
|
|
89
|
+
|
|
90
|
+
/** @param {ElementNode} n @param {string} tag @returns {ElementNode | undefined} */
|
|
91
|
+
export function find(n, tag) {
|
|
92
|
+
for (const c of n.children) {
|
|
93
|
+
if (!isEl(c)) continue;
|
|
94
|
+
if (c.tag === tag) return c;
|
|
95
|
+
const f = find(c, tag);
|
|
96
|
+
if (f) return f;
|
|
97
|
+
}
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {Node} n @returns {string} */
|
|
102
|
+
export const textOf = n =>
|
|
103
|
+
isEl(n)
|
|
104
|
+
? n.tag === 'script' || n.tag === 'style'
|
|
105
|
+
? ''
|
|
106
|
+
: n.children.map(textOf).join('')
|
|
107
|
+
: n.text;
|
|
108
|
+
|
|
109
|
+
/** @param {ElementNode} n */
|
|
110
|
+
export const astryxClass = n =>
|
|
111
|
+
(n.attrs.class ?? '')
|
|
112
|
+
.split(/\s+/)
|
|
113
|
+
.find(c => /^astryx-[a-z0-9]+(?:-[a-z0-9]+)*$/.test(c))
|
|
114
|
+
?.slice('astryx-'.length);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Whether markup was rendered from Astryx components.
|
|
118
|
+
* @param {string} html
|
|
119
|
+
*/
|
|
120
|
+
export const isAstryxMarkup = html => /\sclass="(?:[^"]*\s)?astryx-[a-z0-9]/.test(html);
|
|
121
|
+
|