@astryxdesign/cli 0.7.4 → 0.7.5-canary.1093026
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/discover/_adapter.d.mts +1 -1
- 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/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/_result.d.mts +5 -5
- package/api/search/search.d.mts +1 -1
- package/api/template/template.type.d.mts +2 -1
- package/api/template/template.type.mjs +1 -1
- 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 +15 -0
- package/foundation/doc-compiler/tree.mjs +3 -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/package.json +14 -9
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Worker for `astryx experimental render`: loads one page module from
|
|
5
|
+
* the project (TypeScript, JSX and StyleX compiled in memory) and renders its
|
|
6
|
+
* default export to static markup with the project's own React.
|
|
7
|
+
*
|
|
8
|
+
* It runs in a worker thread so a page that never finishes rendering can be
|
|
9
|
+
* stopped, and so nothing the page prints reaches the CLI's own output.
|
|
10
|
+
*
|
|
11
|
+
* @input workerData {entry, root}
|
|
12
|
+
* @output one message: {ok: true, markup, css, files, texts, logs} or
|
|
13
|
+
* {ok: false, error: {code, message, detail?}}
|
|
14
|
+
* @position foundation/render — started by render-page.mjs
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import * as fs from 'node:fs';
|
|
18
|
+
import * as path from 'node:path';
|
|
19
|
+
import {createRequire} from 'node:module';
|
|
20
|
+
import {format} from 'node:util';
|
|
21
|
+
import {parentPort, workerData} from 'node:worker_threads';
|
|
22
|
+
import {transformSync} from '@babel/core';
|
|
23
|
+
import {createJiti} from 'jiti';
|
|
24
|
+
import {assetDataUri, isAssetPath} from './assets.mjs';
|
|
25
|
+
|
|
26
|
+
/** @typedef {{level: string, text: string}} PageLog */
|
|
27
|
+
|
|
28
|
+
/** @type {{entry: string, root: string}} */
|
|
29
|
+
const {entry, root} = workerData;
|
|
30
|
+
const ROOT_PREFIX = root.endsWith(path.sep) ? root : root + path.sep;
|
|
31
|
+
|
|
32
|
+
/** @type {PageLog[]} */
|
|
33
|
+
const logs = [];
|
|
34
|
+
for (const level of /** @type {const} */ (['log', 'info', 'warn', 'error', 'debug'])) {
|
|
35
|
+
console[level] = (/** @type {unknown[]} */ ...args) => {
|
|
36
|
+
const text = format(...args);
|
|
37
|
+
// The compiler's own notes are not the page's output.
|
|
38
|
+
if (text.startsWith('[BABEL]')) return;
|
|
39
|
+
logs.push({level, text});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const requireFromCli = createRequire(import.meta.url);
|
|
44
|
+
const stylexPlugin = /** @type {any} */ (requireFromCli('@stylexjs/babel-plugin'));
|
|
45
|
+
|
|
46
|
+
/** StyleX rules collected from every local module the page loads. */
|
|
47
|
+
/** @type {unknown[]} */
|
|
48
|
+
const stylexRules = [];
|
|
49
|
+
/** CSS text of local stylesheets the page imports, in load order. */
|
|
50
|
+
/** @type {{file: string, css: string}[]} */
|
|
51
|
+
const importedCss = [];
|
|
52
|
+
/** Every local (non-package) module the page loaded, page first. */
|
|
53
|
+
/** @type {string[]} */
|
|
54
|
+
const localFiles = [];
|
|
55
|
+
/** Package stylesheets the page imports, by specifier and importing file. */
|
|
56
|
+
/** @type {{spec: string, from: string}[]} */
|
|
57
|
+
const packageCss = [];
|
|
58
|
+
/** Sass and Less stylesheets the page imports, which a render cannot compile. @type {string[]} */
|
|
59
|
+
const skippedCss = [];
|
|
60
|
+
/** Local files inlined from asset imports. */
|
|
61
|
+
/** @type {Set<string>} */
|
|
62
|
+
const assetsInlined = new Set();
|
|
63
|
+
/** Packages the page's own modules import from. */
|
|
64
|
+
/** @type {Set<string>} */
|
|
65
|
+
const packages = new Set();
|
|
66
|
+
/** Static JSX text per element, for the dropped-children warning. */
|
|
67
|
+
/** @type {{file: string, component: string, text: string, hidden: boolean, fn: string, line: number}[]} */
|
|
68
|
+
const texts = [];
|
|
69
|
+
/** Every static JSX text, by the function it is in. */
|
|
70
|
+
/** @type {Record<string, string[]>} */
|
|
71
|
+
const fnTexts = {};
|
|
72
|
+
|
|
73
|
+
/** Components that show their content only on demand. */
|
|
74
|
+
const HIDING =
|
|
75
|
+
/Dialog|Modal|Popover|Tooltip|Menu|Drawer|Sheet|Tab|Collapsible|Accordion|Disclosure|Overlay|Layer|Toast|Hover|Select|Combobox|Typeahead|Portal|Suspense|Lazy/;
|
|
76
|
+
|
|
77
|
+
/** @param {any} el whether a JSX element has an open/show-style prop */
|
|
78
|
+
const hasShowAttr = el =>
|
|
79
|
+
el.openingElement.attributes.some(
|
|
80
|
+
(/** @type {any} */ a) =>
|
|
81
|
+
a.type === 'JSXAttribute' &&
|
|
82
|
+
/^(open|isOpen|defaultOpen|show|isShown|visible|isVisible|hidden|expanded|isExpanded|defaultExpanded|collapsed|isCollapsed|defaultCollapsed|collapsible|when|if|max[A-Z]\w*|limit)$/.test(
|
|
83
|
+
a.name?.name,
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
/** @param {any} node a JSX element's name, as written */
|
|
88
|
+
const jsxName = node => {
|
|
89
|
+
const n = node.openingElement.name;
|
|
90
|
+
return n.type === 'JSXIdentifier'
|
|
91
|
+
? n.name
|
|
92
|
+
: n.type === 'JSXMemberExpression'
|
|
93
|
+
? `${n.object.name}.${n.property.name}`
|
|
94
|
+
: '';
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** @type {Record<string, string>} */
|
|
98
|
+
const ENTITIES = {
|
|
99
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0',
|
|
100
|
+
rsquo: '\u2019', lsquo: '\u2018', rdquo: '\u201d', ldquo: '\u201c',
|
|
101
|
+
mdash: '\u2014', ndash: '\u2013', hellip: '\u2026', middot: '\u00b7',
|
|
102
|
+
copy: '\u00a9', reg: '\u00ae', trade: '\u2122', times: '\u00d7', rarr: '\u2192', larr: '\u2190',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** @param {any} node static JSX text as it renders, whitespace collapsed */
|
|
106
|
+
const cleanText = node =>
|
|
107
|
+
node.type === 'JSXText'
|
|
108
|
+
? String(node.extra?.raw ?? node.value)
|
|
109
|
+
.replace(/&#x([0-9a-f]+);/gi, (/** @type {string} */ _, h) => String.fromCodePoint(parseInt(h, 16)))
|
|
110
|
+
.replace(/&#(\d+);/g, (/** @type {string} */ _, d) => String.fromCodePoint(Number(d)))
|
|
111
|
+
.replace(/&([a-z]+);/gi, (/** @type {string} */ whole, e) => ENTITIES[e] ?? whole)
|
|
112
|
+
.replace(/\s+/g, ' ')
|
|
113
|
+
.trim()
|
|
114
|
+
: '';
|
|
115
|
+
|
|
116
|
+
/** @param {any} p the function a JSX node is in, as a key */
|
|
117
|
+
const fnKey = p => {
|
|
118
|
+
const fn = p.getFunctionParent();
|
|
119
|
+
return `${p.hub?.file?.opts?.filename ?? ''}:${fn ? fn.node.start : 'module'}`;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
|
|
123
|
+
|
|
124
|
+
/** A module of the project itself: inside its folder, not a package.
|
|
125
|
+
* @param {string} file */
|
|
126
|
+
const isLocal = file =>
|
|
127
|
+
!file.includes(NODE_MODULES) && (file === entry || file.startsWith(ROOT_PREFIX));
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Babel plugin: replace asset imports with data: URIs and stylesheet imports
|
|
131
|
+
* with nothing (their CSS is inlined into the page), and record static text
|
|
132
|
+
* children of capitalized JSX elements.
|
|
133
|
+
* @param {string} filename
|
|
134
|
+
*/
|
|
135
|
+
function pagePlugin(filename) {
|
|
136
|
+
const dir = path.dirname(filename);
|
|
137
|
+
return (/** @type {any} */ {types: t}) => ({
|
|
138
|
+
visitor: {
|
|
139
|
+
ImportDeclaration(/** @type {any} */ p) {
|
|
140
|
+
const spec = /** @type {string} */ (p.node.source.value);
|
|
141
|
+
const raw = spec.split('?')[0];
|
|
142
|
+
// A stylesheet, image or JSON file imported through a project alias
|
|
143
|
+
// (`@/styles/site.css`) resolves the way modules do.
|
|
144
|
+
const bare =
|
|
145
|
+
isAliased(raw) && (/\.(css|scss|sass|less|json)$/i.test(raw) || isAssetPath(raw)) ? resolveAlias(raw) : raw;
|
|
146
|
+
if (!bare.startsWith('.') && !path.isAbsolute(bare) && !isAliased(bare)) {
|
|
147
|
+
packages.add(
|
|
148
|
+
bare.startsWith('@')
|
|
149
|
+
? bare.split('/').slice(0, 2).join('/')
|
|
150
|
+
: bare.split('/')[0],
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (/\.json$/i.test(bare) && (bare.startsWith('.') || path.isAbsolute(bare))) {
|
|
154
|
+
const file = path.resolve(dir, bare);
|
|
155
|
+
if (fs.existsSync(file) && !localFiles.includes(file)) localFiles.push(file);
|
|
156
|
+
}
|
|
157
|
+
if (/\.(css|scss|sass|less)$/i.test(bare) && (/\.module\.[a-z]+$/i.test(bare) || p.node.specifiers.length > 0)) {
|
|
158
|
+
// CSS Modules (any stylesheet imported as a value) give class names
|
|
159
|
+
// at build time; a render has none.
|
|
160
|
+
const err = new Error(
|
|
161
|
+
`${path.basename(filename)} imports ${bare} as a value (CSS Modules), which a render does not support: use StyleX or a plain stylesheet imported for its side effect.`,
|
|
162
|
+
);
|
|
163
|
+
/** @type {any} */ (err).renderCode = 'ERR_RENDER_FAILED';
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
if (/\.(scss|sass|less)$/i.test(bare)) {
|
|
167
|
+
// Sass and Less need their compiler: say so rather than drop them.
|
|
168
|
+
skippedCss.push(bare);
|
|
169
|
+
p.remove();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (/\.css$/i.test(bare)) {
|
|
173
|
+
if (bare.startsWith('.') || path.isAbsolute(bare)) {
|
|
174
|
+
const file = path.resolve(dir, bare);
|
|
175
|
+
// A stylesheet two modules import is inlined once, where it was
|
|
176
|
+
// first imported, as bundlers do.
|
|
177
|
+
if (!fs.existsSync(file)) {
|
|
178
|
+
const err = new Error(`${path.basename(filename)} imports ${spec}, which does not exist.`);
|
|
179
|
+
/** @type {any} */ (err).renderCode = 'ERR_FILE_NOT_FOUND';
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
if (/\.css$/i.test(bare) && fs.existsSync(file) && !importedCss.some(c => c.file === file)) {
|
|
183
|
+
importedCss.push({file, css: fs.readFileSync(file, 'utf8')});
|
|
184
|
+
if (!localFiles.includes(file)) localFiles.push(file);
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
// A package stylesheet (a theme's, say): the caller resolves it
|
|
188
|
+
// from this file and inlines the rules the page uses.
|
|
189
|
+
packageCss.push({spec: bare, from: filename});
|
|
190
|
+
}
|
|
191
|
+
p.remove();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (isAssetPath(bare) && (bare.startsWith('.') || path.isAbsolute(bare))) {
|
|
195
|
+
const file = path.resolve(dir, bare);
|
|
196
|
+
const uri = assetDataUri(file);
|
|
197
|
+
assetsInlined.add(file);
|
|
198
|
+
if (!localFiles.includes(file)) localFiles.push(file);
|
|
199
|
+
const local = p.node.specifiers.find(
|
|
200
|
+
(/** @type {any} */ s) => s.type === 'ImportDefaultSpecifier',
|
|
201
|
+
);
|
|
202
|
+
if (!local) {
|
|
203
|
+
p.remove();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
p.replaceWith(
|
|
207
|
+
t.variableDeclaration('const', [
|
|
208
|
+
t.variableDeclarator(t.identifier(local.local.name), t.stringLiteral(uri)),
|
|
209
|
+
]),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
JSXText(/** @type {any} */ p) {
|
|
214
|
+
const v = cleanText(p.node);
|
|
215
|
+
if (v.length >= 2) (fnTexts[fnKey(p)] ??= []).push(v);
|
|
216
|
+
},
|
|
217
|
+
JSXElement(/** @type {any} */ p) {
|
|
218
|
+
const component = jsxName(p.node);
|
|
219
|
+
if (!/^[A-Z]/.test(component)) return;
|
|
220
|
+
// Only children that are all static text: the whole text must show.
|
|
221
|
+
if (p.node.children.some((/** @type {any} */ c) => c.type !== 'JSXText')) return;
|
|
222
|
+
const own = p.node.children.map(cleanText).filter(Boolean).join(' ');
|
|
223
|
+
if (!own) return;
|
|
224
|
+
// Content inside something that shows it only on demand (a closed
|
|
225
|
+
// dialog, an inactive tab) is not expected in a static render.
|
|
226
|
+
let hidden = HIDING.test(component) || hasShowAttr(p.node);
|
|
227
|
+
for (let up = p.parentPath; up && !hidden; up = up.parentPath) {
|
|
228
|
+
if (!up.isJSXElement()) continue;
|
|
229
|
+
hidden = HIDING.test(jsxName(up.node)) || hasShowAttr(up.node);
|
|
230
|
+
}
|
|
231
|
+
// Content shown only under a condition, or passed as a prop.
|
|
232
|
+
let conditional = false;
|
|
233
|
+
for (let up = p.parentPath; up && !up.isFunction() && !up.isProgram(); up = up.parentPath) {
|
|
234
|
+
if (
|
|
235
|
+
up.isConditionalExpression() ||
|
|
236
|
+
up.isLogicalExpression() ||
|
|
237
|
+
up.isIfStatement() ||
|
|
238
|
+
up.isSwitchCase() ||
|
|
239
|
+
up.isJSXAttribute()
|
|
240
|
+
) {
|
|
241
|
+
conditional = true;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
// JSX kept in a variable, object or list shows only where it is used.
|
|
246
|
+
if (!conditional) {
|
|
247
|
+
let top = p;
|
|
248
|
+
while (top.parentPath && (top.parentPath.isJSXElement() || top.parentPath.isJSXFragment() || top.parentPath.isJSXExpressionContainer() || top.parentPath.isParenthesizedExpression?.())) {
|
|
249
|
+
top = top.parentPath;
|
|
250
|
+
}
|
|
251
|
+
const holder = top.parentPath;
|
|
252
|
+
conditional = Boolean(
|
|
253
|
+
holder &&
|
|
254
|
+
!holder.isReturnStatement() &&
|
|
255
|
+
!(holder.isArrowFunctionExpression() && holder.node.body === top.node),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
// Content after an early return runs only when that return did not.
|
|
259
|
+
if (!conditional) {
|
|
260
|
+
const fn = p.getFunctionParent();
|
|
261
|
+
const stmt = fn
|
|
262
|
+
? p.findParent((/** @type {any} */ x) => x.parentPath?.node === fn.node.body)
|
|
263
|
+
: null;
|
|
264
|
+
for (const sib of stmt?.getAllPrevSiblings?.() ?? []) {
|
|
265
|
+
let returns = sib.isReturnStatement();
|
|
266
|
+
if (!returns && (sib.isIfStatement() || sib.isSwitchStatement() || sib.isTryStatement())) {
|
|
267
|
+
sib.traverse({
|
|
268
|
+
ReturnStatement() {
|
|
269
|
+
returns = true;
|
|
270
|
+
},
|
|
271
|
+
Function(/** @type {any} */ q) {
|
|
272
|
+
q.skip();
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (returns) {
|
|
277
|
+
conditional = true;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
texts.push({
|
|
283
|
+
file: filename,
|
|
284
|
+
component,
|
|
285
|
+
text: own,
|
|
286
|
+
hidden: hidden || conditional,
|
|
287
|
+
fn: fnKey(p),
|
|
288
|
+
line: p.node.loc?.start.line ?? 0,
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Path aliases from the project's tsconfig.json or jsconfig.json
|
|
297
|
+
* (`compilerOptions.paths`), as jiti aliases: `"@/*": ["./src/*"]` becomes
|
|
298
|
+
* `{"@/": "<root>/src/"}`.
|
|
299
|
+
* @returns {Record<string, string>}
|
|
300
|
+
*/
|
|
301
|
+
function projectAliases() {
|
|
302
|
+
/** @type {Record<string, string>} */
|
|
303
|
+
const alias = {};
|
|
304
|
+
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
|
305
|
+
const file = path.join(root, name);
|
|
306
|
+
if (!fs.existsSync(file)) continue;
|
|
307
|
+
/** @type {any} */
|
|
308
|
+
let config;
|
|
309
|
+
try {
|
|
310
|
+
// Allow the comments and trailing commas these files often carry.
|
|
311
|
+
const text = fs
|
|
312
|
+
.readFileSync(file, 'utf8')
|
|
313
|
+
.replace(/("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, str) => str ?? '')
|
|
314
|
+
.replace(/,(\s*[}\]])/g, '$1');
|
|
315
|
+
config = JSON.parse(text);
|
|
316
|
+
} catch {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const options = config?.compilerOptions ?? {};
|
|
320
|
+
const baseDir = path.resolve(root, typeof options.baseUrl === 'string' ? options.baseUrl : '.');
|
|
321
|
+
for (const [key, targets] of Object.entries(options.paths ?? {})) {
|
|
322
|
+
const target = Array.isArray(targets) ? targets[0] : undefined;
|
|
323
|
+
if (typeof target !== 'string') continue;
|
|
324
|
+
if (key.endsWith('/*') && target.endsWith('/*')) {
|
|
325
|
+
alias[key.slice(0, -1)] = `${path.resolve(baseDir, target.slice(0, -2))}/`;
|
|
326
|
+
} else if (!key.includes('*') && !target.includes('*')) {
|
|
327
|
+
alias[key] = path.resolve(baseDir, target);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
return alias;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const ALIASES = projectAliases();
|
|
336
|
+
|
|
337
|
+
/** @param {string} spec whether a specifier goes through a project alias */
|
|
338
|
+
const isAliased = spec =>
|
|
339
|
+
Object.keys(ALIASES).some(key => (key.endsWith('/') ? spec.startsWith(key) : spec === key));
|
|
340
|
+
|
|
341
|
+
/** @param {string} spec a specifier through a project alias, as an absolute path */
|
|
342
|
+
const resolveAlias = spec => {
|
|
343
|
+
for (const [key, target] of Object.entries(ALIASES)) {
|
|
344
|
+
if (key.endsWith('/') ? spec.startsWith(key) : spec === key) {
|
|
345
|
+
return key.endsWith('/') ? target + spec.slice(key.length) : target;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return spec;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const plain = createJiti(entry, {fsCache: false, jsx: {runtime: 'automatic'}});
|
|
352
|
+
|
|
353
|
+
const jiti = createJiti(entry, {
|
|
354
|
+
fsCache: false,
|
|
355
|
+
tryNative: false,
|
|
356
|
+
extensions: ['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx', '.mts', '.cts', '.json'],
|
|
357
|
+
moduleCache: true,
|
|
358
|
+
interopDefault: true,
|
|
359
|
+
alias: ALIASES,
|
|
360
|
+
jsx: {runtime: 'automatic'},
|
|
361
|
+
transform(opts) {
|
|
362
|
+
const filename = opts.filename ?? '';
|
|
363
|
+
let source = opts.source;
|
|
364
|
+
if (filename && isLocal(filename) && /\.json$/i.test(filename) && !localFiles.includes(filename)) {
|
|
365
|
+
localFiles.push(filename);
|
|
366
|
+
}
|
|
367
|
+
if (filename && isLocal(filename) && /\.(tsx?|jsx?|mjs|cjs|mts|cts)$/.test(filename)) {
|
|
368
|
+
if (!localFiles.includes(filename)) localFiles.push(filename);
|
|
369
|
+
const ts = /\.(tsx?|mts|cts)$/.test(filename);
|
|
370
|
+
const result = transformSync(source, {
|
|
371
|
+
filename,
|
|
372
|
+
babelrc: false,
|
|
373
|
+
configFile: false,
|
|
374
|
+
sourceType: 'module',
|
|
375
|
+
parserOpts: {plugins: ts ? ['jsx', 'typescript'] : ['jsx']},
|
|
376
|
+
generatorOpts: {retainLines: true},
|
|
377
|
+
compact: false,
|
|
378
|
+
plugins: [
|
|
379
|
+
pagePlugin(filename),
|
|
380
|
+
...(source.includes('stylex')
|
|
381
|
+
? [
|
|
382
|
+
[
|
|
383
|
+
stylexPlugin,
|
|
384
|
+
{
|
|
385
|
+
dev: false,
|
|
386
|
+
runtimeInjection: false,
|
|
387
|
+
treeshakeCompensation: true,
|
|
388
|
+
unstable_moduleResolution: {type: 'commonJS', rootDir: root},
|
|
389
|
+
},
|
|
390
|
+
],
|
|
391
|
+
]
|
|
392
|
+
: []),
|
|
393
|
+
],
|
|
394
|
+
});
|
|
395
|
+
const rules = /** @type {any} */ (result?.metadata)?.stylex;
|
|
396
|
+
if (Array.isArray(rules)) stylexRules.push(...rules);
|
|
397
|
+
source = result?.code ?? source;
|
|
398
|
+
}
|
|
399
|
+
return {code: plain.transform({...opts, source, babel: {...opts.babel, compact: false}})};
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* @param {string} code
|
|
405
|
+
* @param {string} message
|
|
406
|
+
* @param {Record<string, unknown>} [detail]
|
|
407
|
+
*/
|
|
408
|
+
function fail(code, message, detail) {
|
|
409
|
+
parentPort?.postMessage({ok: false, error: {code, message, detail}, logs});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** @param {unknown} e */
|
|
413
|
+
function classify(e) {
|
|
414
|
+
const err = /** @type {any} */ (e);
|
|
415
|
+
if (err?.renderCode) return fail(err.renderCode, String(err.message).replace(/^\/[^:\n]*: /, ''));
|
|
416
|
+
// Babel prefixes errors raised while compiling with the file's path.
|
|
417
|
+
const message = String(err?.message ?? err).replace(/^\/[^:\n]*: /, '');
|
|
418
|
+
if (/suspended while responding to synchronous input|A component suspended/.test(message)) {
|
|
419
|
+
return fail(
|
|
420
|
+
'ERR_RENDER_FAILED',
|
|
421
|
+
'The page waits for data while rendering (a component suspended), and a static render cannot wait. Load the data first, or render a version of the page that has it.',
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
const missing =
|
|
425
|
+
/Cannot find (?:module|package) ['"]([^'"]+)['"]/.exec(message) ??
|
|
426
|
+
/Failed to resolve import ['"]([^'"]+)['"]/.exec(message);
|
|
427
|
+
if (missing && (isAliased(missing[1]) || /^[@~#]\/|^#/.test(missing[1]))) {
|
|
428
|
+
return fail(
|
|
429
|
+
'ERR_FILE_NOT_FOUND',
|
|
430
|
+
`The page imports ${missing[1]} through a path alias that does not lead to a file. Check \`compilerOptions.paths\` in the project's tsconfig.json, or import it by a relative path.`,
|
|
431
|
+
{module: missing[1]},
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
if (missing && !missing[1].startsWith('.') && !path.isAbsolute(missing[1])) {
|
|
435
|
+
const spec = missing[1];
|
|
436
|
+
const pkg = spec.startsWith('@') ? spec.split('/').slice(0, 2).join('/') : spec.split('/')[0];
|
|
437
|
+
return fail('ERR_RENDER_MISSING_DEPENDENCY', `The page imports ${pkg}, which is not installed`, {package: pkg});
|
|
438
|
+
}
|
|
439
|
+
if (missing) {
|
|
440
|
+
return fail('ERR_FILE_NOT_FOUND', `The page imports ${missing[1]}, which does not exist`, {module: missing[1]});
|
|
441
|
+
}
|
|
442
|
+
if (err?.code === 'BABEL_PARSE_ERROR' || err?.reasonCode || /SyntaxError|Unexpected token/.test(`${err?.name} ${message}`)) {
|
|
443
|
+
const loc = err?.loc ? `:${err.loc.line}:${err.loc.column + 1}` : '';
|
|
444
|
+
return fail('ERR_RENDER_COMPILE', `The page does not compile${loc ? ` (line ${err.loc.line})` : ''}: ${message.split('\n')[0]}`, {
|
|
445
|
+
...(err?.loc ? {line: err.loc.line, column: err.loc.column + 1} : {}),
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
const browser = /\b(window|document|localStorage|sessionStorage|navigator|location|matchMedia|requestAnimationFrame|IntersectionObserver|ResizeObserver|HTMLElement|customElements) is not defined/.exec(message);
|
|
449
|
+
if (browser) {
|
|
450
|
+
return fail(
|
|
451
|
+
'ERR_RENDER_BROWSER_API',
|
|
452
|
+
`The page uses \`${browser[1]}\` while rendering, and a rendered page has no browser yet. Read it inside useEffect, or guard it with typeof ${browser[1]} !== 'undefined'.`,
|
|
453
|
+
{api: browser[1]},
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
if (err?.code === 'ERR_RENDER_ASSET_TOO_LARGE' || err?.code === 'ERR_FILE_NOT_FOUND') {
|
|
457
|
+
return fail(err.code, message, err.detail);
|
|
458
|
+
}
|
|
459
|
+
return fail('ERR_RENDER_FAILED', `The page threw while rendering: ${message.split('\n')[0]}`);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
try {
|
|
463
|
+
/** @type {any} */
|
|
464
|
+
let mod;
|
|
465
|
+
try {
|
|
466
|
+
mod = await jiti.import(entry);
|
|
467
|
+
} catch (e) {
|
|
468
|
+
classify(e);
|
|
469
|
+
throw null;
|
|
470
|
+
}
|
|
471
|
+
const Page = mod?.default ?? (typeof mod === 'function' ? mod : undefined);
|
|
472
|
+
if (typeof Page !== 'function' && typeof Page?.$$typeof !== 'symbol') {
|
|
473
|
+
fail(
|
|
474
|
+
'ERR_RENDER_NO_DEFAULT_EXPORT',
|
|
475
|
+
'The page has no default export to render. Export the page component: export default function Page() { … }',
|
|
476
|
+
);
|
|
477
|
+
throw null;
|
|
478
|
+
}
|
|
479
|
+
/** @type {any} */
|
|
480
|
+
let React;
|
|
481
|
+
/** @type {any} */
|
|
482
|
+
let server;
|
|
483
|
+
try {
|
|
484
|
+
React = await jiti.import('react');
|
|
485
|
+
server = await jiti.import('react-dom/server');
|
|
486
|
+
} catch (e) {
|
|
487
|
+
classify(e);
|
|
488
|
+
throw null;
|
|
489
|
+
}
|
|
490
|
+
/** @type {string} */
|
|
491
|
+
let markup;
|
|
492
|
+
try {
|
|
493
|
+
markup = server.renderToStaticMarkup(React.createElement(Page));
|
|
494
|
+
} catch (e) {
|
|
495
|
+
classify(e);
|
|
496
|
+
throw null;
|
|
497
|
+
}
|
|
498
|
+
/** @type {string} */
|
|
499
|
+
let css = '';
|
|
500
|
+
if (stylexRules.length > 0) {
|
|
501
|
+
css = stylexPlugin.processStylexRules(stylexRules, {useLayers: false});
|
|
502
|
+
}
|
|
503
|
+
parentPort?.postMessage({
|
|
504
|
+
ok: true,
|
|
505
|
+
markup,
|
|
506
|
+
css,
|
|
507
|
+
importedCss,
|
|
508
|
+
packages: [...packages],
|
|
509
|
+
packageCss,
|
|
510
|
+
skippedCss,
|
|
511
|
+
assets: [...assetsInlined],
|
|
512
|
+
files: localFiles,
|
|
513
|
+
texts,
|
|
514
|
+
fnTexts,
|
|
515
|
+
logs,
|
|
516
|
+
});
|
|
517
|
+
} catch (e) {
|
|
518
|
+
if (e !== null) classify(e);
|
|
519
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
* Rebuild a page module from Astryx markup.
|
|
6
|
+
* @param {string} html
|
|
7
|
+
* @param {string} fromDir where to find the installed packages
|
|
8
|
+
* @returns {{source: string, components: string[], elements: number, unknown: string[], omitted: string[]}}
|
|
9
|
+
*/
|
|
10
|
+
export function rebuildPage(html: string, fromDir: string): {
|
|
11
|
+
source: string;
|
|
12
|
+
components: string[];
|
|
13
|
+
elements: number;
|
|
14
|
+
unknown: string[];
|
|
15
|
+
omitted: string[];
|
|
16
|
+
};
|
|
17
|
+
export type ElementNode = import("./html-reader.mjs").ElementNode;
|
|
18
|
+
export type Node = import("./html-reader.mjs").Node;
|
|
19
|
+
export type ComponentEntry = import("./component-map.mjs").ComponentEntry;
|