@webority/canvas-kit 0.96.0 → 0.98.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -0
- package/dist/build.js +3 -3
- package/dist/drawn.js +4 -4
- package/dist/meta.d.ts +2 -0
- package/dist/metaCheck.js +3 -3
- package/dist/metas.js +2 -2
- package/dist/sourceStamp.d.ts +28 -0
- package/dist/sourceStamp.js +1 -0
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -68,6 +68,7 @@ A meta module imports only `defineMeta` from `@webority/canvas-kit/meta` and exp
|
|
|
68
68
|
| `acceptsChildren` | `true` or `false`. |
|
|
69
69
|
| `props` | Each prop has a `type`, one of `string`, `number`, `stringOrNumber`, `boolean`, `enum`, `enumOrNumber`, `url`, `icon`, `node`, `array`, `object` or `any`, and a `description`. An enum or enumOrNumber lists its `values`; numbers are allowed in an enum, and an enumOrNumber lists its named values only and also takes any number. Optional `default` (never on a required prop), `required`, `fields` on an object and `itemFields` on an array of objects. |
|
|
70
70
|
| `omit` | Optional. Design props deliberately not described. |
|
|
71
|
+
| `actions` | Optional, web only. Handler name to action stand-in prop name, such as `{ onConfirm: "confirmAction" }`. The manifest lists each stand-in as an object prop with no fields and names it in `takesAction`. |
|
|
71
72
|
| `examples` | Each has an `id`, a `title`, `props`, optional `children` and optional `wrap`. The id is the name for the base example and `<name>.<case>` for the others. |
|
|
72
73
|
| `usage` | `when` lines, `whenNot` entries with `text` and optional `instead`, `do` and `dont` pairs of `note` and `example` matched by index, and `a11y` lines. |
|
|
73
74
|
|
|
@@ -97,6 +98,7 @@ covers Web metas; a phone meta is proven by mobile-ui's own type check.
|
|
|
97
98
|
- places a node whose component is unknown;
|
|
98
99
|
- has an empty `when`, `do` and `dont` lists of different lengths, or an `instead` that names an unknown component;
|
|
99
100
|
- has an `icon` outside the kit's icons;
|
|
101
|
+
- has `actions` that are not `on<Event>` names, a stand-in name that is malformed, repeated or also a prop, or `actions` without `web` in `platforms`;
|
|
100
102
|
- has an em or en dash in any string.
|
|
101
103
|
|
|
102
104
|
## Guidance file
|
|
@@ -135,12 +137,66 @@ The build stops with a named fix when:
|
|
|
135
137
|
`readReleaseManifest(dir)` checks every stamped file against `release.json` and returns the stamp and the manifest.
|
|
136
138
|
Any file changed after the build fails the check.
|
|
137
139
|
|
|
140
|
+
## Source stamps
|
|
141
|
+
|
|
142
|
+
`@webority/canvas-kit/source-stamp` writes `data-canvas-at="<repo path>:<line>:<column>"` on every JSX element of the
|
|
143
|
+
product's own files, so Webority Canvas can link a board node to the line that renders it (for example
|
|
144
|
+
`Web.Portal/src/pages/Loans.jsx:42:7`, line and column counted from 1). The path starts at the folder that holds
|
|
145
|
+
`.webority/settings.json`. Files under `node_modules` and fragments get no stamp, and an element that already has the
|
|
146
|
+
attribute keeps it. An App* component writes the stamp it was given on its root (the element with `data-wui`).
|
|
147
|
+
|
|
148
|
+
`sourceStampPlugins({ environment })` takes the app's compiled `ENVIRONMENT` (Local, Development, Staging or
|
|
149
|
+
Production). It returns the stamp plugin in every environment except Production, and no plugin in Production. Any
|
|
150
|
+
other value, or none, throws. Never pass `NODE_ENV`.
|
|
151
|
+
|
|
152
|
+
Web portal (Vite 8 with `@vitejs/plugin-react` 6, which has no Babel option): add `@rolldown/plugin-babel` 0.2.4 and
|
|
153
|
+
`@babel/core` 7.29.7 as exact dev dependencies, then in `vite.config.js`:
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
import { readFileSync } from "node:fs";
|
|
157
|
+
import babel from "@rolldown/plugin-babel";
|
|
158
|
+
import react from "@vitejs/plugin-react";
|
|
159
|
+
import { sourceStampPlugins } from "@webority/canvas-kit/source-stamp";
|
|
160
|
+
import { defineConfig } from "vite";
|
|
161
|
+
|
|
162
|
+
// Written by scripts/prebuild.js before every dev and build run.
|
|
163
|
+
const { ENVIRONMENT } = JSON.parse(readFileSync(new URL("./src/appsettings.Compiled.json", import.meta.url), "utf8"));
|
|
164
|
+
|
|
165
|
+
export default defineConfig({
|
|
166
|
+
plugins: [babel({ plugins: sourceStampPlugins({ environment: ENVIRONMENT }) }), react()],
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Phone app (Metro): in `babel.config.js`, before any plugin that must come last (such as `react-native-worklets/plugin`):
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
const { sourceStampPlugins } = require("@webority/canvas-kit/source-stamp");
|
|
174
|
+
// Written by mobile-tools prebuild before every run and build.
|
|
175
|
+
const { ENVIRONMENT } = require("./appsettings.Compiled.json");
|
|
176
|
+
|
|
177
|
+
module.exports = (api) => {
|
|
178
|
+
api.cache(true);
|
|
179
|
+
return {
|
|
180
|
+
presets: ["module:@react-native/babel-preset"],
|
|
181
|
+
plugins: [...sourceStampPlugins({ environment: ENVIRONMENT }), "react-native-worklets/plugin"],
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Metro caches each file's transform and does not see the environment, so set `cacheVersion: ENVIRONMENT` (read the same
|
|
187
|
+
way) in `metro.config.js`; a Production bundle then never reuses a stamped Staging transform. On a device the stamp
|
|
188
|
+
stays on the React element, where the Canvas phone reader finds it; the native view does not receive it.
|
|
189
|
+
|
|
190
|
+
The Production build is checked by the deploy-guard gate in gh-actions (`production: true`, kinds `spa` and `mobile`) and
|
|
191
|
+
by `release-mobile.js`, not by a canvas-kit command.
|
|
192
|
+
|
|
138
193
|
## Exports
|
|
139
194
|
|
|
140
195
|
- `.` (Node only): `kitModuleVersion`, `KIT_MODULE_EXTERNALS`, `defineKit`, `readManifest` (async), `readReleaseManifest(dir)`, `buildKitRelease`, `readConfig`, `readMetas`, `HTML_TAGS`, `checkMetaTypes`, `guidanceOf`, `writeGuidance`.
|
|
141
196
|
- `./meta`: `defineMeta` and the meta types (`ComponentMeta`, `MetaProp`, `MetaNode`, `MetaExample`, `MetaPractice`, `ComponentUsage`).
|
|
142
197
|
- `./kitModule`: the kit module contract alone.
|
|
143
198
|
- `./browser`: for the renderers, with no Node built-ins. `loadKit({ kitKey, version, baseUrl })` loads the release at `<baseUrl>/<kitKey>/<version>` and returns its kit module, for a fleet kit and a product kit alike.
|
|
199
|
+
- `./source-stamp` (Node only, for build configs): `sourceStampPlugins({ environment })` and `SOURCE_STAMP_ATTRIBUTE`.
|
|
144
200
|
|
|
145
201
|
## Tests
|
|
146
202
|
|
package/dist/build.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{findPackageDir as e,readJson as t,toPosix as n}from"./files.js";import{KIT_MODULE_EXTERNALS as r}from"./kitModule.js";import{libraryFiles as i,readManifest as a}from"./manifest.js";import{digest as o,writeReleaseFolder as s}from"./release.js";import{existsSync as c}from"node:fs";import{dirname as
|
|
2
|
-
`)}function
|
|
3
|
-
`);return{script:h.code,styles:
|
|
1
|
+
import{findPackageDir as e,readJson as t,toPosix as n}from"./files.js";import{KIT_MODULE_EXTERNALS as r}from"./kitModule.js";import{libraryFiles as i,readManifest as a}from"./manifest.js";import{digest as o,writeReleaseFolder as s}from"./release.js";import{existsSync as c,realpathSync as l}from"node:fs";import{dirname as u,join as d,resolve as f}from"node:path";import{fileURLToPath as p}from"node:url";import{build as m}from"vite";var h=19,g=[`react-redux`,`react-router-dom`,`axios`],_=[`kit`,`platform`,`libraryPackage`,`components`,`tokens`,`rolesFile`,`styles`,`rendererProtocolVersion`],v=[`kit`,`platform`,`libraryPackage`,`components`,`tokens`,`rendererProtocolVersion`],y=`canvas-kit:entry`,b=`\0${y}`,x=d(u(p(import.meta.url)),`kitModule.js`),S={"process.env.NODE_ENV":JSON.stringify(`production`),"process.env":`{}`,__DEV__:`false`},C=/(?<![\w$."'`])(process\.env|__DEV__)(?![\w$])/;function w(e){let n=t(e);if(typeof n!=`object`||!n||Array.isArray(n))throw Error(`${e} must be a JSON object.`);if(n.entry!==void 0)throw Error(`${e}: A kit lists its components with components, a glob of .meta.js files; entry is retired.`);for(let t of Object.keys(n))if(!_.includes(t))throw Error(`${e} has the unknown key ${t}. The keys are ${_.join(`, `)}.`);for(let t of v)if(n[t]===void 0)throw Error(`${e} needs the key ${t}.`);for(let t of[`kit`,`platform`,`libraryPackage`,`components`,`tokens`])if(typeof n[t]!=`string`)throw Error(`${e}: ${t} must be a string.`);for(let t of[`rolesFile`])if(n[t]!==void 0&&typeof n[t]!=`string`)throw Error(`${e}: ${t} must be a string path.`);if(n.styles!==void 0){if(!Array.isArray(n.styles)||n.styles.some(e=>typeof e!=`string`||e===``))throw Error(`${e}: styles must be a list of style file paths.`);for(let t of n.styles){if(!/\.(css|scss)$/.test(t))throw Error(`${e}: styles names ${t}, which is not a .css or .scss file.`);if(!c(f(u(e),t)))throw Error(`${e}: styles names ${t}, which does not exist.`)}}return n}function T(n){let r=e(n,`react`);if(!r)throw Error(`react is not installed for ${n}. Run pnpm install there.`);let{version:i}=t(d(r,`package.json`));if(Number(String(i).split(`.`)[0])!==h)throw Error(`The product uses React ${i}; the Canvas renderer runs React ${h}. Pin react to ${h}.x and rebuild.`)}function E(e){let{version:n}=t(d(e,`package.json`));if(!n)throw Error(`${d(e,`package.json`)} needs a version; the release is stamped with it.`);return n}function D({styleFiles:e,libraryFile:t,chromeFile:r,manifest:i,libraryNames:a,chromeNames:o,productModules:s}){let c=[...s],l=[...a.map(({fixedProps:e})=>e),...c.map(([,{fixedProps:e}])=>e)].some(e=>e!==void 0),u=e.map(e=>`import ${JSON.stringify(n(e))};`);l&&u.push(`import { createElement } from "react";`),u.push(`import { ${l?`defineKit, withFixedProps`:`defineKit`} } from ${JSON.stringify(n(x))};`),c.forEach(([,{module:e,exportName:t}],r)=>{u.push(`import { ${t} as product${r} } from ${JSON.stringify(n(e))};`)});let d=[...new Set(a.map(({exportName:e})=>e))];d.length>0&&u.push(`import { ${d.join(`, `)} } from ${JSON.stringify(n(t))};`),o.length>0&&u.push(`import { ${o.join(`, `)} } from ${JSON.stringify(n(r))};`);let f=(e,t,n)=>n===void 0?e===t?e:`${e}: ${t}`:`${e}: withFixedProps(createElement, ${t}, ${JSON.stringify(n)}, ${JSON.stringify(e)})`,p=[...a.map(({name:e,exportName:t,fixedProps:n})=>f(e,t,n)),...o,...c.map(([e,{fixedProps:t}],n)=>f(e,`product${n}`,t))],m=Object.fromEntries(i.components.filter(({marker:e})=>e!==null).map(({name:e,marker:t})=>[e,t]));return u.push(`export default defineKit({ components: { ${p.join(`, `)} }, markers: ${JSON.stringify(m)}, tokenValues: ${JSON.stringify(i.tokenValues)}, manifest: ${JSON.stringify(i)} });`,``),u.join(`
|
|
2
|
+
`)}function O(e){return{name:`canvas-kit-entry`,resolveId(e){return e===y?b:null},load(t){return t===b?e:null}}}function k(e){let t=`${n(d(e,`src`,`services`))}/`;return{name:`canvas-kit-banned-imports`,transform(e,r){let i=n(r.split(`?`)[0]),a=g.find(e=>i.includes(`/node_modules/${e}/`));if(a)throw Error(`The kit imports ${a} (${i}). A kit draws from its props only; the container that renders it holds the state and the calls.`);if(i.startsWith(t))throw Error(`The kit imports ${i}, which is in src/services. A kit draws from its props only; move the call into the container.`);return null}}}async function A({root:e,kit:t,platform:n,styleFiles:i,libraryFile:a,chromeFile:o,libraryNames:s,chromeNames:c,productModules:l,manifest:u}){let d=r[n],f=await m({configFile:!1,root:e,logLevel:`warn`,publicDir:!1,envDir:!1,define:S,plugins:[O(D({styleFiles:i,libraryFile:a,chromeFile:o,manifest:u,libraryNames:s,chromeNames:c,productModules:l})),k(e)],esbuild:{jsx:`automatic`,jsxImportSource:`react`,include:/\.[cm]?[jt]sx?$/},build:{write:!1,minify:!0,target:`es2022`,cssCodeSplit:!1,lib:{entry:y,formats:[`es`],fileName:()=>`kit.js`},rollupOptions:{input:y,external:e=>d.some(t=>e===t||e.startsWith(`${t}/`)),output:{codeSplitting:!1}}}}),p=(Array.isArray(f)?f:[f]).flatMap(e=>e.output),h=p.find(e=>e.type===`chunk`&&e.isEntry);if(!h)throw Error(`The bundle of ${t} has no entry module.`);let g=C.exec(h.code);if(g)throw Error(`The bundle of ${t} still reads ${g[1]}, which the renderer does not define.`);let _=p.filter(e=>e.type===`chunk`&&!e.isEntry);if(_.length>0)throw Error(`The bundle of ${t} split ${_.map(e=>e.fileName).join(`, `)} out of kit.js; a kit must be one file.`);let v=p.filter(e=>e.type===`asset`&&e.fileName.endsWith(`.css`)).map(e=>typeof e.source==`string`?e.source:Buffer.from(e.source).toString(`utf8`)).join(`
|
|
3
|
+
`);return{script:h.code,styles:v}}async function j({configFile:e,outDir:t,styleKeysFile:n,phoneRendererSupportFile:r}){let d=f(e),p=f(t);if(c(p))throw Error(`${p} already exists. A release folder is never overwritten; choose a new folder.`);let m=w(d),h=l(u(d));if(m.platform!==`Phone`&&r!==void 0)throw Error(`Only a Phone kit reads --phone-renderer-support; a Web kit takes no phone support file.`);T(h);let g=m.rolesFile===void 0?void 0:f(h,m.rolesFile),{manifest:_,warnings:v,phoneChromeNames:y,productModules:b}=await a({projectRoot:h,kit:m.kit,platform:m.platform,components:m.components,tokens:m.tokens,libraryPackage:m.libraryPackage,rolesFile:g,rendererProtocolVersion:m.rendererProtocolVersion,phoneRendererSupportFile:r===void 0?void 0:f(r),styleKeysFile:n===void 0?void 0:f(n)}),x=i(h,m.libraryPackage),S=E(h),C=_.components.filter(({source:e,name:t})=>e===m.libraryPackage&&!y.includes(t)).map(({name:e,component:t,fixedProps:n})=>({name:e,exportName:t??e,...n===void 0?{}:{fixedProps:n}})),{script:D,styles:O}=await A({root:h,kit:m.kit,platform:m.platform,styleFiles:(m.styles??[]).map(e=>f(h,e)),libraryFile:x.entryFile,chromeFile:x.canvasKitFile,libraryNames:C,chromeNames:y,productModules:b,manifest:_}),k={"manifest.json":`${JSON.stringify(_,null,2)}\n`,"kit.js":D,"kit.css":O},j={name:m.kit,version:S,platform:m.platform,kitModuleVersion:2,rendererProtocolVersion:m.rendererProtocolVersion,library:_.library,files:Object.fromEntries(Object.entries(k).map(([e,t])=>[e,o(t)]))};return s(p,k,j),{outDir:p,release:j,manifest:_,warnings:v}}export{j as buildKitRelease,w as readConfig};
|
package/dist/drawn.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{readJson as e,toPosix as t}from"./files.js";import{TSC as n,specifierOf as r}from"./metaCheck.js";import{existsSync as i,mkdirSync as a,
|
|
2
|
-
`)}\n`),
|
|
3
|
-
`)}`);return[...new Set([...f[1].matchAll(/(\w+): "\w+"/g)].map(e=>e[1]))].sort()}finally{
|
|
4
|
-
`))}export{
|
|
1
|
+
import{readJson as e,toPosix as t}from"./files.js";import{TSC as n,specifierOf as r}from"./metaCheck.js";import{existsSync as i,mkdirSync as a,realpathSync as o,rmSync as s,writeFileSync as c}from"node:fs";import{join as l}from"node:path";import{execFileSync as u}from"node:child_process";var d=`src/components/not-drawn.json`;function f({root:e,typesFile:i}){let d=l(o(e),`node_modules`,`.canvas-kit-exports-${process.pid}`);try{a(d,{recursive:!0});let e=[`import type * as L from "${r(i)}";`,`type IsComponent<T> = 0 extends 1 & T ? true : T extends (...args: never[]) => unknown ? true : T extends abstract new (...args: never[]) => unknown ? true : T extends { $$typeof: symbol } ? true : false;`,`type Named<K> = K extends string ? (K extends Capitalize<K> ? (K extends Uncapitalize<K> ? never : K) : never) : never;`,`type Components = { [K in keyof typeof L]: Named<K> extends never ? never : IsComponent<(typeof L)[K]> extends true ? K : never }[keyof typeof L];`,`export const components: never = null as unknown as { [K in Components]: K };`];c(l(d,`check.ts`),`${e.join(`
|
|
2
|
+
`)}\n`),c(l(d,`tsconfig.json`),JSON.stringify({compilerOptions:{strict:!0,noEmit:!0,allowJs:!0,checkJs:!1,jsx:`react-jsx`,module:`esnext`,moduleResolution:`bundler`,target:`es2022`,skipLibCheck:!0,noErrorTruncation:!0,types:[]},files:[`check.ts`]}));let o=``;try{u(process.execPath,[n,`-p`,l(d,`tsconfig.json`),`--pretty`,`false`],{cwd:d,encoding:`utf8`,stdio:`pipe`})}catch(e){o=String(e.stdout??``)+String(e.stderr??``)}let s=o.split(/\r?\n/).find(e=>/^check\.ts\(5,\d+\): error TS2322: /.test(e)),f=s&&/Type '(\{[^']*\})' is not assignable to type 'never'/.exec(s);if(!f)throw Error(`tsc could not list the components ${t(i)} exports: ${o.split(/\r?\n/).slice(0,20).join(`
|
|
3
|
+
`)}`);return[...new Set([...f[1].matchAll(/(\w+): "\w+"/g)].map(e=>e[1]))].sort()}finally{s(d,{recursive:!0,force:!0})}}function p(t){if(!i(t))return{};let n=e(t);if(typeof n!=`object`||!n||Array.isArray(n))throw Error(`${t} must be an object from component name to the reason the kit does not draw it.`);let r=Object.entries(n).filter(([,e])=>typeof e!=`string`||e.trim()===``).map(([e])=>e);if(r.length>0)throw Error(`${t}: ${r.join(`, `)} need a reason the kit does not draw them.`);return n}function m({libraryPackage:e,exported:t,drawnNames:n,notDrawn:r}){let i=new Set(t),a=[...new Map(n.map(e=>[`${e.name} ${e.component}`,e])).values()],o=new Set(a.map(({component:e})=>e)),s=Object.keys(r),c=[],l=t.filter(e=>!o.has(e)&&!(e in r));l.length>0&&c.push(`${e} exports ${l.join(`, `)} with no meta. Write a meta for each, or list it in ${d} with the reason the kit does not draw it.`);let u=s.filter(e=>o.has(e));u.length>0&&c.push(`${d} lists ${u.join(`, `)}, which have a meta. A component is drawn or not drawn; remove one.`);let f=s.filter(e=>!i.has(e));f.length>0&&c.push(`${d} lists ${f.join(`, `)}, which ${e} does not export as components. Remove them.`);let p=a.filter(({component:e})=>!i.has(e));if(p.length>0){let t=p.map(({name:e,component:t})=>e===t?e:`${e} (draws ${t})`);c.push(`${t.join(`, `)} have a meta, but ${e} does not export them as components.`)}if(c.length>0)throw Error(c.join(`
|
|
4
|
+
`))}export{d as NOT_DRAWN_FILE,m as checkDrawn,f as componentExports,p as readNotDrawn};
|
package/dist/meta.d.ts
CHANGED
|
@@ -80,6 +80,8 @@ export interface ComponentMeta {
|
|
|
80
80
|
platforms: readonly ("web" | "phone")[];
|
|
81
81
|
acceptsChildren: boolean;
|
|
82
82
|
props: Readonly<Record<string, MetaProp>>;
|
|
83
|
+
/** The action stand-in each event handler is drawn as, by handler name, such as { onConfirm: "confirmAction" }. A design cannot send a function, so the manifest lists each stand-in as an object prop and names it in takesAction; in play the handler runs the stored action. Only on a meta whose platforms list web. */
|
|
84
|
+
actions?: Readonly<Record<string, string>>;
|
|
83
85
|
/** Design props deliberately not shown in the kit, such as a render prop no kit type fits. The type check accepts them unlisted. */
|
|
84
86
|
omit?: readonly string[];
|
|
85
87
|
examples: readonly MetaExample[];
|
package/dist/metaCheck.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{toPosix as e}from"./files.js";import{createRequire as t}from"node:module";import{mkdirSync as n,
|
|
2
|
-
`)}\n`),
|
|
1
|
+
import{toPosix as e}from"./files.js";import{createRequire as t}from"node:module";import{mkdirSync as n,realpathSync as r,rmSync as i,writeFileSync as a}from"node:fs";import{dirname as o,join as s}from"node:path";import{fileURLToPath as c}from"node:url";import{execFileSync as l}from"node:child_process";var u=s(o(t(import.meta.url).resolve(`typescript/package.json`)),`bin`,`tsc`),d=s(o(c(import.meta.url)),`metaCheck.js`),f=/^check\.ts\((\d+),\d+\): error TS\d+: (.*)$/,p=/^(.+?)\(\d+,\d+\): error TS\d+: (.*)$/,m=[`propsNotInMeta`,`metaPropsNotOnComponent`,`propsThatDiffer`,`children`,`fixedProps`],h={propsNotInMeta:`props the component takes that the meta does not list`,metaPropsNotOnComponent:`props the meta lists that the component does not take`,propsThatDiffer:`props whose type, required or nullable flag, or nested fields differ from the component`,fixedProps:`fixed props whose value does not fit the component's prop`};function g(t){let n=e(t);return/\.d\.ts$/.test(n)?n.replace(/\.d\.ts$/,``):n.replace(/\.(jsx|tsx|js|ts|mjs)$/,``)}function _(e){let t=[];for(let n of e.split(/\r?\n/)){let e=f.exec(n);if(e){t.push({line:Number(e[1]),file:null,message:e[2]});continue}let r=p.exec(n);r&&t.push({line:null,file:r[1],message:r[2]})}return t}function v(e){let t=/Type '(\{[^']*\})'/.exec(e);return t?[...t[1].matchAll(/: "([^"]+)"/g)].map(e=>e[1]):[]}function y(e){let t=new Map,n=(e,n,r,i)=>{let a=`${e} ${n}`;t.has(a)||t.set(a,{component:e,handler:n,path:r,owner:i})},r=(e,t,i)=>{if(!(typeof e!=`object`||!e)){for(let r of e.handlers??[])n(e.component,r,t,i);Array.isArray(e.children)&&e.children.forEach((e,n)=>r(e,`${t}.children[${n}]`,i))}};return e.forEach(({meta:e},t)=>{(e.examples??[]).forEach((i,a)=>{let o=`examples[${a}]`;for(let r of i.handlers??[])n(e.name,r,o,t);r({children:i.children},o,t),r(i.wrap,`${o}.wrap`,t)});for(let r of Object.keys(e.actions??{}))n(e.name,r,`actions.${r}`,t);for(let n of[`do`,`dont`])(e.usage?.[n]??[]).forEach((e,i)=>{(e.example??[]).forEach((e,a)=>r(e,`usage.${n}[${i}].example[${a}]`,t))})}),t}function b({root:t,metas:o,componentOf:c}){let f=s(r(t),`node_modules`,`.canvas-kit-check-${process.pid}`);try{n(f,{recursive:!0});let t=o.map(({meta:e,file:t})=>({component:g(c(t,e.component??e.name)),meta:g(t)})),r=[],i=new Map,p=new Map,b=(e,t=-1,n=null)=>{r.push(e);let a=r.length;t>=0&&i.set(a,t),n&&p.set(a,n)};b(`import type { ExtraProps, MissingProps, MismatchedProps, ChildrenOk, HandlerKeys } from "${e(d)}";`),b(`type N = import("react").ReactNode;`),b(`type Empty = { [key: string]: never };`),o.forEach((e,n)=>{b(`import type * as C${n} from "${t[n].component}";`,n),b(`import type { meta as M${n} } from "${t[n].meta}";`,n)});let x=e=>`{ [K in ${e}]: K }`;o.forEach(({meta:e},t)=>{let n=e.name,r=e.component??e.name;b(`type P${t} = import("react").ComponentProps<typeof C${t}.${r}>;`,t),b(`export const ${n}__propsNotInMeta: Empty = null as unknown as ${x(`ExtraProps<P${t}, typeof M${t}>`)};`,t,`propsNotInMeta`),b(`export const ${n}__metaPropsNotOnComponent: Empty = null as unknown as ${x(`MissingProps<P${t}, typeof M${t}>`)};`,t,`metaPropsNotOnComponent`),b(`export const ${n}__propsThatDiffer: Empty = null as unknown as ${x(`MismatchedProps<P${t}, typeof M${t}, N>`)};`,t,`propsThatDiffer`),b(`export const ${n}__children: true = null as unknown as ChildrenOk<P${t}, typeof M${t}>;`,t,`children`),e.fixedProps!==void 0&&b(`export const ${n}__fixedProps: Partial<P${t}> = null as unknown as NonNullable<(typeof M${t})["fixedProps"]>;`,t,`fixedProps`)});let S=new Map(o.map(({meta:e},t)=>[e.name,t])),C=new Map,w=0;for(let{component:t,handler:n,path:i,owner:a}of y(o).values()){let s=S.get(t);s!==void 0&&(w+=1,b(`export const ${t.replace(/[^A-Za-z0-9_]/g,`_`)}__handler_${n}_${w}: Empty = null as unknown as { [K in Exclude<"${n}", HandlerKeys<P${s}>>]: K };`),C.set(r.length,{owner:a,where:`${e(o[a].file)}: ${i}`,component:t,handler:n}))}a(s(f,`check.ts`),`${r.join(`
|
|
2
|
+
`)}\n`),a(s(f,`tsconfig.json`),JSON.stringify({compilerOptions:{strict:!0,noEmit:!0,allowJs:!0,checkJs:!1,jsx:`react-jsx`,module:`esnext`,moduleResolution:`bundler`,target:`es2022`,skipLibCheck:!0,noErrorTruncation:!0,types:[]},files:[`check.ts`]},null,2));let T;try{l(process.execPath,[u,`-p`,s(f,`tsconfig.json`),`--pretty`,`false`],{cwd:f,encoding:`utf8`,stdio:`pipe`});return}catch(e){T=String(e.stdout??``)+String(e.stderr??``)}let E=new Map,D=[];for(let n of _(T)){let r=n.line===null?void 0:C.get(n.line);if(r){D.push({owner:r.owner,rank:m.length,text:`${r.where}: "${r.handler}" is not a function prop of ${r.component}`});continue}let a=n.line!==null&&i.has(n.line)?i.get(n.line):void 0,s=n.line===null?void 0:p.get(n.line);if(s&&a!==void 0){let e=`${a}:${s}`;if(E.has(e)||E.set(e,{owner:a,kind:s,names:new Set}),s!==`children`)for(let t of v(n.message))E.get(e).names.add(t);continue}let c=a??t.findIndex(e=>[n.file,n.message].some(t=>t&&(t.includes(e.component)||t.includes(e.meta)))),l=n.line===null?`${e(n.file)}: `:``;D.push({owner:c,rank:-1,text:`${c>=0?o[c].meta.name:`metaCheck`}: tsc could not read the check file: ${l}${n.message}`})}for(let{owner:e,kind:t,names:n}of E.values()){let{meta:r}=o[e],i=t===`children`?`${r.name}: acceptsChildren is ${r.acceptsChildren} but the component ${r.acceptsChildren?`does not take`:`takes`} children`:n.size>0?`${r.name}: ${h[t]}: ${[...n].sort().join(`, `)}`:`${r.name}: ${h[t]}: see the meta against the component's props`;D.push({owner:e,rank:m.indexOf(t),text:i})}throw D.length===0?Error(`metaCheck: tsc failed: ${T.split(/\r?\n/).slice(0,20).join(`
|
|
3
3
|
`)}`):(D.sort((e,t)=>e.owner-t.owner||e.rank-t.rank),Error(D.map(e=>e.text).join(`
|
|
4
|
-
`)))}finally{
|
|
4
|
+
`)))}finally{i(f,{recursive:!0,force:!0})}}export{u as TSC,b as checkMetaTypes,g as specifierOf};
|
package/dist/metas.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{toPosix as e}from"./files.js";import{existsSync as t,globSync as n}from"node:fs";import{basename as r,dirname as i,join as a,resolve as o}from"node:path";import{pathToFileURL as s}from"node:url";var c=Object.freeze([`a`,`b`,`button`,`div`,`img`,`kbd`,`li`,`p`,`span`,`strong`,`table`,`tbody`,`td`,`th`,`thead`,`tr`,`ul`]),l=/^[A-Z][A-Za-z0-9]*$/,u=/^[A-Za-z0-9][A-Za-z0-9_-]*$/,d=/^App[A-Za-z0-9]*$/,f=/^on[A-Z][A-Za-z0-9]*$/,p=/[\u2013\u2014]/,
|
|
2
|
-
`))}function v(e,{platform:t,source:n}){let r=Object.keys(e.props).sort(B);return{name:e.name,marker:e.marker,acceptsChildren:e.acceptsChildren,props:r.map(t=>P(t,e.props[t])),itemsTakeAction:t===`Web`?r.filter(t=>L(e.props[t])):[],takesAction:[],source:n,...e.component===void 0?{}:{component:e.component},...e.fixedProps===void 0?{}:{fixedProps:e.fixedProps},usage:e.usage}}function y(n,o){let s=n.replace(/\.meta\.[jt]s$/,``),c=r(s)===o?s:a(i(s),o);for(let n of h)if(t(c+n))return e(c+n);throw Error(`no component module for ${o} beside ${n}`)}function b(e,t,n,r,i,a){if(!V(e)){a.push([`meta`,`must be an object`]);return}let{name:o}=e;typeof o!=`string`||!l.test(o)?a.push([`name`,`"${o}" is not a catalogue name; start with a capital letter and use letters and digits`]):n.has(o)?a.push([`name`,`"${o}" is also the name in ${n.get(o)}`]):n.set(o,t),e.component!==void 0&&(U(e.component)?e.component===o&&a.push([`component`,`"${e.component}" is the name; leave component out when the meta draws its own export`]):a.push([`component`,`must name an export as text`]));let{platforms:s}=e;Array.isArray(s)&&s.length>0&&s.every(e=>e===`web`||e===`phone`)||a.push([`platforms`,`must list web, phone or both`]),e.marker!==null&&typeof e.marker!=`string`&&a.push([`marker`,`must be a string or null`]),typeof e.acceptsChildren!=`boolean`&&a.push([`acceptsChildren`,`must be true or false`]),e.omit!==void 0&&(!Array.isArray(e.omit)||!e.omit.every(e=>typeof e==`string`))&&a.push([`omit`,`must be a list of prop names`]);let c=V(e.props)?e.props:{};if(!V(e.props))a.push([`props`,`must be an object`]);else for(let[t,n]of Object.entries(e.props))x(n,`props.${t}`,i,a);let u=new Set;if(e.fixedProps!==void 0)if(e.component===void 0&&a.push([`fixedProps`,`needs component, the export the fixed props go to`]),!V(e.fixedProps)||Object.keys(e.fixedProps).length===0)a.push([`fixedProps`,`must be an object of the props the kit sets`]);else{let t=Array.isArray(e.omit)?e.omit:[];for(let n of Object.keys(e.fixedProps))u.add(n),Object.hasOwn(c,n)?a.push([`fixedProps.${n}`,`is also a prop; a fixed prop is set by the kit, not the panel`]):t.includes(n)&&a.push([`fixedProps.${n}`,`is listed in omit; a prop is fixed or omitted, not both`])}i.fixed={name:o,keys:u},Array.isArray(e.examples)?e.examples.forEach((n,o)=>{k(n,`examples[${o}]`,e,c,t,r,i,a)}):a.push([`examples`,`must be a list`]),A(e.usage,i,a),i.fixed=null,N(e,``,a)}function x(e,t,n,r){if(!V(e)){r.push([t,`is not a prop`]);return}if(!m.includes(e.type)){r.push([`${t}.type`,`${z(e.type)} is not a prop type`]);return}if(e.type===`enum`||e.type===`enumOrNumber`?S(e,t,r):e.values!==void 0&&r.push([`${t}.values`,`only an enum or an enumOrNumber has values`]),e.fields!==void 0)if(e.type!==`object`)r.push([`${t}.fields`,`only an object has fields`]);else for(let[i,a]of Object.entries(e.fields))x(a,`${t}.fields.${i}`,n,r);if(e.itemFields!==void 0)if(e.type!==`array`)r.push([`${t}.itemFields`,`only an array has itemFields`]);else for(let[i,a]of Object.entries(e.itemFields))x(a,`${t}.itemFields.${i}`,n,r);if(U(e.description)||r.push([`${t}.description`,`is empty`]),e.required!==void 0&&typeof e.required!=`boolean`&&r.push([`${t}.required`,`must be true or false`]),e.nullable!==void 0&&typeof e.nullable!=`boolean`&&r.push([`${t}.nullable`,`must be true or false`]),e.default!==void 0)if(e.required===!0)r.push([`${t}.default`,`a required prop has no default`]);else{let i=[];C(e.default,e,t,n,i),i.length>0&&r.push([`${t}.default`,`${z(e.default)} does not fit ${e.type}`])}}function S(e,t,n){let{type:r,values:i}=e;if(!Array.isArray(i)||i.length===0){n.push([`${t}.values`,`an ${r} lists its values`]);return}let a=new Set;for(let e of i)(r===`enum`?typeof e==`string`||H(e):typeof e==`string`)?a.has(e)&&n.push([`${t}.values`,`"${e}" is listed twice`]):n.push([`${t}.values`,`${z(e)} is not allowed here`]),a.add(e)}function C(e,t,n,r,i){if(!V(t)||e===null&&(t.nullable===!0||t.type===`node`||t.type===`icon`))return;let a=Array.isArray(t.values)?t.values:[];switch(t.type){case`string`:case`url`:typeof e!=`string`&&R(e,t.type,n,i);return;case`number`:H(e)||R(e,t.type,n,i);return;case`boolean`:typeof e!=`boolean`&&R(e,t.type,n,i);return;case`stringOrNumber`:typeof e!=`string`&&!H(e)&&R(e,t.type,n,i);return;case`enum`:a.includes(e)||i.push([n,`${z(e)} is not one of ${z(a)}`]);return;case`enumOrNumber`:!H(e)&&!a.includes(e)&&i.push([n,`${z(e)} is not one of ${z(a)}`]);return;case`icon`:W(e)?E(e,n,r,i):typeof e==`string`?Array.isArray(r.icons)&&!r.icons.includes(e)&&i.push([n,`"${e}" is not an icon of the kit`]):R(e,t.type,n,i);return;case`node`:Array.isArray(e)?e.forEach((e,t)=>w(e,`${n}[${t}]`,r,i)):w(e,n,r,i);return;case`array`:Array.isArray(e)?t.itemFields!==void 0&&e.forEach((e,a)=>{V(e)?T(e,t.itemFields,`${n}[${a}]`,r,i):i.push([`${n}[${a}]`,`${z(e)} is not an object`])}):R(e,t.type,n,i);return;case`object`:V(e)?t.fields!==void 0&&T(e,t.fields,n,r,i):R(e,t.type,n,i);return;default:return}}function w(e,t,n,r){W(e)?E(e,t,n,r):typeof e!=`string`&&!H(e)&&r.push([t,`${z(e)} is not a node`])}function T(e,t,n,r,i){for(let a of Object.keys(e))Object.hasOwn(t,a)?C(e[a],t[a],`${n}.${a}`,r,i):i.push([`${n}.${a}`,`not a field`]);for(let[r,a]of Object.entries(t))V(a)&&a.required===!0&&!Object.hasOwn(e,r)&&i.push([`${n}.${r}`,`missing, the field is required`])}function E(e,t,n,r){let{component:i}=e,a=n.byName.get(i);if(!a&&!n.knownNames.has(i)&&!c.includes(i)&&r.push([`${t}.component`,`"${i}" is neither a component of the kit nor one of the HTML tags`]),a&&e.props!==void 0)if(!V(e.props))r.push([`${t}.props`,`must be an object`]);else{let o=V(a.props)?a.props:{};for(let[a,s]of Object.entries(e.props))n.fixed&&n.fixed.name===i&&n.fixed.keys.has(a)?r.push([`${t}.props.${a}`,`is a fixed prop; the kit sets it`]):Object.hasOwn(o,a)?C(s,o[a],`${t}.props.${a}`,n,r):r.push([`${t}.props.${a}`,`not a prop of ${i}`])}e.children!==void 0&&(a&&a.acceptsChildren===!1&&r.push([`${t}.children`,`${i} takes no children`]),O(e.children,`${t}.children`,n,r)),e.handlers!==void 0&&D(e.handlers,`${t}.handlers`,i,a,r)}function D(e,t,n,r,i){if(!Array.isArray(e)||!e.every(e=>typeof e==`string`)){i.push([t,`must be a list of handler names`]);return}if(c.includes(n)){i.push([t,`handlers need a kit component`]);return}if(e.length===0){i.push([t,`omit handlers when empty`]);return}let a=new Set;for(let o of e)f.test(o)?a.has(o)?i.push([t,`"${o}" is listed twice`]):r&&V(r.props)&&Object.hasOwn(r.props,o)&&i.push([t,`"${o}" is a data prop of ${n}, set it in props`]):i.push([t,`"${o}" is not a handler name (on<Event>)`]),a.add(o)}function O(e,t,n,r){if(typeof e!=`string`){if(!Array.isArray(e)){r.push([t,`is text or a list of text and placed elements`]);return}e.forEach((e,i)=>{typeof e!=`string`&&(W(e)?E(e,`${t}[${i}]`,n,r):r.push([`${t}[${i}]`,`is neither text nor a placed element`]))})}}function k(e,t,n,r,i,a,o,s){if(!V(e)){s.push([t,`is not an example`]);return}let{name:c}=n,{id:l}=e;if(typeof l==`string`&&(l===c||l.startsWith(`${c}.`)&&u.test(l.slice(c.length+1)))?a.has(l)?s.push([`${t}.id`,`"${l}" is also used in ${a.get(l)}`]):a.set(l,i):s.push([`${t}.id`,`"${l}" must be ${c} or ${c}.<case>`]),U(e.title)||s.push([`${t}.title`,`is empty`]),!V(e.props))s.push([`${t}.props`,`must be an object`]);else{for(let[n,i]of Object.entries(e.props))o.fixed&&o.fixed.keys.has(n)?s.push([`${t}.props.${n}`,`is a fixed prop; the kit sets it`]):Object.hasOwn(r,n)?C(i,r[n],`${t}.props.${n}`,o,s):s.push([`${t}.props.${n}`,`not a prop of ${c}`]);for(let[n,i]of Object.entries(r))V(i)&&i.required===!0&&!Object.hasOwn(e.props,n)&&s.push([`${t}.props.${n}`,`missing, the prop is required`])}e.children!==void 0&&(n.acceptsChildren===!1&&s.push([`${t}.children`,`${c} takes no children`]),O(e.children,`${t}.children`,o,s)),e.wrap!==void 0&&(W(e.wrap)?E(e.wrap,`${t}.wrap`,o,s):s.push([`${t}.wrap`,`is not a placed element`])),e.handlers!==void 0&&D(e.handlers,`${t}.handlers`,c,n,s)}function A(e,t,n){if(!V(e)){n.push([`usage`,`must be an object`]);return}(!Array.isArray(e.when)||e.when.length===0||!e.when.every(U))&&n.push([`usage.when`,`lists nothing; write at least one line`]),Array.isArray(e.whenNot)?e.whenNot.forEach((e,r)=>j(e,`usage.whenNot[${r}]`,t,n)):n.push([`usage.whenNot`,`must be a list`]),Array.isArray(e.do)||n.push([`usage.do`,`must be a list`]),Array.isArray(e.dont)||n.push([`usage.dont`,`must be a list`]),Array.isArray(e.do)&&Array.isArray(e.dont)&&(e.do.length!==e.dont.length&&n.push([`usage`,`${e.do.length} do against ${e.dont.length} dont; they pair up one to one`]),e.do.forEach((e,r)=>M(e,`usage.do[${r}]`,t,n)),e.dont.forEach((e,r)=>M(e,`usage.dont[${r}]`,t,n))),Array.isArray(e.a11y)?e.a11y.forEach((e,t)=>{U(e)||n.push([`usage.a11y[${t}]`,`is empty`])}):n.push([`usage.a11y`,`must be a list`])}function j(e,t,n,r){if(!V(e)){r.push([t,`is not an object`]);return}U(e.text)||r.push([`${t}.text`,`is empty`]),e.instead!==void 0&&(typeof e.instead==`string`?d.test(e.instead)&&!n.byName.has(e.instead)&&!n.knownNames.has(e.instead)&&r.push([`${t}.instead`,`"${e.instead}" is not a component of the kit`]):r.push([`${t}.instead`,`must be text`]))}function M(e,t,n,r){if(!V(e)){r.push([t,`is not a practice`]);return}if(U(e.note)||r.push([`${t}.note`,`is empty`]),!Array.isArray(e.example)||e.example.length===0){r.push([`${t}.example`,`needs at least one placed element`]);return}e.example.forEach((e,i)=>{let a=`${t}.example[${i}]`;W(e)?E(e,a,n,r):r.push([a,`is not a placed element`])})}function N(e,t,n){if(typeof e==`string`)p.test(e)&&n.push([t,`contains an em or en dash; use a comma, colon or period`]);else if(Array.isArray(e))e.forEach((e,r)=>N(e,`${t}[${r}]`,n));else if(V(e))for(let[r,i]of Object.entries(e))N(i,t?`${t}.${r}`:r,n)}function P(e,t){let n={name:e,type:t.type};return(t.type===`enum`||t.type===`enumOrNumber`)&&(n.enum=I(t.values)),t.fields!==void 0&&(n.fields=F(t.fields)),t.itemFields!==void 0&&(n.itemFields=F(t.itemFields)),n.required=t.required===!0,t.nullable===!0&&(n.nullable=!0),t.description!==void 0&&(n.description=t.description),t.default!==void 0&&(n.default=t.default),n}function F(e){return Object.keys(e).sort(B).map(t=>P(t,e[t]))}function I(e){return[...e].sort((e,t)=>{let n=typeof e==`number`,r=typeof t==`number`;return n&&r?e-t:n===r?B(e,t):n?-1:1})}function L(e){return e.type===`array`&&Object.keys(e.itemFields??{}).some(e=>e===`href`||e===`to`)}function R(e,t,n,r){r.push([n,`${z(e)} is not a ${t}`])}function z(e){return JSON.stringify(e)}function B(e,t){return e<t?-1:+(e>t)}function V(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function H(e){return typeof e==`number`&&Number.isFinite(e)}function U(e){return typeof e==`string`&&e.trim()!==``}function W(e){return V(e)&&typeof e.component==`string`}export{c as HTML_TAGS,_ as checkMetas,y as componentModuleOf,v as manifestComponent,g as readMetas};
|
|
1
|
+
import{toPosix as e}from"./files.js";import{existsSync as t,globSync as n}from"node:fs";import{basename as r,dirname as i,join as a,resolve as o}from"node:path";import{pathToFileURL as s}from"node:url";var c=Object.freeze([`a`,`b`,`button`,`div`,`img`,`kbd`,`li`,`p`,`span`,`strong`,`table`,`tbody`,`td`,`th`,`thead`,`tr`,`ul`]),l=/^[A-Z][A-Za-z0-9]*$/,u=/^[A-Za-z0-9][A-Za-z0-9_-]*$/,d=/^App[A-Za-z0-9]*$/,f=/^on[A-Z][A-Za-z0-9]*$/,p=/^[a-z][A-Za-z0-9]*$/,m=/[\u2013\u2014]/,h=[`string`,`number`,`stringOrNumber`,`boolean`,`enum`,`enumOrNumber`,`url`,`icon`,`node`,`array`,`object`,`any`],g=[`.jsx`,`.tsx`,`.js`,`.ts`];async function _({root:e,components:t,knownNames:r,icons:i}){let a=n(t,{cwd:e});if(a.length===0)throw Error(`${e}: no component metadata matches ${t}`);let c=a.map(t=>o(e,t)).sort(H),l=[];for(let e of c){let t=await import(s(e).href);if(!(`meta`in t))throw Error(`${e}: exports no meta; write export const meta = defineMeta({...})`);l.push({meta:t.meta,file:e})}return v(l,{knownNames:r,icons:i}),l.sort((e,t)=>H(e.meta.name,t.meta.name))}function v(e,{knownNames:t=[],icons:n}={}){let r=new Map;for(let{meta:t}of e)U(t)&&typeof t.name==`string`&&!r.has(t.name)&&r.set(t.name,t);let i={byName:r,knownNames:new Set(t),icons:n},a=new Map,o=new Map,s=[];for(let{meta:t,file:n}of e){let e=[];x(t,n,a,o,i,e);for(let[t,r]of e)s.push(`${n}: ${t}: ${r}`)}if(s.length>0)throw Error(s.join(`
|
|
2
|
+
`))}function y(e,{platform:t,source:n}){let r=Object.keys(e.props).sort(H),i=t===`Web`&&U(e.actions)?Object.values(e.actions):[];return{name:e.name,marker:e.marker,acceptsChildren:e.acceptsChildren,props:[...r.map(t=>I(t,e.props[t])),...i.map(e=>({name:e,type:`object`,fields:[],required:!1}))].sort((e,t)=>H(e.name,t.name)),itemsTakeAction:t===`Web`?r.filter(t=>z(e.props[t])):[],takesAction:[...i].sort(H),source:n,...e.component===void 0?{}:{component:e.component},...e.fixedProps===void 0?{}:{fixedProps:e.fixedProps},usage:e.usage}}function b(n,o){let s=n.replace(/\.meta\.[jt]s$/,``),c=r(s)===o?s:a(i(s),o);for(let n of g)if(t(c+n))return e(c+n);throw Error(`no component module for ${o} beside ${n}`)}function x(e,t,n,r,i,a){if(!U(e)){a.push([`meta`,`must be an object`]);return}let{name:o}=e;typeof o!=`string`||!l.test(o)?a.push([`name`,`"${o}" is not a catalogue name; start with a capital letter and use letters and digits`]):n.has(o)?a.push([`name`,`"${o}" is also the name in ${n.get(o)}`]):n.set(o,t),e.component!==void 0&&(G(e.component)?e.component===o&&a.push([`component`,`"${e.component}" is the name; leave component out when the meta draws its own export`]):a.push([`component`,`must name an export as text`]));let{platforms:s}=e;Array.isArray(s)&&s.length>0&&s.every(e=>e===`web`||e===`phone`)||a.push([`platforms`,`must list web, phone or both`]),e.marker!==null&&typeof e.marker!=`string`&&a.push([`marker`,`must be a string or null`]),typeof e.acceptsChildren!=`boolean`&&a.push([`acceptsChildren`,`must be true or false`]),e.omit!==void 0&&(!Array.isArray(e.omit)||!e.omit.every(e=>typeof e==`string`))&&a.push([`omit`,`must be a list of prop names`]),e.actions!==void 0&&S(e,a);let c=U(e.props)?e.props:{};if(!U(e.props))a.push([`props`,`must be an object`]);else for(let[t,n]of Object.entries(e.props))C(n,`props.${t}`,i,a);let u=new Set;if(e.fixedProps!==void 0)if(e.component===void 0&&a.push([`fixedProps`,`needs component, the export the fixed props go to`]),!U(e.fixedProps)||Object.keys(e.fixedProps).length===0)a.push([`fixedProps`,`must be an object of the props the kit sets`]);else{let t=Array.isArray(e.omit)?e.omit:[];for(let n of Object.keys(e.fixedProps))u.add(n),Object.hasOwn(c,n)?a.push([`fixedProps.${n}`,`is also a prop; a fixed prop is set by the kit, not the panel`]):t.includes(n)&&a.push([`fixedProps.${n}`,`is listed in omit; a prop is fixed or omitted, not both`])}i.fixed={name:o,keys:u},Array.isArray(e.examples)?e.examples.forEach((n,o)=>{j(n,`examples[${o}]`,e,c,t,r,i,a)}):a.push([`examples`,`must be a list`]),M(e.usage,i,a),i.fixed=null,F(e,``,a)}function S(e,t){let{actions:n}=e;if(!U(n)||Object.keys(n).length===0){t.push([`actions`,`must be an object of handler name to action prop name`]);return}(!Array.isArray(e.platforms)||!e.platforms.includes(`web`))&&t.push([`actions`,`only a web component takes actions; add web to platforms or remove actions`]);let r=U(e.props)?e.props:{},i=U(e.fixedProps)?e.fixedProps:{},a=new Map;for(let[e,o]of Object.entries(n)){let n=`actions.${e}`;f.test(e)||t.push([n,`"${e}" is not a handler name (on<Event>)`]),typeof o!=`string`||!p.test(o)?t.push([n,`${V(o)} is not an action prop name; start with a lower-case letter and use letters and digits`]):Object.hasOwn(r,o)||Object.hasOwn(i,o)?t.push([n,`"${o}" is also a prop; an action stand-in is not a library prop`]):a.has(o)?t.push([n,`"${o}" is also the action prop of ${a.get(o)}`]):a.set(o,e)}}function C(e,t,n,r){if(!U(e)){r.push([t,`is not a prop`]);return}if(!h.includes(e.type)){r.push([`${t}.type`,`${V(e.type)} is not a prop type`]);return}if(e.type===`enum`||e.type===`enumOrNumber`?w(e,t,r):e.values!==void 0&&r.push([`${t}.values`,`only an enum or an enumOrNumber has values`]),e.fields!==void 0)if(e.type!==`object`)r.push([`${t}.fields`,`only an object has fields`]);else for(let[i,a]of Object.entries(e.fields))C(a,`${t}.fields.${i}`,n,r);if(e.itemFields!==void 0)if(e.type!==`array`)r.push([`${t}.itemFields`,`only an array has itemFields`]);else for(let[i,a]of Object.entries(e.itemFields))C(a,`${t}.itemFields.${i}`,n,r);if(G(e.description)||r.push([`${t}.description`,`is empty`]),e.required!==void 0&&typeof e.required!=`boolean`&&r.push([`${t}.required`,`must be true or false`]),e.nullable!==void 0&&typeof e.nullable!=`boolean`&&r.push([`${t}.nullable`,`must be true or false`]),e.default!==void 0)if(e.required===!0)r.push([`${t}.default`,`a required prop has no default`]);else{let i=[];T(e.default,e,t,n,i),i.length>0&&r.push([`${t}.default`,`${V(e.default)} does not fit ${e.type}`])}}function w(e,t,n){let{type:r,values:i}=e;if(!Array.isArray(i)||i.length===0){n.push([`${t}.values`,`an ${r} lists its values`]);return}let a=new Set;for(let e of i)(r===`enum`?typeof e==`string`||W(e):typeof e==`string`)?a.has(e)&&n.push([`${t}.values`,`"${e}" is listed twice`]):n.push([`${t}.values`,`${V(e)} is not allowed here`]),a.add(e)}function T(e,t,n,r,i){if(!U(t)||e===null&&(t.nullable===!0||t.type===`node`||t.type===`icon`))return;let a=Array.isArray(t.values)?t.values:[];switch(t.type){case`string`:case`url`:typeof e!=`string`&&B(e,t.type,n,i);return;case`number`:W(e)||B(e,t.type,n,i);return;case`boolean`:typeof e!=`boolean`&&B(e,t.type,n,i);return;case`stringOrNumber`:typeof e!=`string`&&!W(e)&&B(e,t.type,n,i);return;case`enum`:a.includes(e)||i.push([n,`${V(e)} is not one of ${V(a)}`]);return;case`enumOrNumber`:!W(e)&&!a.includes(e)&&i.push([n,`${V(e)} is not one of ${V(a)}`]);return;case`icon`:K(e)?O(e,n,r,i):typeof e==`string`?Array.isArray(r.icons)&&!r.icons.includes(e)&&i.push([n,`"${e}" is not an icon of the kit`]):B(e,t.type,n,i);return;case`node`:Array.isArray(e)?e.forEach((e,t)=>E(e,`${n}[${t}]`,r,i)):E(e,n,r,i);return;case`array`:Array.isArray(e)?t.itemFields!==void 0&&e.forEach((e,a)=>{U(e)?D(e,t.itemFields,`${n}[${a}]`,r,i):i.push([`${n}[${a}]`,`${V(e)} is not an object`])}):B(e,t.type,n,i);return;case`object`:U(e)?t.fields!==void 0&&D(e,t.fields,n,r,i):B(e,t.type,n,i);return;default:return}}function E(e,t,n,r){K(e)?O(e,t,n,r):typeof e!=`string`&&!W(e)&&r.push([t,`${V(e)} is not a node`])}function D(e,t,n,r,i){for(let a of Object.keys(e))Object.hasOwn(t,a)?T(e[a],t[a],`${n}.${a}`,r,i):i.push([`${n}.${a}`,`not a field`]);for(let[r,a]of Object.entries(t))U(a)&&a.required===!0&&!Object.hasOwn(e,r)&&i.push([`${n}.${r}`,`missing, the field is required`])}function O(e,t,n,r){let{component:i}=e,a=n.byName.get(i);if(!a&&!n.knownNames.has(i)&&!c.includes(i)&&r.push([`${t}.component`,`"${i}" is neither a component of the kit nor one of the HTML tags`]),a&&e.props!==void 0)if(!U(e.props))r.push([`${t}.props`,`must be an object`]);else{let o=U(a.props)?a.props:{};for(let[a,s]of Object.entries(e.props))n.fixed&&n.fixed.name===i&&n.fixed.keys.has(a)?r.push([`${t}.props.${a}`,`is a fixed prop; the kit sets it`]):Object.hasOwn(o,a)?T(s,o[a],`${t}.props.${a}`,n,r):r.push([`${t}.props.${a}`,`not a prop of ${i}`])}e.children!==void 0&&(a&&a.acceptsChildren===!1&&r.push([`${t}.children`,`${i} takes no children`]),A(e.children,`${t}.children`,n,r)),e.handlers!==void 0&&k(e.handlers,`${t}.handlers`,i,a,r)}function k(e,t,n,r,i){if(!Array.isArray(e)||!e.every(e=>typeof e==`string`)){i.push([t,`must be a list of handler names`]);return}if(c.includes(n)){i.push([t,`handlers need a kit component`]);return}if(e.length===0){i.push([t,`omit handlers when empty`]);return}let a=new Set;for(let o of e)f.test(o)?a.has(o)?i.push([t,`"${o}" is listed twice`]):r&&U(r.props)&&Object.hasOwn(r.props,o)&&i.push([t,`"${o}" is a data prop of ${n}, set it in props`]):i.push([t,`"${o}" is not a handler name (on<Event>)`]),a.add(o)}function A(e,t,n,r){if(typeof e!=`string`){if(!Array.isArray(e)){r.push([t,`is text or a list of text and placed elements`]);return}e.forEach((e,i)=>{typeof e!=`string`&&(K(e)?O(e,`${t}[${i}]`,n,r):r.push([`${t}[${i}]`,`is neither text nor a placed element`]))})}}function j(e,t,n,r,i,a,o,s){if(!U(e)){s.push([t,`is not an example`]);return}let{name:c}=n,{id:l}=e;if(typeof l==`string`&&(l===c||l.startsWith(`${c}.`)&&u.test(l.slice(c.length+1)))?a.has(l)?s.push([`${t}.id`,`"${l}" is also used in ${a.get(l)}`]):a.set(l,i):s.push([`${t}.id`,`"${l}" must be ${c} or ${c}.<case>`]),G(e.title)||s.push([`${t}.title`,`is empty`]),!U(e.props))s.push([`${t}.props`,`must be an object`]);else{for(let[n,i]of Object.entries(e.props))o.fixed&&o.fixed.keys.has(n)?s.push([`${t}.props.${n}`,`is a fixed prop; the kit sets it`]):Object.hasOwn(r,n)?T(i,r[n],`${t}.props.${n}`,o,s):s.push([`${t}.props.${n}`,`not a prop of ${c}`]);for(let[n,i]of Object.entries(r))U(i)&&i.required===!0&&!Object.hasOwn(e.props,n)&&s.push([`${t}.props.${n}`,`missing, the prop is required`])}e.children!==void 0&&(n.acceptsChildren===!1&&s.push([`${t}.children`,`${c} takes no children`]),A(e.children,`${t}.children`,o,s)),e.wrap!==void 0&&(K(e.wrap)?O(e.wrap,`${t}.wrap`,o,s):s.push([`${t}.wrap`,`is not a placed element`])),e.handlers!==void 0&&k(e.handlers,`${t}.handlers`,c,n,s)}function M(e,t,n){if(!U(e)){n.push([`usage`,`must be an object`]);return}(!Array.isArray(e.when)||e.when.length===0||!e.when.every(G))&&n.push([`usage.when`,`lists nothing; write at least one line`]),Array.isArray(e.whenNot)?e.whenNot.forEach((e,r)=>N(e,`usage.whenNot[${r}]`,t,n)):n.push([`usage.whenNot`,`must be a list`]),Array.isArray(e.do)||n.push([`usage.do`,`must be a list`]),Array.isArray(e.dont)||n.push([`usage.dont`,`must be a list`]),Array.isArray(e.do)&&Array.isArray(e.dont)&&(e.do.length!==e.dont.length&&n.push([`usage`,`${e.do.length} do against ${e.dont.length} dont; they pair up one to one`]),e.do.forEach((e,r)=>P(e,`usage.do[${r}]`,t,n)),e.dont.forEach((e,r)=>P(e,`usage.dont[${r}]`,t,n))),Array.isArray(e.a11y)?e.a11y.forEach((e,t)=>{G(e)||n.push([`usage.a11y[${t}]`,`is empty`])}):n.push([`usage.a11y`,`must be a list`])}function N(e,t,n,r){if(!U(e)){r.push([t,`is not an object`]);return}G(e.text)||r.push([`${t}.text`,`is empty`]),e.instead!==void 0&&(typeof e.instead==`string`?d.test(e.instead)&&!n.byName.has(e.instead)&&!n.knownNames.has(e.instead)&&r.push([`${t}.instead`,`"${e.instead}" is not a component of the kit`]):r.push([`${t}.instead`,`must be text`]))}function P(e,t,n,r){if(!U(e)){r.push([t,`is not a practice`]);return}if(G(e.note)||r.push([`${t}.note`,`is empty`]),!Array.isArray(e.example)||e.example.length===0){r.push([`${t}.example`,`needs at least one placed element`]);return}e.example.forEach((e,i)=>{let a=`${t}.example[${i}]`;K(e)?O(e,a,n,r):r.push([a,`is not a placed element`])})}function F(e,t,n){if(typeof e==`string`)m.test(e)&&n.push([t,`contains an em or en dash; use a comma, colon or period`]);else if(Array.isArray(e))e.forEach((e,r)=>F(e,`${t}[${r}]`,n));else if(U(e))for(let[r,i]of Object.entries(e))F(i,t?`${t}.${r}`:r,n)}function I(e,t){let n={name:e,type:t.type};return(t.type===`enum`||t.type===`enumOrNumber`)&&(n.enum=R(t.values)),t.fields!==void 0&&(n.fields=L(t.fields)),t.itemFields!==void 0&&(n.itemFields=L(t.itemFields)),n.required=t.required===!0,t.nullable===!0&&(n.nullable=!0),t.description!==void 0&&(n.description=t.description),t.default!==void 0&&(n.default=t.default),n}function L(e){return Object.keys(e).sort(H).map(t=>I(t,e[t]))}function R(e){return[...e].sort((e,t)=>{let n=typeof e==`number`,r=typeof t==`number`;return n&&r?e-t:n===r?H(e,t):n?-1:1})}function z(e){return e.type===`array`&&Object.keys(e.itemFields??{}).some(e=>e===`href`||e===`to`)}function B(e,t,n,r){r.push([n,`${V(e)} is not a ${t}`])}function V(e){return JSON.stringify(e)}function H(e,t){return e<t?-1:+(e>t)}function U(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function W(e){return typeof e==`number`&&Number.isFinite(e)}function G(e){return typeof e==`string`&&e.trim()!==``}function K(e){return U(e)&&typeof e.component==`string`}export{c as HTML_TAGS,v as checkMetas,b as componentModuleOf,y as manifestComponent,_ as readMetas};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** The attribute every stamped element carries: "<repo path>:<line>:<column>", line and column 1-based. */
|
|
2
|
+
export declare const SOURCE_STAMP_ATTRIBUTE: "data-canvas-at";
|
|
3
|
+
|
|
4
|
+
/** The environment the app is built for: its compiled AppSettings ENVIRONMENT, exactly. */
|
|
5
|
+
export type SourceStampEnvironment = "Local" | "Development" | "Staging" | "Production";
|
|
6
|
+
|
|
7
|
+
export interface SourceStampOptions {
|
|
8
|
+
environment: SourceStampEnvironment;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The slice of Babel's plugin API the stamp uses (Babel 7.29 or 8). */
|
|
12
|
+
export interface BabelPluginApi {
|
|
13
|
+
assertVersion(range: string | number): void;
|
|
14
|
+
types: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface BabelPlugin {
|
|
18
|
+
name: string;
|
|
19
|
+
visitor: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type BabelPluginItem = [plugin: (api: BabelPluginApi) => BabelPlugin, options: Record<string, never>];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The Babel plugins for this environment: the stamp in Local, Development and Staging, none in Production.
|
|
26
|
+
* Throws when environment is missing or not one of the four names.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sourceStampPlugins(options: SourceStampOptions): [] | [BabelPluginItem];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{existsSync as e}from"node:fs";import{dirname as t,join as n,relative as r,sep as i}from"node:path";var a=`data-canvas-at`,o=[`Local`,`Development`,`Staging`,`Production`],s=new Map;function c(e){let t=e?.environment;if(!o.includes(t))throw Error(`sourceStamp: environment must be one of Local, Development, Staging, Production (the app's compiled AppSettings ENVIRONMENT); got ${JSON.stringify(t)}.`);return t===`Production`?[]:[[l,{}]]}function l(e){e.assertVersion(`^7.29.0 || ^8.0.0`);let t=e.types;return{name:`webority-source-stamp`,pre(){this.stampPath=u(this.filename)},visitor:{JSXOpeningElement(e){if(this.stampPath===null||!e.node.loc)return;let{node:n}=e;if(f(n.name)||n.attributes.some(p))return;let{line:r,column:i}=n.loc.start;n.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(a),t.stringLiteral(`${this.stampPath}:${r}:${i+1}`)))}}}}function u(e){if(e===void 0||e===``)throw Error(`sourceStamp: Babel gave no file name; the transform must pass filename.`);let t=e.split(`?`,1)[0];return t.startsWith(`\0`)||t.split(/[/\\]/).includes(`node_modules`)?null:r(d(t),t).split(i).join(`/`)}function d(r){let i=[],a=t(r),o;for(;o===void 0;){if(s.has(a)){o=s.get(a);break}if(i.push(a),e(n(a,`.webority`,`settings.json`))){o=a;break}let c=t(a);if(c===a)throw Error(`sourceStamp: no .webority/settings.json at or above ${r}; the stamp path starts at the repository root.`);a=c}for(let e of i)s.set(e,o);return o}function f(e){return e.type===`JSXIdentifier`&&e.name===`Fragment`||e.type===`JSXMemberExpression`&&e.property.name===`Fragment`}function p(e){return e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===`data-canvas-at`}export{a as SOURCE_STAMP_ATTRIBUTE,c as sourceStampPlugins};
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority/canvas-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.98.0",
|
|
4
4
|
"description": "Builds product kits for Webority Canvas: a kit bundle, its manifest, and the spec shape the renderers answer.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
5
6
|
"private": false,
|
|
6
7
|
"type": "module",
|
|
7
8
|
"files": [
|
|
@@ -15,6 +16,10 @@
|
|
|
15
16
|
"./meta": {
|
|
16
17
|
"types": "./dist/meta.d.ts",
|
|
17
18
|
"default": "./dist/meta.js"
|
|
19
|
+
},
|
|
20
|
+
"./source-stamp": {
|
|
21
|
+
"types": "./dist/sourceStamp.d.ts",
|
|
22
|
+
"default": "./dist/sourceStamp.js"
|
|
18
23
|
}
|
|
19
24
|
},
|
|
20
25
|
"bin": {
|
|
@@ -26,6 +31,7 @@
|
|
|
26
31
|
"vite": "^8.0.12"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
34
|
+
"@babel/core": "7.29.7",
|
|
29
35
|
"react": "^19.2.6"
|
|
30
36
|
},
|
|
31
37
|
"scripts": {
|