@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.61
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/assets/pano-bootstrap.css +17903 -0
- package/bin/__tests__/new/e2e.test.js +111 -0
- package/bin/__tests__/new/helpers.js +76 -0
- package/bin/__tests__/new/new.test.js +472 -0
- package/bin/new.js +530 -0
- package/bin/pano-plugin.js +162 -0
- package/bin/templates/plugin/README.md +30 -0
- package/bin/templates/plugin/_gitignore +15 -0
- package/bin/templates/plugin/build.gradle.kts +389 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/bin/templates/plugin/gradle.properties +55 -0
- package/bin/templates/plugin/gradlew +249 -0
- package/bin/templates/plugin/gradlew.bat +92 -0
- package/bin/templates/plugin/package.json.tpl +14 -0
- package/bin/templates/plugin/rollup.config.js +2 -0
- package/bin/templates/plugin/settings.gradle.kts +5 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
- package/bin/templates/plugin/src/main/resources/config.conf +1 -0
- package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
- package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
- package/bin/templates/plugin/src/main/resources/logo.png +0 -0
- package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
- package/bin/ui.js +96 -0
- package/package.json +46 -4
- package/src/config.js +191 -0
- package/src/controller/__tests__/controller.test.js +647 -0
- package/src/controller/index.js +604 -0
- package/src/dev.js +208 -0
- package/src/index.js +22 -0
- package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
- package/src/rollup/__tests__/controllers/fixture.js +143 -0
- package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
- package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
- package/src/rollup/__tests__/preset/preset.test.js +418 -0
- package/src/rollup/__tests__/rules/_build.js +39 -0
- package/src/rollup/__tests__/rules/_fixture.js +83 -0
- package/src/rollup/__tests__/rules/helpers.test.js +148 -0
- package/src/rollup/__tests__/rules/rules.test.js +259 -0
- package/src/rollup/__tests__/rules/stamp.test.js +113 -0
- package/src/rollup/__tests__/samples/samples.test.js +481 -0
- package/src/rollup/__tests__/seams/config.test.js +78 -0
- package/src/rollup/__tests__/seams/lock.test.js +124 -0
- package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
- package/src/rollup/__tests__/seams/stubs.test.js +52 -0
- package/src/rollup/__tests__/views/helpers.js +82 -0
- package/src/rollup/__tests__/views/views.test.js +522 -0
- package/src/rollup/__tests__/widgets/helpers.js +117 -0
- package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
- package/src/rollup/cli-build.js +90 -0
- package/src/rollup/cli-check.js +280 -0
- package/src/rollup/cli-classes.js +75 -0
- package/src/rollup/client-gen.js +72 -0
- package/src/rollup/controllers.js +434 -0
- package/src/rollup/entry.js +250 -0
- package/src/rollup/helpers.js +193 -0
- package/src/rollup/index.js +601 -0
- package/src/rollup/integrate.js +205 -0
- package/src/rollup/lock.js +230 -0
- package/src/rollup/meta.js +479 -0
- package/src/rollup/rules.js +950 -0
- package/src/rollup/samples.js +667 -0
- package/src/rollup/stamp.js +410 -0
- package/src/rollup/types.js +248 -0
- package/src/rollup/views.js +1205 -0
- package/src/rollup/widgets.js +1000 -0
- package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
- package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
- package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
- package/src/styles/__tests__/fallback/plugins.test.js +301 -0
- package/src/styles/__tests__/lint/check-styles.test.js +423 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
- package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
- package/src/styles/bootstrap-classes.js +155 -0
- package/src/styles/check-styles.js +626 -0
- package/src/styles/fallback-css.js +1182 -0
- package/src/styles/plugin-css.js +237 -0
- package/src/styles/scan.js +206 -0
- package/src/styles/semantic-classes.js +436 -0
- package/src/styles/tokens.map.js +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { namespaceOf, readProperty } from "../config.js";
|
|
6
|
+
import { diffSection, readLock, writeLockSection } from "./lock.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* panoViews (doc 01 section 2, doc 02 section 4): the view half of the plugin build.
|
|
10
|
+
*
|
|
11
|
+
* - scans every `.svelte` file under `dirs`; name = basename, id = `<ns>:<Name>`;
|
|
12
|
+
* - reads the literal `export const view = {...}` and the props (`$props()` /
|
|
13
|
+
* `export let`, type from the preceding JSDoc) without running anything;
|
|
14
|
+
* - generates the virtual module `pano:views` (`ViewDef[]`, every def carries `pluginId`);
|
|
15
|
+
* - rewrites every import of a view file to a proxy module
|
|
16
|
+
* `export * from "<file>"; export default createViewProxy("<ns>:<Name>", Default)`;
|
|
17
|
+
* - writes `contract/views.json` (the record shape of doc 01 section 1) and a readable
|
|
18
|
+
* copy of every view to `contract/src/<path under src/theme>` (after the optional
|
|
19
|
+
* `transformSource` step, which is where the root-class step of doc 03 plugs in);
|
|
20
|
+
* - keeps section `views` of `pano-plugin.lock.json`: a watch build warns, a build fails.
|
|
21
|
+
*
|
|
22
|
+
* Errors name the file, the line and the fix.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const VIRTUAL_VIEWS_ID = "\0pano:views";
|
|
26
|
+
export const PROXY_PREFIX = "\0pano-view:";
|
|
27
|
+
|
|
28
|
+
const VIEW_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
29
|
+
const SLOT_ID = /^[a-z0-9][a-z0-9:-]*$/;
|
|
30
|
+
const SDK_VIEWS_SPECIFIER = "@panomc/sdk/views";
|
|
31
|
+
const DEFAULT_INJECT_PRIORITY = 10;
|
|
32
|
+
|
|
33
|
+
/** Keys of `export const view` (doc 01 section 2). */
|
|
34
|
+
const VIEW_KEYS = new Set([
|
|
35
|
+
"contract",
|
|
36
|
+
"path",
|
|
37
|
+
"systemLayout",
|
|
38
|
+
"layout",
|
|
39
|
+
"permission",
|
|
40
|
+
"loginRequired",
|
|
41
|
+
"resetLayout",
|
|
42
|
+
"controller",
|
|
43
|
+
"slot",
|
|
44
|
+
"hook",
|
|
45
|
+
"sidebar",
|
|
46
|
+
"id",
|
|
47
|
+
"priority",
|
|
48
|
+
"skipLoad",
|
|
49
|
+
"block",
|
|
50
|
+
"home",
|
|
51
|
+
"widget",
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const PAGE_KEYS = ["systemLayout", "layout", "permission", "loginRequired", "resetLayout"];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @typedef {object} PanoViewsOptions
|
|
58
|
+
* @property {string[]} dirs view folders, relative to `root` (or absolute)
|
|
59
|
+
* @property {string} namespace plugin namespace (checked against the reserved list)
|
|
60
|
+
* @property {string} pluginId full plugin id, carried by every view def
|
|
61
|
+
* @property {string} [root] plugin root, default the current directory
|
|
62
|
+
* @property {string} [outDir] folder that receives `contract/`, default `<root>/src/main/resources/plugin-ui`
|
|
63
|
+
* @property {(source: string, ctx: { ns: string, view: string, file: string }) => string | Promise<string>} [transformSource]
|
|
64
|
+
* runs on the source written to `contract/src` (never on the compiled code)
|
|
65
|
+
* @property {boolean} [watch] treat the build as a watch build (default: rollup's `meta.watchMode`)
|
|
66
|
+
* @property {'server' | 'client' | 'controllers' | 'widgets'} [side] the build this plugin runs in; the server build writes no readable copies and does not prune
|
|
67
|
+
* @property {boolean} [lock] keep `pano-plugin.lock.json` (default true)
|
|
68
|
+
* @property {string} [version] plugin version for `views.json` (default: `version` of gradle.properties)
|
|
69
|
+
* @property {string} [viewsSpecifier] where `createViewProxy` comes from (default `@panomc/sdk/views`)
|
|
70
|
+
* @property {(info: ViewInfo) => string[] | undefined} [classes]
|
|
71
|
+
* the classes the lock keeps for a view (doc 03 section 1.3: root class always, parts with the badge);
|
|
72
|
+
* without it the lock carries the stored `classes` over untouched
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @typedef {object} ViewInfo
|
|
77
|
+
* @property {string} name basename without `.svelte`
|
|
78
|
+
* @property {string} id `<ns>:<Name>`
|
|
79
|
+
* @property {string} file absolute path
|
|
80
|
+
* @property {string} rel path relative to the plugin root
|
|
81
|
+
* @property {string} source file content
|
|
82
|
+
* @property {string} contractPath path of the readable copy relative to `contract/`
|
|
83
|
+
* @property {Record<string, any>} record the entry of `contract/views.json`
|
|
84
|
+
* @property {Record<string, any>} def the `ViewDef` fields except `component`
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
/** A build error with a location; turned into `this.error` by the plugin. */
|
|
88
|
+
export class ViewsError extends Error {
|
|
89
|
+
/**
|
|
90
|
+
* @param {string} message
|
|
91
|
+
* @param {{ file?: string, line?: number }} [where]
|
|
92
|
+
*/
|
|
93
|
+
constructor(message, where = {}) {
|
|
94
|
+
super(message);
|
|
95
|
+
this.name = "ViewsError";
|
|
96
|
+
this.file = where.file;
|
|
97
|
+
this.line = where.line;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} p
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
const slash = (p) => p.replace(/\\/g, "/");
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} source
|
|
109
|
+
* @param {number} offset
|
|
110
|
+
* @returns {number} 1-based line
|
|
111
|
+
*/
|
|
112
|
+
function lineOf(source, offset) {
|
|
113
|
+
let line = 1;
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < offset && i < source.length; i += 1) {
|
|
116
|
+
if (source.charCodeAt(i) === 10) line += 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return line;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Loads `svelte/compiler`: the plugin's own copy first (the kit lists svelte as a peer),
|
|
124
|
+
* then the one next to the kit.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} root
|
|
127
|
+
* @returns {Promise<{ parse: Function }>}
|
|
128
|
+
*/
|
|
129
|
+
async function loadCompiler(root) {
|
|
130
|
+
// Under Node `require.resolve("svelte/compiler")` answers the CommonJS build, whose `import()` has only
|
|
131
|
+
// `{ default }`: import the compiler's ES build from the plugin's own svelte instead. Bun's import of the
|
|
132
|
+
// resolved file has the named exports, so it keeps today's path.
|
|
133
|
+
if (!process.versions.bun) {
|
|
134
|
+
try {
|
|
135
|
+
const dir = path.dirname(createRequire(path.join(root, "noop.js")).resolve("svelte/package.json"));
|
|
136
|
+
const module = await import(pathToFileURL(path.join(dir, "src/compiler/index.js")).href);
|
|
137
|
+
|
|
138
|
+
if (typeof module.parse === "function") return module;
|
|
139
|
+
} catch {
|
|
140
|
+
// fall through to the path below
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const resolved = createRequire(path.join(root, "noop.js")).resolve("svelte/compiler");
|
|
146
|
+
|
|
147
|
+
return await import(pathToFileURL(resolved).href);
|
|
148
|
+
} catch {
|
|
149
|
+
return await import("svelte/compiler");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {string} dir
|
|
155
|
+
* @param {string[]} out
|
|
156
|
+
*/
|
|
157
|
+
function walk(dir, out) {
|
|
158
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1));
|
|
159
|
+
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
162
|
+
|
|
163
|
+
const full = path.join(dir, entry.name);
|
|
164
|
+
|
|
165
|
+
if (entry.isDirectory()) walk(full, out);
|
|
166
|
+
else if (entry.isFile() && entry.name.endsWith(".svelte")) out.push(full);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Thrown by `literal` with the dotted key path of the first non-literal value. */
|
|
171
|
+
class NotLiteral extends Error {
|
|
172
|
+
/**
|
|
173
|
+
* @param {string} where
|
|
174
|
+
* @param {number} start
|
|
175
|
+
*/
|
|
176
|
+
constructor(where, start) {
|
|
177
|
+
super(where);
|
|
178
|
+
this.where = where;
|
|
179
|
+
this.start = start;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Evaluates a JSON-like AST node (strings, numbers, booleans, null, arrays, objects, plain
|
|
185
|
+
* template literals, signed numbers) without running code.
|
|
186
|
+
*
|
|
187
|
+
* @param {any} node
|
|
188
|
+
* @param {string} where dotted key path, for the error
|
|
189
|
+
* @returns {any}
|
|
190
|
+
*/
|
|
191
|
+
function literal(node, where) {
|
|
192
|
+
switch (node?.type) {
|
|
193
|
+
case "Literal":
|
|
194
|
+
if (node.regex || typeof node.value === "bigint") break;
|
|
195
|
+
return node.value;
|
|
196
|
+
|
|
197
|
+
case "TemplateLiteral":
|
|
198
|
+
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
|
|
199
|
+
break;
|
|
200
|
+
|
|
201
|
+
case "UnaryExpression":
|
|
202
|
+
if (
|
|
203
|
+
(node.operator === "-" || node.operator === "+") &&
|
|
204
|
+
node.argument?.type === "Literal" &&
|
|
205
|
+
typeof node.argument.value === "number"
|
|
206
|
+
) {
|
|
207
|
+
return node.operator === "-" ? -node.argument.value : node.argument.value;
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
|
|
211
|
+
case "ArrayExpression":
|
|
212
|
+
return node.elements.map((element, index) => {
|
|
213
|
+
if (!element || element.type === "SpreadElement") {
|
|
214
|
+
throw new NotLiteral(`${where}[${index}]`, node.start);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return literal(element, `${where}[${index}]`);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
case "ObjectExpression": {
|
|
221
|
+
/** @type {Record<string, any>} */
|
|
222
|
+
const out = {};
|
|
223
|
+
|
|
224
|
+
for (const property of node.properties) {
|
|
225
|
+
if (
|
|
226
|
+
property.type !== "Property" ||
|
|
227
|
+
property.computed ||
|
|
228
|
+
property.kind !== "init" ||
|
|
229
|
+
property.method ||
|
|
230
|
+
property.shorthand
|
|
231
|
+
) {
|
|
232
|
+
throw new NotLiteral(where, property.start ?? node.start);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const key =
|
|
236
|
+
property.key.type === "Identifier"
|
|
237
|
+
? property.key.name
|
|
238
|
+
: property.key.type === "Literal"
|
|
239
|
+
? String(property.key.value)
|
|
240
|
+
: null;
|
|
241
|
+
|
|
242
|
+
if (key === null) throw new NotLiteral(where, property.start);
|
|
243
|
+
|
|
244
|
+
out[key] = literal(property.value, where ? `${where}.${key}` : key);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
default:
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
throw new NotLiteral(where, node?.start ?? 0);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The JSDoc block that ends right before `offset` (only whitespace between), or "".
|
|
259
|
+
*
|
|
260
|
+
* @param {string} source
|
|
261
|
+
* @param {number} offset
|
|
262
|
+
* @returns {string}
|
|
263
|
+
*/
|
|
264
|
+
function jsdocBefore(source, offset) {
|
|
265
|
+
const before = source.slice(0, offset).trimEnd();
|
|
266
|
+
|
|
267
|
+
if (!before.endsWith("*/")) return "";
|
|
268
|
+
|
|
269
|
+
const start = before.lastIndexOf("/*");
|
|
270
|
+
|
|
271
|
+
if (start < 0 || !before.startsWith("/**", start)) return "";
|
|
272
|
+
|
|
273
|
+
return before.slice(start, before.length);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The text inside the braces of the first `@type {...}` of a JSDoc block.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} doc
|
|
280
|
+
* @returns {string | null}
|
|
281
|
+
*/
|
|
282
|
+
function typeTag(doc) {
|
|
283
|
+
const at = doc.search(/@type\s*\{/);
|
|
284
|
+
|
|
285
|
+
if (at < 0) return null;
|
|
286
|
+
|
|
287
|
+
let depth = 0;
|
|
288
|
+
const open = doc.indexOf("{", at);
|
|
289
|
+
|
|
290
|
+
for (let i = open; i < doc.length; i += 1) {
|
|
291
|
+
if (doc[i] === "{") depth += 1;
|
|
292
|
+
else if (doc[i] === "}") {
|
|
293
|
+
depth -= 1;
|
|
294
|
+
|
|
295
|
+
if (depth === 0) {
|
|
296
|
+
return doc
|
|
297
|
+
.slice(open + 1, i)
|
|
298
|
+
.replace(/^\s*\*\s?/gm, " ")
|
|
299
|
+
.replace(/\s+/g, " ")
|
|
300
|
+
.trim();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Splits `{ a: string, b?: Product[] }` into its members; null when `type` is not an
|
|
310
|
+
* object type literal.
|
|
311
|
+
*
|
|
312
|
+
* @param {string} type
|
|
313
|
+
* @returns {Record<string, { type: string, optional: boolean }> | null}
|
|
314
|
+
*/
|
|
315
|
+
function objectTypeMembers(type) {
|
|
316
|
+
const text = type.trim();
|
|
317
|
+
|
|
318
|
+
if (!text.startsWith("{") || !text.endsWith("}")) return null;
|
|
319
|
+
|
|
320
|
+
const inner = text.slice(1, -1);
|
|
321
|
+
const parts = [];
|
|
322
|
+
let depth = 0;
|
|
323
|
+
let quote = "";
|
|
324
|
+
let current = "";
|
|
325
|
+
|
|
326
|
+
for (let i = 0; i < inner.length; i += 1) {
|
|
327
|
+
const char = inner[i];
|
|
328
|
+
|
|
329
|
+
if (quote) {
|
|
330
|
+
current += char;
|
|
331
|
+
if (char === quote && inner[i - 1] !== "\\") quote = "";
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
336
|
+
quote = char;
|
|
337
|
+
current += char;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if ("{[(<".includes(char)) depth += 1;
|
|
342
|
+
else if ("}])".includes(char)) depth -= 1;
|
|
343
|
+
else if (char === ">" && inner[i - 1] !== "=") depth -= 1;
|
|
344
|
+
|
|
345
|
+
if (depth === 0 && (char === "," || char === ";")) {
|
|
346
|
+
parts.push(current);
|
|
347
|
+
current = "";
|
|
348
|
+
} else {
|
|
349
|
+
current += char;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
parts.push(current);
|
|
354
|
+
|
|
355
|
+
/** @type {Record<string, { type: string, optional: boolean }>} */
|
|
356
|
+
const members = {};
|
|
357
|
+
|
|
358
|
+
for (const part of parts) {
|
|
359
|
+
const match = /^\s*(?:readonly\s+)?(["']?)([A-Za-z_$][\w$-]*)\1\s*(\?)?\s*:\s*([\s\S]+?)\s*$/.exec(part);
|
|
360
|
+
|
|
361
|
+
if (match) members[match[2]] = { type: match[4].replace(/\s+/g, " "), optional: Boolean(match[3]) };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return members;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Props of a view: `let { a, b = 1, ...rest } = $props()` or `export let a`.
|
|
369
|
+
* `required` = no default value and not marked `?` in the JSDoc type.
|
|
370
|
+
*
|
|
371
|
+
* @param {any[]} body statements of the instance script
|
|
372
|
+
* @param {string} source
|
|
373
|
+
* @returns {Record<string, { type: string, required: boolean }>}
|
|
374
|
+
*/
|
|
375
|
+
function readProps(body, source) {
|
|
376
|
+
/** @type {Record<string, { type: string, required: boolean }>} */
|
|
377
|
+
const props = {};
|
|
378
|
+
|
|
379
|
+
for (const statement of body) {
|
|
380
|
+
if (statement.type === "VariableDeclaration") {
|
|
381
|
+
for (const declarator of statement.declarations) {
|
|
382
|
+
const init = declarator.init;
|
|
383
|
+
|
|
384
|
+
if (
|
|
385
|
+
init?.type !== "CallExpression" ||
|
|
386
|
+
init.callee?.type !== "Identifier" ||
|
|
387
|
+
init.callee.name !== "$props"
|
|
388
|
+
) {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const tag = typeTag(jsdocBefore(source, statement.start));
|
|
393
|
+
const members = (tag && objectTypeMembers(tag)) || {};
|
|
394
|
+
|
|
395
|
+
if (declarator.id.type === "ObjectPattern") {
|
|
396
|
+
for (const property of declarator.id.properties) {
|
|
397
|
+
if (property.type !== "Property" || property.computed) continue;
|
|
398
|
+
|
|
399
|
+
const name = property.key.type === "Identifier" ? property.key.name : String(property.key.value);
|
|
400
|
+
const hasDefault = property.value.type === "AssignmentPattern";
|
|
401
|
+
const member = members[name];
|
|
402
|
+
|
|
403
|
+
props[name] = {
|
|
404
|
+
type: member?.type ?? "any",
|
|
405
|
+
required: !hasDefault && !member?.optional,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
} else {
|
|
409
|
+
for (const [name, member] of Object.entries(members)) {
|
|
410
|
+
props[name] = { type: member.type, required: !member.optional };
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
} else if (
|
|
415
|
+
statement.type === "ExportNamedDeclaration" &&
|
|
416
|
+
statement.declaration?.type === "VariableDeclaration" &&
|
|
417
|
+
statement.declaration.kind !== "const"
|
|
418
|
+
) {
|
|
419
|
+
const tag = typeTag(jsdocBefore(source, statement.start));
|
|
420
|
+
|
|
421
|
+
for (const declarator of statement.declaration.declarations) {
|
|
422
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
423
|
+
|
|
424
|
+
props[declarator.id.name] = { type: tag ?? "any", required: declarator.init === null };
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return props;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Literal value of an attribute node of a component, or `undefined` when it is not literal.
|
|
434
|
+
*
|
|
435
|
+
* @param {any} attribute
|
|
436
|
+
* @returns {string | undefined}
|
|
437
|
+
*/
|
|
438
|
+
function literalAttribute(attribute) {
|
|
439
|
+
const value = attribute.value;
|
|
440
|
+
|
|
441
|
+
if (value === true) return undefined;
|
|
442
|
+
|
|
443
|
+
if (Array.isArray(value)) {
|
|
444
|
+
if (value.length === 1 && value[0].type === "Text") return value[0].data;
|
|
445
|
+
if (value.length === 1 && value[0].type === "ExpressionTag") return literalExpression(value[0].expression);
|
|
446
|
+
|
|
447
|
+
return undefined;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (value?.type === "ExpressionTag") return literalExpression(value.expression);
|
|
451
|
+
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @param {any} expression
|
|
457
|
+
* @returns {string | undefined}
|
|
458
|
+
*/
|
|
459
|
+
function literalExpression(expression) {
|
|
460
|
+
if (expression?.type === "Literal" && typeof expression.value === "string") return expression.value;
|
|
461
|
+
|
|
462
|
+
if (expression?.type === "TemplateLiteral" && expression.expressions.length === 0) {
|
|
463
|
+
return expression.quasis[0].value.cooked;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return undefined;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Visits every template node.
|
|
471
|
+
*
|
|
472
|
+
* @param {any} node
|
|
473
|
+
* @param {(node: any) => void} visit
|
|
474
|
+
*/
|
|
475
|
+
function walkTemplate(node, visit) {
|
|
476
|
+
if (!node || typeof node !== "object") return;
|
|
477
|
+
|
|
478
|
+
if (Array.isArray(node)) {
|
|
479
|
+
for (const child of node) walkTemplate(child, visit);
|
|
480
|
+
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (typeof node.type === "string") visit(node);
|
|
485
|
+
|
|
486
|
+
for (const [key, value] of Object.entries(node)) {
|
|
487
|
+
if (key === "metadata" || key === "loc" || key === "parent") continue;
|
|
488
|
+
if (value && typeof value === "object") walkTemplate(value, visit);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @param {string} message
|
|
494
|
+
* @param {string} file
|
|
495
|
+
* @param {number} line
|
|
496
|
+
*/
|
|
497
|
+
const fail = (message, file, line) => {
|
|
498
|
+
throw new ViewsError(`${file}:${line} ${message}`, { file, line });
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Parses one view file into its record, its def and the facts the cross checks need.
|
|
503
|
+
*
|
|
504
|
+
* @param {{ parse: Function }} compiler
|
|
505
|
+
* @param {string} file absolute path
|
|
506
|
+
* @param {string} rel path relative to the root, for messages
|
|
507
|
+
* @param {string} source
|
|
508
|
+
* @param {{ ns: string, name: string }} names
|
|
509
|
+
* @param {string[]} warnings
|
|
510
|
+
*/
|
|
511
|
+
function parseView(compiler, file, rel, source, names, warnings) {
|
|
512
|
+
/** @type {any} */
|
|
513
|
+
let ast;
|
|
514
|
+
|
|
515
|
+
try {
|
|
516
|
+
ast = compiler.parse(source, { modern: true });
|
|
517
|
+
} catch (error) {
|
|
518
|
+
const line = error?.start?.line ?? error?.position?.[0] ?? 1;
|
|
519
|
+
|
|
520
|
+
fail(
|
|
521
|
+
`${error?.message ?? error} — fix the syntax error; this file is read by the plugin build`,
|
|
522
|
+
rel,
|
|
523
|
+
typeof line === "number" ? line : 1,
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// export const view
|
|
528
|
+
/** @type {Record<string, any>} */
|
|
529
|
+
let view = {};
|
|
530
|
+
let viewLine = 1;
|
|
531
|
+
|
|
532
|
+
for (const statement of ast.module?.content?.body ?? []) {
|
|
533
|
+
if (statement.type !== "ExportNamedDeclaration") continue;
|
|
534
|
+
|
|
535
|
+
const declaration = statement.declaration;
|
|
536
|
+
const declaresView =
|
|
537
|
+
declaration?.type === "VariableDeclaration" &&
|
|
538
|
+
declaration.declarations.some((d) => d.id?.name === "view");
|
|
539
|
+
const specifiesView = statement.specifiers?.some((s) => s.exported?.name === "view");
|
|
540
|
+
|
|
541
|
+
if (specifiesView) {
|
|
542
|
+
fail(
|
|
543
|
+
"export { view } cannot be read — declare it in place as: export const view = { ... } with literal values",
|
|
544
|
+
rel,
|
|
545
|
+
lineOf(source, statement.start),
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (!declaresView) continue;
|
|
550
|
+
|
|
551
|
+
viewLine = lineOf(source, statement.start);
|
|
552
|
+
|
|
553
|
+
const declarator = declaration.declarations.find((d) => d.id?.name === "view");
|
|
554
|
+
|
|
555
|
+
if (declaration.kind !== "const" || declarator.init?.type !== "ObjectExpression") {
|
|
556
|
+
fail(
|
|
557
|
+
"export const view must be an object literal — write: export const view = { path: \"/x\" }",
|
|
558
|
+
rel,
|
|
559
|
+
viewLine,
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
try {
|
|
564
|
+
view = literal(declarator.init, "view");
|
|
565
|
+
} catch (error) {
|
|
566
|
+
if (!(error instanceof NotLiteral)) throw error;
|
|
567
|
+
|
|
568
|
+
fail(
|
|
569
|
+
`export const view is not a literal (${error.where}) — the build reads it without running it; use only strings, numbers, booleans, arrays and objects, and move computed values elsewhere`,
|
|
570
|
+
rel,
|
|
571
|
+
lineOf(source, error.start),
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
for (const key of Object.keys(view)) {
|
|
577
|
+
if (!VIEW_KEYS.has(key)) {
|
|
578
|
+
warnings.push(
|
|
579
|
+
`${rel}:${viewLine} unknown key "${key}" in export const view — it is ignored (known: ${[...VIEW_KEYS].join(", ")})`,
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (view.contract !== undefined && (!Number.isInteger(view.contract) || view.contract < 1)) {
|
|
585
|
+
fail("view.contract must be a whole number of 1 or more — write: contract: 2", rel, viewLine);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (view.path !== undefined && (typeof view.path !== "string" || !view.path.startsWith("/"))) {
|
|
589
|
+
fail('view.path must be a string that starts with "/" — write: path: "/hello"', rel, viewLine);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
for (const key of ["slot", "hook", "sidebar"]) {
|
|
593
|
+
const value = view[key];
|
|
594
|
+
|
|
595
|
+
if (
|
|
596
|
+
value !== undefined &&
|
|
597
|
+
!(typeof value === "string" || (Array.isArray(value) && value.every((v) => typeof v === "string")))
|
|
598
|
+
) {
|
|
599
|
+
fail(`view.${key} must be a string or an array of strings`, rel, viewLine);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// props
|
|
604
|
+
const props = readProps(ast.instance?.content?.body ?? [], source);
|
|
605
|
+
|
|
606
|
+
// slots, hooks, child views
|
|
607
|
+
const slots = new Set();
|
|
608
|
+
const hooks = new Set();
|
|
609
|
+
|
|
610
|
+
walkTemplate(ast.fragment, (node) => {
|
|
611
|
+
if (node.type !== "Component") return;
|
|
612
|
+
|
|
613
|
+
if (node.name === "PluginSlot") {
|
|
614
|
+
const line = lineOf(source, node.start);
|
|
615
|
+
const attribute = node.attributes.find((a) => a.type === "Attribute" && a.name === "id");
|
|
616
|
+
const id = attribute ? literalAttribute(attribute) : undefined;
|
|
617
|
+
|
|
618
|
+
if (id === undefined) {
|
|
619
|
+
fail(
|
|
620
|
+
`<PluginSlot> needs a literal id — write id="${names.ns}:<slot>" (the build lists slot ids in the view contract; a computed id cannot be listed)`,
|
|
621
|
+
rel,
|
|
622
|
+
line,
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (!id.startsWith(`${names.ns}:`) || !SLOT_ID.test(id) || id.length === names.ns.length + 1) {
|
|
627
|
+
fail(
|
|
628
|
+
`<PluginSlot id="${id}"> is outside this plugin's namespace — slot ids of ${names.ns} look like "${names.ns}:<slot>" (lowercase letters, digits, ":" and "-"); rename it to "${names.ns}:${id.split(":").pop() || "slot"}"`,
|
|
629
|
+
rel,
|
|
630
|
+
line,
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
slots.add(id);
|
|
635
|
+
} else if (node.name === "Hook") {
|
|
636
|
+
const attribute = node.attributes.find((a) => a.type === "Attribute" && a.name === "name");
|
|
637
|
+
const name = attribute ? literalAttribute(attribute) : undefined;
|
|
638
|
+
|
|
639
|
+
if (name !== undefined) hooks.add(name);
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
const imports = [];
|
|
644
|
+
|
|
645
|
+
for (const script of [ast.module, ast.instance]) {
|
|
646
|
+
for (const statement of script?.content?.body ?? []) {
|
|
647
|
+
if (statement.type === "ImportDeclaration" && typeof statement.source.value === "string") {
|
|
648
|
+
imports.push(statement.source.value);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
return { view, viewLine, props, slots: [...slots].sort(), hooks: [...hooks].sort(), imports };
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* The `ViewDef` fields (`inject`, `page`, `home`) of one view from its `export const view`.
|
|
658
|
+
*
|
|
659
|
+
* @param {Record<string, any>} view
|
|
660
|
+
* @param {string} id
|
|
661
|
+
* @param {string} rel
|
|
662
|
+
* @param {number} line
|
|
663
|
+
* @param {string[]} warnings
|
|
664
|
+
*/
|
|
665
|
+
function describeView(view, id, rel, line, warnings) {
|
|
666
|
+
/** @type {Record<string, any> | null} */
|
|
667
|
+
let page = null;
|
|
668
|
+
|
|
669
|
+
if (view.path !== undefined) {
|
|
670
|
+
page = { path: view.path };
|
|
671
|
+
|
|
672
|
+
for (const key of PAGE_KEYS) if (view[key] !== undefined) page[key] = view[key];
|
|
673
|
+
|
|
674
|
+
if (view.controller !== undefined) page.controller = view.controller;
|
|
675
|
+
} else if (view.controller !== undefined) {
|
|
676
|
+
warnings.push(`${rel}:${line} view.controller is ignored without view.path (controllers load page data)`);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** @type {Record<string, any>[]} */
|
|
680
|
+
const inject = [];
|
|
681
|
+
const priority = typeof view.priority === "number" ? view.priority : DEFAULT_INJECT_PRIORITY;
|
|
682
|
+
|
|
683
|
+
for (const kind of ["slot", "hook", "sidebar"]) {
|
|
684
|
+
if (view[kind] === undefined) continue;
|
|
685
|
+
|
|
686
|
+
const targets = Array.isArray(view[kind]) ? view[kind] : [view[kind]];
|
|
687
|
+
|
|
688
|
+
for (const target of targets) {
|
|
689
|
+
const item = { [kind]: target, id: typeof view.id === "string" ? view.id : id, priority };
|
|
690
|
+
|
|
691
|
+
if (view.skipLoad !== undefined) item.skipLoad = view.skipLoad;
|
|
692
|
+
|
|
693
|
+
inject.push(item);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
let home = null;
|
|
698
|
+
|
|
699
|
+
if (view.home !== undefined) {
|
|
700
|
+
if (typeof view.home !== "object" || view.home === null || typeof view.home.label !== "string") {
|
|
701
|
+
fail('view.home must be { label: "..." } — the label is what the admin sees in the home page list', rel, line);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
home = { label: view.home.label };
|
|
705
|
+
|
|
706
|
+
if (!page) warnings.push(`${rel}:${line} view.home is offered but this view has no path; add path: "/..."`);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return {
|
|
710
|
+
page,
|
|
711
|
+
inject: inject.length > 0 ? inject : null,
|
|
712
|
+
home,
|
|
713
|
+
block: view.block === true || (view.widget !== undefined && view.widget !== null && view.widget !== false),
|
|
714
|
+
widget: view.widget !== undefined ? view.widget : null,
|
|
715
|
+
contract: view.contract ?? 1,
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Where the readable copy of a view lives, relative to `contract/`: the path under
|
|
721
|
+
* `src/theme/` (so relative imports between views keep working), else under `src/`.
|
|
722
|
+
*
|
|
723
|
+
* @param {string} root
|
|
724
|
+
* @param {string} file
|
|
725
|
+
* @returns {string}
|
|
726
|
+
*/
|
|
727
|
+
function contractPathOf(root, file) {
|
|
728
|
+
const rel = slash(path.relative(root, file));
|
|
729
|
+
|
|
730
|
+
for (const prefix of ["src/theme/", "src/"]) {
|
|
731
|
+
if (rel.startsWith(prefix)) return `src/${rel.slice(prefix.length)}`;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
return `src/${path.basename(file)}`;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* @param {string} dir
|
|
739
|
+
* @param {string[]} out
|
|
740
|
+
*/
|
|
741
|
+
function walkJs(dir, out) {
|
|
742
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
743
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
744
|
+
|
|
745
|
+
const full = path.join(dir, entry.name);
|
|
746
|
+
|
|
747
|
+
if (entry.isDirectory()) walkJs(full, out);
|
|
748
|
+
else if (entry.isFile() && /\.(js|mjs)$/.test(entry.name)) out.push(full);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Rule of doc 01 section 2: `view: "<own ns>:X"` in `src/**\/*.js` must name a view of this plugin.
|
|
754
|
+
*
|
|
755
|
+
* @param {string} root
|
|
756
|
+
* @param {string[]} prefixes `<ns>:` and `<pluginId>:`
|
|
757
|
+
* @param {Set<string>} names view names of this plugin
|
|
758
|
+
*/
|
|
759
|
+
function checkViewLiterals(root, prefixes, names) {
|
|
760
|
+
const src = path.join(root, "src");
|
|
761
|
+
|
|
762
|
+
if (!fs.existsSync(src)) return;
|
|
763
|
+
|
|
764
|
+
const files = [];
|
|
765
|
+
|
|
766
|
+
walkJs(src, files);
|
|
767
|
+
|
|
768
|
+
for (const file of files) {
|
|
769
|
+
const text = fs.readFileSync(file, "utf8");
|
|
770
|
+
|
|
771
|
+
for (const match of text.matchAll(/\bview\s*:\s*(["'`])([^"'`\n]+)\1/g)) {
|
|
772
|
+
const value = match[2];
|
|
773
|
+
const prefix = prefixes.find((p) => value.startsWith(p));
|
|
774
|
+
|
|
775
|
+
if (!prefix) continue;
|
|
776
|
+
|
|
777
|
+
const name = value.slice(prefix.length);
|
|
778
|
+
|
|
779
|
+
if (!names.has(name)) {
|
|
780
|
+
fail(
|
|
781
|
+
`view: "${value}" names no view of this plugin — create ${name}.svelte in a view folder, or fix the name`,
|
|
782
|
+
slash(path.relative(root, file)),
|
|
783
|
+
lineOf(text, match.index),
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Scans the view folders of a plugin. Pure: reads files, writes nothing.
|
|
792
|
+
*
|
|
793
|
+
* @param {Pick<PanoViewsOptions, "dirs" | "namespace" | "pluginId" | "root" | "version">} options
|
|
794
|
+
* @returns {Promise<{ namespace: string, pluginId: string, version: string, views: Map<string, ViewInfo>, byFile: Map<string, ViewInfo>, warnings: string[], missingDirs: string[] }>}
|
|
795
|
+
*/
|
|
796
|
+
export async function scanViews(options) {
|
|
797
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
798
|
+
/** @type {string[]} */
|
|
799
|
+
const warnings = [];
|
|
800
|
+
/** @type {string[]} */
|
|
801
|
+
const missingDirs = [];
|
|
802
|
+
|
|
803
|
+
let namespace;
|
|
804
|
+
|
|
805
|
+
try {
|
|
806
|
+
namespace = namespaceOf(options.pluginId, { namespace: options.namespace });
|
|
807
|
+
} catch (error) {
|
|
808
|
+
throw new ViewsError(String(error.message).replace(/^\[pano-plugin\] /, ""), { file: "pano.plugin.js", line: 1 });
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const compiler = await loadCompiler(root);
|
|
812
|
+
const files = [];
|
|
813
|
+
|
|
814
|
+
for (const dir of options.dirs) {
|
|
815
|
+
const absolute = path.resolve(root, dir);
|
|
816
|
+
|
|
817
|
+
if (!fs.existsSync(absolute)) {
|
|
818
|
+
missingDirs.push(dir);
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
walk(absolute, files);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/** @type {Map<string, ViewInfo>} */
|
|
826
|
+
const views = new Map();
|
|
827
|
+
/** @type {Map<string, ViewInfo>} */
|
|
828
|
+
const byFile = new Map();
|
|
829
|
+
/** @type {Map<string, string[]>} */
|
|
830
|
+
const importsOf = new Map();
|
|
831
|
+
|
|
832
|
+
for (const file of files) {
|
|
833
|
+
const rel = slash(path.relative(root, file));
|
|
834
|
+
const name = path.basename(file, ".svelte");
|
|
835
|
+
|
|
836
|
+
if (!VIEW_NAME.test(name)) {
|
|
837
|
+
fail(
|
|
838
|
+
`view file names must start with an uppercase letter and use letters and digits only (the file name is the view name) — rename it, for example to ${name.replace(/(^|[^A-Za-z0-9]+)([A-Za-z0-9])/g, (_, __, c) => c.toUpperCase()) || "View"}.svelte`,
|
|
839
|
+
rel,
|
|
840
|
+
1,
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
const existing = views.get(name);
|
|
845
|
+
|
|
846
|
+
if (existing) {
|
|
847
|
+
fail(
|
|
848
|
+
`view name "${name}" is already used by ${existing.rel} — the file name is the view name, so rename one of the two files`,
|
|
849
|
+
rel,
|
|
850
|
+
1,
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
const source = fs.readFileSync(file, "utf8");
|
|
855
|
+
const id = `${namespace}:${name}`;
|
|
856
|
+
const parsed = parseView(compiler, file, rel, source, { ns: namespace, name }, warnings);
|
|
857
|
+
const described = describeView(parsed.view, id, rel, parsed.viewLine, warnings);
|
|
858
|
+
const contractPath = contractPathOf(root, file);
|
|
859
|
+
|
|
860
|
+
/** @type {ViewInfo} */
|
|
861
|
+
const info = {
|
|
862
|
+
name,
|
|
863
|
+
id,
|
|
864
|
+
file,
|
|
865
|
+
rel,
|
|
866
|
+
source,
|
|
867
|
+
contractPath,
|
|
868
|
+
record: {
|
|
869
|
+
kind: described.page ? "page" : "component",
|
|
870
|
+
contract: described.contract,
|
|
871
|
+
source: contractPath,
|
|
872
|
+
props: Object.fromEntries(Object.entries(parsed.props).map(([k, v]) => [k, { type: v.type, required: v.required }])),
|
|
873
|
+
uses: [],
|
|
874
|
+
slots: parsed.slots,
|
|
875
|
+
hooks: parsed.hooks,
|
|
876
|
+
block: described.block,
|
|
877
|
+
inject: described.inject,
|
|
878
|
+
page: described.page,
|
|
879
|
+
home: described.home,
|
|
880
|
+
widget: described.widget,
|
|
881
|
+
},
|
|
882
|
+
def: {},
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
views.set(name, info);
|
|
886
|
+
byFile.set(file, info);
|
|
887
|
+
importsOf.set(file, parsed.imports);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// child views (`uses`) and the defs
|
|
891
|
+
for (const info of views.values()) {
|
|
892
|
+
const uses = new Set();
|
|
893
|
+
|
|
894
|
+
for (const specifier of importsOf.get(info.file) ?? []) {
|
|
895
|
+
if (!specifier.startsWith(".") || !specifier.endsWith(".svelte")) continue;
|
|
896
|
+
|
|
897
|
+
const target = byFile.get(path.resolve(path.dirname(info.file), specifier));
|
|
898
|
+
|
|
899
|
+
if (target && target !== info) uses.add(target.id);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
info.record.uses = [...uses].sort();
|
|
903
|
+
info.def = {
|
|
904
|
+
name: info.id,
|
|
905
|
+
pluginId: options.pluginId,
|
|
906
|
+
contract: info.record.contract,
|
|
907
|
+
kind: info.record.kind,
|
|
908
|
+
uses: info.record.uses,
|
|
909
|
+
slots: info.record.slots,
|
|
910
|
+
hooks: info.record.hooks,
|
|
911
|
+
block: info.record.block,
|
|
912
|
+
inject: info.record.inject,
|
|
913
|
+
page: info.record.page,
|
|
914
|
+
home: info.record.home,
|
|
915
|
+
...(info.record.widget !== null ? { widget: info.record.widget } : {}),
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
checkViewLiterals(root, [`${namespace}:`, `${options.pluginId}:`], new Set(views.keys()));
|
|
920
|
+
|
|
921
|
+
return {
|
|
922
|
+
namespace,
|
|
923
|
+
pluginId: options.pluginId,
|
|
924
|
+
version: options.version ?? readProperty(path.join(root, "gradle.properties"), "version") ?? "0.0.0",
|
|
925
|
+
views,
|
|
926
|
+
byFile,
|
|
927
|
+
warnings,
|
|
928
|
+
missingDirs,
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* The `contract/views.json` document.
|
|
934
|
+
*
|
|
935
|
+
* @param {Awaited<ReturnType<typeof scanViews>>} scan
|
|
936
|
+
* @returns {Record<string, any>}
|
|
937
|
+
*/
|
|
938
|
+
export function buildViewsJson(scan) {
|
|
939
|
+
const ids = [...scan.views.values()].map((v) => v.id).sort();
|
|
940
|
+
const byId = new Map([...scan.views.values()].map((v) => [v.id, v]));
|
|
941
|
+
|
|
942
|
+
return {
|
|
943
|
+
namespace: scan.namespace,
|
|
944
|
+
pluginId: scan.pluginId,
|
|
945
|
+
version: scan.version,
|
|
946
|
+
sdk: 2,
|
|
947
|
+
views: Object.fromEntries(ids.map((id) => [id, byId.get(id)?.record])),
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Section `views` of `pano-plugin.lock.json`. `classes` (written by the semantic-class
|
|
953
|
+
* step, doc 03) is carried over from the previous section untouched.
|
|
954
|
+
*
|
|
955
|
+
* @param {Awaited<ReturnType<typeof scanViews>>} scan
|
|
956
|
+
* @param {Record<string, any> | null | undefined} previous
|
|
957
|
+
* @param {(info: ViewInfo) => string[] | undefined} [classesOf] classes to lock for a view (see `PanoViewsOptions.classes`)
|
|
958
|
+
* @returns {Record<string, any>}
|
|
959
|
+
*/
|
|
960
|
+
export function buildLockSection(scan, previous, classesOf) {
|
|
961
|
+
/** @type {Record<string, any>} */
|
|
962
|
+
const section = {};
|
|
963
|
+
|
|
964
|
+
for (const info of scan.views.values()) {
|
|
965
|
+
const { record } = info;
|
|
966
|
+
const entry = {
|
|
967
|
+
contract: record.contract,
|
|
968
|
+
props: Object.fromEntries(Object.entries(record.props).map(([k, v]) => [k, { required: v.required }])),
|
|
969
|
+
slots: record.slots,
|
|
970
|
+
hooks: record.hooks,
|
|
971
|
+
};
|
|
972
|
+
const classes = classesOf?.(info) ?? previous?.[info.id]?.classes;
|
|
973
|
+
|
|
974
|
+
if (Array.isArray(classes)) entry.classes = classes;
|
|
975
|
+
|
|
976
|
+
section[info.id] = entry;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
return section;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/**
|
|
983
|
+
* The `pano:views` module source.
|
|
984
|
+
*
|
|
985
|
+
* @param {Awaited<ReturnType<typeof scanViews>>} scan
|
|
986
|
+
* @returns {string}
|
|
987
|
+
*/
|
|
988
|
+
export function generateViewsModule(scan) {
|
|
989
|
+
const defs = [...scan.views.values()]
|
|
990
|
+
.sort((a, b) => (a.id < b.id ? -1 : 1))
|
|
991
|
+
.map((info) => {
|
|
992
|
+
const body = Object.entries(info.def)
|
|
993
|
+
.map(([key, value]) => ` ${JSON.stringify(key)}: ${JSON.stringify(value)}`)
|
|
994
|
+
.join(",\n");
|
|
995
|
+
|
|
996
|
+
return `{\n${body},\n "component": () => import(${JSON.stringify(info.file)})\n}`;
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
return `export default [\n${defs.join(",\n")}\n];\n`;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* @param {string} file
|
|
1004
|
+
* @param {string} content
|
|
1005
|
+
*/
|
|
1006
|
+
function writeIfChanged(file, content) {
|
|
1007
|
+
try {
|
|
1008
|
+
if (fs.readFileSync(file, "utf8") === content) return;
|
|
1009
|
+
} catch {
|
|
1010
|
+
// not there yet
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
1014
|
+
|
|
1015
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
1016
|
+
|
|
1017
|
+
fs.writeFileSync(temp, content);
|
|
1018
|
+
fs.renameSync(temp, file);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Removes `.svelte` files under `dir` that are not in `keep`, then empty folders.
|
|
1023
|
+
*
|
|
1024
|
+
* @param {string} dir
|
|
1025
|
+
* @param {Set<string>} keep
|
|
1026
|
+
* @returns {boolean} true when `dir` is empty afterwards
|
|
1027
|
+
*/
|
|
1028
|
+
function prune(dir, keep) {
|
|
1029
|
+
if (!fs.existsSync(dir)) return true;
|
|
1030
|
+
|
|
1031
|
+
let empty = true;
|
|
1032
|
+
|
|
1033
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
1034
|
+
const full = path.join(dir, entry.name);
|
|
1035
|
+
|
|
1036
|
+
if (entry.isDirectory()) {
|
|
1037
|
+
if (entry.name === "_lib") empty = false;
|
|
1038
|
+
else if (!prune(full, keep)) empty = false;
|
|
1039
|
+
else fs.rmdirSync(full);
|
|
1040
|
+
} else if (entry.name.endsWith(".svelte") && !keep.has(full)) {
|
|
1041
|
+
fs.rmSync(full);
|
|
1042
|
+
} else {
|
|
1043
|
+
empty = false;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
return empty;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* @param {PanoViewsOptions} options
|
|
1052
|
+
* @returns {import('rollup').Plugin & { api: { scan: () => Awaited<ReturnType<typeof scanViews>> | null, options: PanoViewsOptions } }}
|
|
1053
|
+
*/
|
|
1054
|
+
export function panoViews(options) {
|
|
1055
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
1056
|
+
const outDir = path.resolve(root, options.outDir ?? "src/main/resources/plugin-ui");
|
|
1057
|
+
const sdkViews = options.viewsSpecifier ?? SDK_VIEWS_SPECIFIER;
|
|
1058
|
+
|
|
1059
|
+
/** @type {Awaited<ReturnType<typeof scanViews>> | null} */
|
|
1060
|
+
let scan = null;
|
|
1061
|
+
let watching = false;
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* @param {any} context
|
|
1065
|
+
* @param {any} error
|
|
1066
|
+
*/
|
|
1067
|
+
const raise = (context, error) => {
|
|
1068
|
+
if (error instanceof ViewsError) {
|
|
1069
|
+
// the message already starts with file:line; rollup would repeat it from loc / id
|
|
1070
|
+
context.error({ message: error.message, pluginCode: "PANO_VIEWS" });
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
throw error;
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
return {
|
|
1077
|
+
name: "pano-views",
|
|
1078
|
+
|
|
1079
|
+
api: {
|
|
1080
|
+
scan: () => scan,
|
|
1081
|
+
options,
|
|
1082
|
+
},
|
|
1083
|
+
|
|
1084
|
+
async buildStart() {
|
|
1085
|
+
watching = options.watch ?? Boolean(this.meta?.watchMode);
|
|
1086
|
+
|
|
1087
|
+
try {
|
|
1088
|
+
scan = await scanViews({ ...options, root });
|
|
1089
|
+
} catch (error) {
|
|
1090
|
+
scan = null;
|
|
1091
|
+
raise(this, error);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
for (const dir of scan.missingDirs) {
|
|
1095
|
+
this.warn(`view folder ${dir} does not exist — no views are registered from it (check viewDirs in pano.plugin.js)`);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
for (const warning of scan.warnings) this.warn(warning);
|
|
1099
|
+
|
|
1100
|
+
for (const dir of options.dirs) {
|
|
1101
|
+
const absolute = path.resolve(root, dir);
|
|
1102
|
+
|
|
1103
|
+
if (fs.existsSync(absolute)) this.addWatchFile(absolute);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
for (const info of scan.views.values()) this.addWatchFile(info.file);
|
|
1107
|
+
|
|
1108
|
+
if (options.lock !== false) {
|
|
1109
|
+
let previous;
|
|
1110
|
+
|
|
1111
|
+
try {
|
|
1112
|
+
previous = readLock(root).views;
|
|
1113
|
+
} catch (error) {
|
|
1114
|
+
this.error(error.message);
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
const { breaking } = diffSection("views", previous, buildLockSection(scan, previous, options.classes));
|
|
1118
|
+
|
|
1119
|
+
if (breaking.length > 0) {
|
|
1120
|
+
if (watching) {
|
|
1121
|
+
for (const message of breaking) this.warn(message);
|
|
1122
|
+
} else {
|
|
1123
|
+
this.error(breaking.join("\n"));
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
|
|
1129
|
+
async resolveId(source, importer, resolveOptions) {
|
|
1130
|
+
if (source === "pano:views") return VIRTUAL_VIEWS_ID;
|
|
1131
|
+
|
|
1132
|
+
if (!scan || !importer || importer === VIRTUAL_VIEWS_ID || importer.startsWith(PROXY_PREFIX)) return null;
|
|
1133
|
+
if (!source.endsWith(".svelte")) return null;
|
|
1134
|
+
|
|
1135
|
+
const resolved = await this.resolve(source, importer, { ...resolveOptions, skipSelf: true });
|
|
1136
|
+
|
|
1137
|
+
if (!resolved || resolved.external) return null;
|
|
1138
|
+
|
|
1139
|
+
const file = resolved.id.split("?")[0];
|
|
1140
|
+
|
|
1141
|
+
return scan.byFile.has(file) ? PROXY_PREFIX + file : null;
|
|
1142
|
+
},
|
|
1143
|
+
|
|
1144
|
+
load(id) {
|
|
1145
|
+
if (id === VIRTUAL_VIEWS_ID) return scan ? generateViewsModule(scan) : "export default [];\n";
|
|
1146
|
+
|
|
1147
|
+
if (id.startsWith(PROXY_PREFIX) && scan) {
|
|
1148
|
+
const file = id.slice(PROXY_PREFIX.length);
|
|
1149
|
+
const info = scan.byFile.get(file);
|
|
1150
|
+
|
|
1151
|
+
if (!info) return null;
|
|
1152
|
+
|
|
1153
|
+
const target = JSON.stringify(file);
|
|
1154
|
+
|
|
1155
|
+
return [
|
|
1156
|
+
`import { createViewProxy } from ${JSON.stringify(sdkViews)};`,
|
|
1157
|
+
`import Default from ${target};`,
|
|
1158
|
+
`export * from ${target};`,
|
|
1159
|
+
`export default createViewProxy(${JSON.stringify(info.id)}, Default);`,
|
|
1160
|
+
"",
|
|
1161
|
+
].join("\n");
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
return null;
|
|
1165
|
+
},
|
|
1166
|
+
|
|
1167
|
+
async writeBundle() {
|
|
1168
|
+
if (!scan) return;
|
|
1169
|
+
|
|
1170
|
+
const contract = path.join(outDir, "contract");
|
|
1171
|
+
|
|
1172
|
+
writeIfChanged(path.join(contract, "views.json"), JSON.stringify(buildViewsJson(scan), null, 2) + "\n");
|
|
1173
|
+
|
|
1174
|
+
// The client build runs first and rewrites the readable copies (helper imports); the server build must
|
|
1175
|
+
// not write them again from the original source (FX-08).
|
|
1176
|
+
if (options.side !== "server") {
|
|
1177
|
+
const keep = new Set();
|
|
1178
|
+
|
|
1179
|
+
for (const info of scan.views.values()) {
|
|
1180
|
+
const target = path.join(contract, info.contractPath);
|
|
1181
|
+
let content = info.source;
|
|
1182
|
+
|
|
1183
|
+
if (options.transformSource) {
|
|
1184
|
+
const result = await options.transformSource(info.source, { ns: scan.namespace, view: info.name, file: info.file });
|
|
1185
|
+
|
|
1186
|
+
if (typeof result !== "string") {
|
|
1187
|
+
throw new Error(`transformSource returned ${typeof result} for ${info.rel} — it must return the new source as a string`);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
content = result;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
keep.add(target);
|
|
1194
|
+
writeIfChanged(target, content);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
prune(path.join(contract, "src"), keep);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
if (options.lock !== false && !watching) {
|
|
1201
|
+
writeLockSection(root, "views", buildLockSection(scan, readLock(root).views, options.classes));
|
|
1202
|
+
}
|
|
1203
|
+
},
|
|
1204
|
+
};
|
|
1205
|
+
}
|