@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.62
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 +48 -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 +67 -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,1000 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import MagicString from "magic-string";
|
|
5
|
+
import { parse } from "svelte/compiler";
|
|
6
|
+
import { RUNTIME_FILES, widgetImportPath } from "@panomc/sdk/runtime-specifiers";
|
|
7
|
+
import { loadConfig } from "../config.js";
|
|
8
|
+
import { addRootClass, kebab, SemanticClassCollisionError } from "../styles/semantic-classes.js";
|
|
9
|
+
import { scanViews } from "./views.js";
|
|
10
|
+
import { controllerFiles, resolveProject } from "./rules.js";
|
|
11
|
+
import { getBuildContext } from "./meta.js";
|
|
12
|
+
import { writeIfChanged } from "./controllers.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* panoWidgets (doc 06 section 3.1-3.3): the third rollup target of `panoPlugin()`, web-component widgets.
|
|
16
|
+
*
|
|
17
|
+
* A view whose `export const view` carries `widget` is compiled once more, with the same Svelte options as
|
|
18
|
+
* the client build, for a page that is not a Pano theme:
|
|
19
|
+
*
|
|
20
|
+
* - one entry per widget view: `<View>-<hash>.js` exporting `{ component, load, attrs, session }` (the loader
|
|
21
|
+
* defines the element, so the tag is not baked in), shared code in `chunk-<hash>.js`;
|
|
22
|
+
* - every host specifier (`svelte`, `svelte/*`, `svelte-i18n`, `@panomc/sdk`, `@panomc/sdk/*`) stays external and is
|
|
23
|
+
* rewritten by `output.paths` to the relative runtime URL of `@panomc/widget-host` (`widgetImportPath`), so the
|
|
24
|
+
* page needs no import map; a specifier the widget runtime has no implementation for fails the build;
|
|
25
|
+
* - the `part` step (widget target only): every semantic class `<ns>-...` of an element is copied to its `part`
|
|
26
|
+
* attribute, so a page can style it with `pano-market-goal::part(market-goal__bar)`;
|
|
27
|
+
* - `widgets.json` (`format: 1`, the svelte version the widgets were built for) and `badges.widgets` of
|
|
28
|
+
* `pano-plugin.json`;
|
|
29
|
+
* - `session` is `none`, unless the view says otherwise or a controller of the view calls `host.session`.
|
|
30
|
+
*
|
|
31
|
+
* The target exists only when a view has `widget`: without one the build writes nothing (and removes a stale
|
|
32
|
+
* `widgets/` folder), and the client and server bundles are never touched by this plugin.
|
|
33
|
+
*
|
|
34
|
+
* Put the plugin in the widgets build of `panoPlugin()`; it sets that build's `input` and `output` itself.
|
|
35
|
+
* `hasWidgetViews()` tells `panoPlugin()` whether the build is needed.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export const DEFAULT_OUT_DIR = "src/main/resources/plugin-ui";
|
|
39
|
+
export const WIDGET_ENTRY_PREFIX = "\0pano-widget:";
|
|
40
|
+
export const WIDGETS_JSON_FORMAT = 1;
|
|
41
|
+
|
|
42
|
+
const SESSIONS = ["none", "optional", "required"];
|
|
43
|
+
const PRIMITIVES = new Set(["string", "number", "boolean"]);
|
|
44
|
+
const TAG_SHAPE = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
|
|
45
|
+
const SUFFIX = /(Widget|Block)$/;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} PanoWidgetsOptions
|
|
49
|
+
* @property {string} pluginId full plugin id
|
|
50
|
+
* @property {string} namespace plugin namespace
|
|
51
|
+
* @property {string} [root] plugin root, default the current directory
|
|
52
|
+
* @property {string} [outDir] package folder, default `src/main/resources/plugin-ui`
|
|
53
|
+
* @property {string[]} [viewDirs] view folders, default `viewDirs` of the plugin configuration
|
|
54
|
+
* @property {string} [side] set by `panoPlugin()`
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @typedef {object} WidgetAttr
|
|
59
|
+
* @property {string} name the prop as written in the view
|
|
60
|
+
* @property {string} attribute kebab-case attribute of the element
|
|
61
|
+
* @property {'string' | 'number' | 'boolean'} type
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {object} WidgetDef
|
|
66
|
+
* @property {string} view view name (`GoalWidget`)
|
|
67
|
+
* @property {string} id view id (`market:GoalWidget`)
|
|
68
|
+
* @property {string} file absolute path
|
|
69
|
+
* @property {string} rel path relative to the plugin root
|
|
70
|
+
* @property {string} tag short tag (`goal`); the element is `pano-<ns>-<tag>`
|
|
71
|
+
* @property {string} element the full element name
|
|
72
|
+
* @property {WidgetAttr[]} attrs
|
|
73
|
+
* @property {'none' | 'optional' | 'required' | null} session what the author wrote, null = inferred
|
|
74
|
+
* @property {boolean} hasLoad the view has a module-level `load`
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `GoalWidget` -> `goal`, `RecentBuyersWidget` -> `recent-buyers`, `TopBlock` -> `top`.
|
|
79
|
+
* A name that is only the suffix keeps it.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} view
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
export function defaultTagOf(view) {
|
|
85
|
+
const stripped = view.replace(SUFFIX, "");
|
|
86
|
+
|
|
87
|
+
return kebab(stripped || view);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string} message
|
|
92
|
+
* @param {string} rel
|
|
93
|
+
* @param {number} line
|
|
94
|
+
* @returns {never}
|
|
95
|
+
*/
|
|
96
|
+
function fail(message, rel, line) {
|
|
97
|
+
throw new Error(`${rel}:${line} ${message}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {string} source
|
|
102
|
+
* @param {RegExp} pattern
|
|
103
|
+
* @returns {number} 1-based line of the first match, 1 when there is none
|
|
104
|
+
*/
|
|
105
|
+
function lineMatching(source, pattern) {
|
|
106
|
+
const match = pattern.exec(source);
|
|
107
|
+
|
|
108
|
+
return match ? source.slice(0, match.index).split("\n").length : 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ---- reading the widget views ---------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether the type is one an HTML attribute can carry, ignoring `| undefined` and `| null`.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} type
|
|
117
|
+
* @returns {'string' | 'number' | 'boolean' | null}
|
|
118
|
+
*/
|
|
119
|
+
function primitiveOf(type) {
|
|
120
|
+
const parts = String(type)
|
|
121
|
+
.split("|")
|
|
122
|
+
.map((part) => part.trim())
|
|
123
|
+
.filter((part) => part !== "undefined" && part !== "null");
|
|
124
|
+
|
|
125
|
+
return parts.length === 1 && PRIMITIVES.has(parts[0]) ? /** @type {any} */ (parts[0]) : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Names a `return` / expression can hand back, when the build can tell. `open` = it cannot tell (a spread, a
|
|
130
|
+
* call, a variable): then no prop is reported missing.
|
|
131
|
+
*
|
|
132
|
+
* @typedef {{ keys: Set<string>, open: boolean }} Shape
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
/** @param {any} node @param {(node: any) => void} visit statements of one function body, not entering nested functions */
|
|
136
|
+
function eachReturn(node, visit) {
|
|
137
|
+
if (!node || typeof node !== "object") return;
|
|
138
|
+
|
|
139
|
+
if (Array.isArray(node)) {
|
|
140
|
+
for (const child of node) eachReturn(child, visit);
|
|
141
|
+
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (node.type === "ReturnStatement") {
|
|
146
|
+
visit(node);
|
|
147
|
+
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (/Function/.test(node.type) || node.type === "ClassDeclaration" || node.type === "ClassExpression") return;
|
|
152
|
+
|
|
153
|
+
for (const [key, value] of Object.entries(node)) {
|
|
154
|
+
if (key === "loc" || key === "start" || key === "end") continue;
|
|
155
|
+
if (value && typeof value === "object") eachReturn(value, visit);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {any} node
|
|
161
|
+
* @param {Map<string, any>} locals top-level functions / variables of the module script
|
|
162
|
+
* @param {Shape} out
|
|
163
|
+
*/
|
|
164
|
+
function shapeOfExpression(node, locals, out) {
|
|
165
|
+
if (!node) {
|
|
166
|
+
out.open = true;
|
|
167
|
+
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
switch (node.type) {
|
|
172
|
+
case "ObjectExpression":
|
|
173
|
+
for (const property of node.properties) {
|
|
174
|
+
if (property.type !== "Property" || property.computed) {
|
|
175
|
+
out.open = true;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
out.keys.add(property.key.type === "Identifier" ? property.key.name : String(property.key.value));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return;
|
|
183
|
+
|
|
184
|
+
case "AwaitExpression":
|
|
185
|
+
shapeOfExpression(node.argument, locals, out);
|
|
186
|
+
|
|
187
|
+
return;
|
|
188
|
+
|
|
189
|
+
case "ConditionalExpression":
|
|
190
|
+
shapeOfExpression(node.consequent, locals, out);
|
|
191
|
+
shapeOfExpression(node.alternate, locals, out);
|
|
192
|
+
|
|
193
|
+
return;
|
|
194
|
+
|
|
195
|
+
case "Identifier": {
|
|
196
|
+
const target = locals.get(node.name);
|
|
197
|
+
|
|
198
|
+
// a local object literal: `const data = { a }; return data;`
|
|
199
|
+
if (target?.type === "ObjectExpression") shapeOfExpression(target, locals, out);
|
|
200
|
+
else out.open = true;
|
|
201
|
+
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
case "CallExpression": {
|
|
206
|
+
const callee = node.callee;
|
|
207
|
+
|
|
208
|
+
// `promise.then((data) => ({ data }))`: the shape is what the callback returns
|
|
209
|
+
if (
|
|
210
|
+
callee?.type === "MemberExpression" &&
|
|
211
|
+
!callee.computed &&
|
|
212
|
+
callee.property?.name === "then" &&
|
|
213
|
+
node.arguments[0] &&
|
|
214
|
+
/Function/.test(node.arguments[0].type)
|
|
215
|
+
) {
|
|
216
|
+
shapeOfFunction(node.arguments[0], locals, out);
|
|
217
|
+
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
out.open = true;
|
|
222
|
+
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
default:
|
|
227
|
+
out.open = true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @param {any} fn
|
|
233
|
+
* @param {Map<string, any>} locals
|
|
234
|
+
* @param {Shape} out
|
|
235
|
+
*/
|
|
236
|
+
function shapeOfFunction(fn, locals, out) {
|
|
237
|
+
if (fn.body?.type !== "BlockStatement") {
|
|
238
|
+
shapeOfExpression(fn.body, locals, out);
|
|
239
|
+
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
let returns = 0;
|
|
244
|
+
|
|
245
|
+
eachReturn(fn.body.body, (statement) => {
|
|
246
|
+
returns += 1;
|
|
247
|
+
shapeOfExpression(statement.argument, locals, out);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// a function that falls off its end returns undefined: nothing to learn, nothing to report
|
|
251
|
+
if (returns === 0) out.open = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Reads the module script of a view: is there a module-level `load`, and which keys does it return?
|
|
256
|
+
*
|
|
257
|
+
* @param {string} source
|
|
258
|
+
* @returns {{ hasLoad: boolean, shape: Shape }}
|
|
259
|
+
*/
|
|
260
|
+
export function readLoad(source) {
|
|
261
|
+
/** @type {Shape} */
|
|
262
|
+
const shape = { keys: new Set(), open: false };
|
|
263
|
+
let ast;
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
ast = parse(source, { modern: true });
|
|
267
|
+
} catch {
|
|
268
|
+
return { hasLoad: false, shape: { keys: shape.keys, open: true } };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const body = ast.module?.content?.body ?? [];
|
|
272
|
+
/** @type {Map<string, any>} */
|
|
273
|
+
const locals = new Map();
|
|
274
|
+
/** @type {any} */
|
|
275
|
+
let loadNode = null;
|
|
276
|
+
|
|
277
|
+
for (const statement of body) {
|
|
278
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
279
|
+
|
|
280
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id) {
|
|
281
|
+
locals.set(declaration.id.name, declaration);
|
|
282
|
+
if (statement.type === "ExportNamedDeclaration" && declaration.id.name === "load") loadNode = declaration;
|
|
283
|
+
} else if (declaration?.type === "VariableDeclaration") {
|
|
284
|
+
for (const declarator of declaration.declarations) {
|
|
285
|
+
if (declarator.id?.type !== "Identifier" || !declarator.init) continue;
|
|
286
|
+
|
|
287
|
+
locals.set(declarator.id.name, declarator.init);
|
|
288
|
+
if (statement.type === "ExportNamedDeclaration" && declarator.id.name === "load") loadNode = declarator.init;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// `export { load }` / `export { fetchGoal as load }`
|
|
294
|
+
if (!loadNode) {
|
|
295
|
+
for (const statement of body) {
|
|
296
|
+
if (statement.type !== "ExportNamedDeclaration" || statement.source) continue;
|
|
297
|
+
|
|
298
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
299
|
+
const exported = specifier.exported?.name ?? specifier.exported?.value;
|
|
300
|
+
|
|
301
|
+
if (exported === "load") loadNode = locals.get(specifier.local.name) ?? { type: "Opaque" };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (!loadNode) return { hasLoad: false, shape };
|
|
307
|
+
|
|
308
|
+
if (/Function/.test(loadNode.type)) shapeOfFunction(loadNode, locals, shape);
|
|
309
|
+
else shape.open = true;
|
|
310
|
+
|
|
311
|
+
return { hasLoad: true, shape };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The widget views of the plugin with everything the checks and `widgets.json` need.
|
|
316
|
+
* Throws the build errors of doc 06 section 3.1 (invalid metadata, missing prop, tag collision).
|
|
317
|
+
*
|
|
318
|
+
* @param {{ scan: Awaited<ReturnType<typeof scanViews>>, namespace: string }} input
|
|
319
|
+
* @returns {WidgetDef[]}
|
|
320
|
+
*/
|
|
321
|
+
export function collectWidgets({ scan, namespace }) {
|
|
322
|
+
/** @type {WidgetDef[]} */
|
|
323
|
+
const widgets = [];
|
|
324
|
+
/** @type {Map<string, WidgetDef>} */
|
|
325
|
+
const byElement = new Map();
|
|
326
|
+
|
|
327
|
+
for (const info of [...scan.views.values()].sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
328
|
+
const declared = info.record.widget;
|
|
329
|
+
|
|
330
|
+
if (declared === null || declared === undefined || declared === false) continue;
|
|
331
|
+
|
|
332
|
+
const line = lineMatching(info.source, /\bwidget\s*:/);
|
|
333
|
+
const options = declared === true ? {} : declared;
|
|
334
|
+
|
|
335
|
+
if (
|
|
336
|
+
typeof options !== "object" ||
|
|
337
|
+
Array.isArray(options) ||
|
|
338
|
+
Object.keys(options).some((key) => key !== "tag" && key !== "session")
|
|
339
|
+
) {
|
|
340
|
+
fail('view.widget must be true or { tag: "goal", session: "optional" } — nothing else is read', info.rel, line);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (options.tag !== undefined && (typeof options.tag !== "string" || !TAG_SHAPE.test(options.tag))) {
|
|
344
|
+
fail(
|
|
345
|
+
`view.widget.tag must be lowercase words joined by "-", like "goal" — the element becomes <pano-${namespace}-goal>`,
|
|
346
|
+
info.rel,
|
|
347
|
+
line,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (options.session !== undefined && !SESSIONS.includes(options.session)) {
|
|
352
|
+
fail(`view.widget.session must be one of ${SESSIONS.join(", ")}`, info.rel, line);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const tag = options.tag ?? defaultTagOf(info.name);
|
|
356
|
+
const element = `pano-${namespace}-${tag}`;
|
|
357
|
+
const existing = byElement.get(element);
|
|
358
|
+
|
|
359
|
+
if (existing) {
|
|
360
|
+
fail(
|
|
361
|
+
`${existing.view} and ${info.name} both make the tag <${element}> — set view.widget to { tag: "..." } in one of them`,
|
|
362
|
+
info.rel,
|
|
363
|
+
line,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const { hasLoad, shape } = readLoad(info.source);
|
|
368
|
+
|
|
369
|
+
/** @type {WidgetAttr[]} */
|
|
370
|
+
const attrs = [];
|
|
371
|
+
|
|
372
|
+
for (const [name, prop] of Object.entries(info.record.props)) {
|
|
373
|
+
const type = primitiveOf(prop.type);
|
|
374
|
+
|
|
375
|
+
if (type) {
|
|
376
|
+
attrs.push({ name, attribute: kebab(name), type });
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (!prop.required || name === "children" || /Snippet/.test(String(prop.type))) continue;
|
|
381
|
+
|
|
382
|
+
if (!hasLoad || (!shape.open && !shape.keys.has(name))) {
|
|
383
|
+
fail(
|
|
384
|
+
`${info.name} needs prop "${name}": return it from load() or remove widget from its view metadata`,
|
|
385
|
+
info.rel,
|
|
386
|
+
line,
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** @type {WidgetDef} */
|
|
392
|
+
const def = {
|
|
393
|
+
view: info.name,
|
|
394
|
+
id: info.id,
|
|
395
|
+
file: info.file,
|
|
396
|
+
rel: info.rel,
|
|
397
|
+
tag,
|
|
398
|
+
element,
|
|
399
|
+
attrs,
|
|
400
|
+
session: options.session ?? null,
|
|
401
|
+
hasLoad,
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
byElement.set(element, def);
|
|
405
|
+
widgets.push(def);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return widgets;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Whether the plugin has a view with `widget`: `panoPlugin()` builds the widgets target only then.
|
|
413
|
+
*
|
|
414
|
+
* @param {{ pluginId: string, namespace?: string, root?: string, viewDirs?: string[] }} options
|
|
415
|
+
* @returns {Promise<boolean>}
|
|
416
|
+
*/
|
|
417
|
+
export async function hasWidgetViews(options) {
|
|
418
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
419
|
+
const viewDirs = options.viewDirs ?? (await loadConfig(root)).viewDirs;
|
|
420
|
+
const scan = await scanViews({ dirs: viewDirs, namespace: options.namespace, pluginId: options.pluginId, root });
|
|
421
|
+
|
|
422
|
+
return [...scan.views.values()].some((info) => info.record.widget !== null && info.record.widget !== false);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ---- the part step ---------------------------------------------------------------------------
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Class tokens an expression names literally.
|
|
429
|
+
*
|
|
430
|
+
* @param {any} node
|
|
431
|
+
* @param {string[]} out
|
|
432
|
+
*/
|
|
433
|
+
function literalTokens(node, out) {
|
|
434
|
+
if (!node || typeof node !== "object") return;
|
|
435
|
+
|
|
436
|
+
if (Array.isArray(node)) {
|
|
437
|
+
for (const child of node) literalTokens(child, out);
|
|
438
|
+
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
443
|
+
out.push(...node.value.split(/\s+/).filter(Boolean));
|
|
444
|
+
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (node.type === "TemplateElement") {
|
|
449
|
+
out.push(...String(node.value?.cooked ?? node.value?.raw ?? "").split(/\s+/).filter(Boolean));
|
|
450
|
+
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (node.type === "Property" && !node.computed) {
|
|
455
|
+
if (node.key?.type === "Identifier") out.push(node.key.name);
|
|
456
|
+
else literalTokens(node.key, out);
|
|
457
|
+
|
|
458
|
+
literalTokens(node.value, out);
|
|
459
|
+
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
for (const [key, value] of Object.entries(node)) {
|
|
464
|
+
if (key === "loc" || key === "start" || key === "end" || key === "type") continue;
|
|
465
|
+
literalTokens(value, out);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* @param {any} root
|
|
471
|
+
* @returns {any[]} every RegularElement of the template, in source order
|
|
472
|
+
*/
|
|
473
|
+
function allElements(root) {
|
|
474
|
+
/** @type {any[]} */
|
|
475
|
+
const found = [];
|
|
476
|
+
const seen = new WeakSet();
|
|
477
|
+
|
|
478
|
+
/** @param {any} node */
|
|
479
|
+
const visit = (node) => {
|
|
480
|
+
if (!node || typeof node !== "object" || seen.has(node)) return;
|
|
481
|
+
|
|
482
|
+
seen.add(node);
|
|
483
|
+
|
|
484
|
+
if (Array.isArray(node)) {
|
|
485
|
+
for (const child of node) visit(child);
|
|
486
|
+
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (node.type === "RegularElement") found.push(node);
|
|
491
|
+
|
|
492
|
+
for (const [key, value] of Object.entries(node)) {
|
|
493
|
+
if (key === "loc" || key === "name_loc" || key === "metadata") continue;
|
|
494
|
+
visit(value);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
visit(root.fragment);
|
|
499
|
+
|
|
500
|
+
return found.sort((a, b) => a.start - b.start);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Copies the semantic classes of every element to its `part` attribute (doc 06 section 3.4):
|
|
505
|
+
* `class="market-goal__bar"` becomes `class="market-goal__bar" part="market-goal__bar"`. The root class
|
|
506
|
+
* of the view is added first (the build does it anyway, a step later), so the root element is a part too.
|
|
507
|
+
* An element that has a `part` attribute is left alone. Idempotent.
|
|
508
|
+
*
|
|
509
|
+
* @param {string} source view source
|
|
510
|
+
* @param {{ ns: string, view: string }} ctx
|
|
511
|
+
* @returns {{ code: string, parts: number }} `parts` = elements that got an attribute
|
|
512
|
+
*/
|
|
513
|
+
export function addPartAttributes(source, ctx) {
|
|
514
|
+
let code = source;
|
|
515
|
+
|
|
516
|
+
try {
|
|
517
|
+
code = addRootClass(source, ctx).code;
|
|
518
|
+
} catch (error) {
|
|
519
|
+
if (error instanceof SemanticClassCollisionError) throw error;
|
|
520
|
+
|
|
521
|
+
return { code: source, parts: 0 };
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
let ast;
|
|
525
|
+
|
|
526
|
+
try {
|
|
527
|
+
ast = parse(code, { modern: true });
|
|
528
|
+
} catch {
|
|
529
|
+
return { code: source, parts: 0 }; // the compiler reports the syntax error with better positions
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const ms = new MagicString(code);
|
|
533
|
+
let parts = 0;
|
|
534
|
+
|
|
535
|
+
for (const element of allElements(ast)) {
|
|
536
|
+
const attributes = element.attributes ?? [];
|
|
537
|
+
|
|
538
|
+
if (attributes.some((attribute) => attribute.type === "Attribute" && attribute.name === "part")) continue;
|
|
539
|
+
|
|
540
|
+
/** @type {string[]} */
|
|
541
|
+
const tokens = [];
|
|
542
|
+
|
|
543
|
+
for (const attribute of attributes) {
|
|
544
|
+
if (attribute.type === "ClassDirective") tokens.push(attribute.name);
|
|
545
|
+
|
|
546
|
+
if (attribute.type !== "Attribute" || attribute.name !== "class" || attribute.value === true) continue;
|
|
547
|
+
|
|
548
|
+
for (const piece of Array.isArray(attribute.value) ? attribute.value : [attribute.value]) {
|
|
549
|
+
if (piece.type === "Text") tokens.push(...piece.data.split(/\s+/).filter(Boolean));
|
|
550
|
+
else if (piece.type === "ExpressionTag") literalTokens(piece.expression, tokens);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const names = [...new Set(tokens.filter((token) => token.startsWith(`${ctx.ns}-`)))];
|
|
555
|
+
|
|
556
|
+
if (names.length === 0) continue;
|
|
557
|
+
|
|
558
|
+
ms.appendLeft(element.start + 1 + element.name.length, ` part="${names.join(" ")}"`);
|
|
559
|
+
parts += 1;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
return { code: parts > 0 ? ms.toString() : code, parts };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ---- session inference -----------------------------------------------------------------------
|
|
566
|
+
|
|
567
|
+
/** @param {string} text */
|
|
568
|
+
const withoutComments = (text) => text.replace(/\/\*[\s\S]*?\*\/|(^|[^:])\/\/[^\n]*/g, "$1");
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Controllers (short names) that call `host.session` / `host.onSession`, directly or through a sibling
|
|
572
|
+
* they `use('...')`.
|
|
573
|
+
*
|
|
574
|
+
* @param {import('./rules.js').KitProject} project
|
|
575
|
+
* @returns {Set<string>}
|
|
576
|
+
*/
|
|
577
|
+
export function sessionControllers(project) {
|
|
578
|
+
/** @type {Map<string, { session: boolean, uses: string[] }>} */
|
|
579
|
+
const found = new Map();
|
|
580
|
+
|
|
581
|
+
for (const file of controllerFiles(project)) {
|
|
582
|
+
const source = withoutComments(fs.readFileSync(file, "utf8"));
|
|
583
|
+
const name = /defineController\s*\(\s*\{[\s\S]*?\bname\s*:\s*(["'`])([A-Za-z0-9]+)\1/.exec(source)?.[2] ?? path.basename(file, ".js");
|
|
584
|
+
const session =
|
|
585
|
+
/\bhost\s*\??\.\s*(?:session|onSession)\b/.test(source) ||
|
|
586
|
+
/\{[^{}]*\b(?:session|onSession)\b[^{}]*\}\s*=\s*(?:[\w$]+\.)?host\b/.test(source) ||
|
|
587
|
+
/\bhost\s*:\s*\{[^{}]*\b(?:session|onSession)\b[^{}]*\}/.test(source);
|
|
588
|
+
const uses = [...source.matchAll(/\buse\s*\(\s*(["'`])([A-Za-z0-9]+)\1/g)].map((match) => match[2]);
|
|
589
|
+
|
|
590
|
+
found.set(name, { session, uses });
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const result = new Set([...found].filter(([, value]) => value.session).map(([name]) => name));
|
|
594
|
+
|
|
595
|
+
for (let changed = true; changed; ) {
|
|
596
|
+
changed = false;
|
|
597
|
+
|
|
598
|
+
for (const [name, value] of found) {
|
|
599
|
+
if (!result.has(name) && value.uses.some((use) => result.has(use))) {
|
|
600
|
+
result.add(name);
|
|
601
|
+
changed = true;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return result;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// ---- svelte pin -------------------------------------------------------------------------------
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* @param {string} from
|
|
613
|
+
* @param {string} name
|
|
614
|
+
* @returns {string | null}
|
|
615
|
+
*/
|
|
616
|
+
function packageDir(from, name) {
|
|
617
|
+
let dir = path.resolve(from);
|
|
618
|
+
|
|
619
|
+
for (;;) {
|
|
620
|
+
const candidate = path.join(dir, "node_modules", name);
|
|
621
|
+
|
|
622
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) return fs.realpathSync(candidate);
|
|
623
|
+
|
|
624
|
+
const parent = path.dirname(dir);
|
|
625
|
+
|
|
626
|
+
if (parent === dir) return null;
|
|
627
|
+
|
|
628
|
+
dir = parent;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/** @param {string} file */
|
|
633
|
+
function versionIn(file) {
|
|
634
|
+
try {
|
|
635
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
636
|
+
} catch {
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* The svelte the widgets are compiled for (the installed one, as `checkSvelteVersion` reads it) and the
|
|
643
|
+
* version the sdk pins.
|
|
644
|
+
*
|
|
645
|
+
* @param {string} root
|
|
646
|
+
* @param {string | null} sdkDir
|
|
647
|
+
* @returns {{ built: string | null, pinned: string | null }}
|
|
648
|
+
*/
|
|
649
|
+
export function svelteVersions(root, sdkDir) {
|
|
650
|
+
const own = packageDir(root, "svelte");
|
|
651
|
+
const fromSdk = sdkDir ? path.join(sdkDir, "node_modules/svelte") : null;
|
|
652
|
+
let built = null;
|
|
653
|
+
|
|
654
|
+
for (const dir of [own, fromSdk]) {
|
|
655
|
+
if (dir && !built) built = versionIn(path.join(dir, "package.json"))?.version ?? null;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (!built) {
|
|
659
|
+
try {
|
|
660
|
+
built = versionIn(createRequire(import.meta.url).resolve("svelte/package.json"))?.version ?? null;
|
|
661
|
+
} catch {
|
|
662
|
+
built = null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const sdk = sdkDir ?? packageDir(root, "@panomc/sdk");
|
|
667
|
+
const pin = sdk ? versionIn(path.join(sdk, "package.json"))?.dependencies?.svelte : null;
|
|
668
|
+
|
|
669
|
+
return { built, pinned: typeof pin === "string" ? pin : null };
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// ---- controllers a widget uses ------------------------------------------------------------------
|
|
673
|
+
|
|
674
|
+
const IMPORT_PATTERN = /(?:\bfrom\s*|\bimport\s*\(\s*|^\s*import\s+)(["'])(\.{1,2}\/[^"'\n]+)\1/gm;
|
|
675
|
+
const RESOLVE_SUFFIXES = ["", ".js", ".mjs", ".svelte", "/index.js"];
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Own controllers (short names) a view names with a string literal: `.use('cart')`, `.require('cart')`,
|
|
679
|
+
* `.load('cart')`, `useController('<ns>/cart')`, in the view and in the files it imports by relative path.
|
|
680
|
+
* A source scan, not a parse: it only decides `session`, so a false hit costs `optional` instead of `none`.
|
|
681
|
+
*
|
|
682
|
+
* @param {string} file absolute path of the view
|
|
683
|
+
* @param {string} namespace
|
|
684
|
+
* @param {string} root plugin root; files outside it are not followed
|
|
685
|
+
* @returns {Set<string>}
|
|
686
|
+
*/
|
|
687
|
+
export function controllersUsedBy(file, namespace, root) {
|
|
688
|
+
/** @type {Set<string>} */
|
|
689
|
+
const names = new Set();
|
|
690
|
+
const seen = new Set();
|
|
691
|
+
const queue = [file];
|
|
692
|
+
|
|
693
|
+
while (queue.length) {
|
|
694
|
+
const current = /** @type {string} */ (queue.pop());
|
|
695
|
+
|
|
696
|
+
if (seen.has(current)) continue;
|
|
697
|
+
|
|
698
|
+
seen.add(current);
|
|
699
|
+
|
|
700
|
+
let source;
|
|
701
|
+
|
|
702
|
+
try {
|
|
703
|
+
source = withoutComments(fs.readFileSync(current, "utf8"));
|
|
704
|
+
} catch {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
for (const match of source.matchAll(/\.\s*(?:use|require|load)\s*\(\s*(["'`])([A-Za-z0-9]+)\1/g)) names.add(match[2]);
|
|
709
|
+
|
|
710
|
+
for (const match of source.matchAll(/\buseController\s*\(\s*(["'`])([A-Za-z0-9_-]+)\/([A-Za-z0-9]+)\1/g)) {
|
|
711
|
+
if (match[2] === namespace) names.add(match[3]);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
for (const match of source.matchAll(IMPORT_PATTERN)) {
|
|
715
|
+
const base = path.resolve(path.dirname(current), match[2]);
|
|
716
|
+
|
|
717
|
+
for (const suffix of RESOLVE_SUFFIXES) {
|
|
718
|
+
const candidate = base + suffix;
|
|
719
|
+
|
|
720
|
+
if (candidate.startsWith(root + path.sep) && fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
|
|
721
|
+
queue.push(candidate);
|
|
722
|
+
break;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return names;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// ---- the rollup plugin ------------------------------------------------------------------------
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* @param {string} specifier
|
|
735
|
+
* @returns {boolean}
|
|
736
|
+
*/
|
|
737
|
+
function isHostSpecifier(specifier) {
|
|
738
|
+
return (
|
|
739
|
+
specifier === "svelte" ||
|
|
740
|
+
specifier.startsWith("svelte/") ||
|
|
741
|
+
specifier === "svelte-i18n" ||
|
|
742
|
+
specifier === "@panomc/sdk" ||
|
|
743
|
+
specifier.startsWith("@panomc/sdk/")
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Sets `badges.widgets` in the package index on disk and in the shared build context (the index is written
|
|
749
|
+
* by the client build, which may run before or after this one).
|
|
750
|
+
*
|
|
751
|
+
* @param {string} root
|
|
752
|
+
* @param {string} outDir
|
|
753
|
+
* @param {boolean} value
|
|
754
|
+
*/
|
|
755
|
+
function setBadge(root, outDir, value) {
|
|
756
|
+
getBuildContext(root).badges.widgets = value;
|
|
757
|
+
|
|
758
|
+
const file = path.join(outDir, "pano-plugin.json");
|
|
759
|
+
|
|
760
|
+
if (!fs.existsSync(file)) return;
|
|
761
|
+
|
|
762
|
+
try {
|
|
763
|
+
const index = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
764
|
+
|
|
765
|
+
if (!index.badges || index.badges.widgets === value) return;
|
|
766
|
+
|
|
767
|
+
index.badges.widgets = value;
|
|
768
|
+
writeIfChanged(file, JSON.stringify(index, null, 2) + "\n");
|
|
769
|
+
} catch {
|
|
770
|
+
// an unreadable index is the meta plugin's to report
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* The widgets target of `panoPlugin()`.
|
|
776
|
+
*
|
|
777
|
+
* @param {PanoWidgetsOptions} options
|
|
778
|
+
* @returns {import('rollup').Plugin}
|
|
779
|
+
*/
|
|
780
|
+
export function panoWidgets(options) {
|
|
781
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
782
|
+
const outDir = path.resolve(root, options.outDir ?? DEFAULT_OUT_DIR);
|
|
783
|
+
const widgetsDir = path.join(outDir, "widgets");
|
|
784
|
+
|
|
785
|
+
/** @type {(WidgetDef & { resolvedSession: 'none' | 'optional' | 'required' })[]} */
|
|
786
|
+
let widgets = [];
|
|
787
|
+
/** @type {string[]} */
|
|
788
|
+
let viewDirs = [];
|
|
789
|
+
let namespace = options.namespace;
|
|
790
|
+
/** @type {string | null} */
|
|
791
|
+
let sdkDir = null;
|
|
792
|
+
let scanned = false;
|
|
793
|
+
/** @type {{ source: string, importer: string | undefined }[]} */
|
|
794
|
+
let unsupported = [];
|
|
795
|
+
|
|
796
|
+
/** @param {string} id */
|
|
797
|
+
const isViewFile = (id) => {
|
|
798
|
+
if (!id.endsWith(".svelte")) return false;
|
|
799
|
+
|
|
800
|
+
const file = path.resolve(id.replace(/\?.*$/, ""));
|
|
801
|
+
|
|
802
|
+
return viewDirs.some((dir) => file.startsWith(dir + path.sep));
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Widget views whose graph contains `importer`, the module that holds the import being resolved.
|
|
807
|
+
*
|
|
808
|
+
* @param {import('rollup').PluginContext} context
|
|
809
|
+
* @param {string | undefined} importer
|
|
810
|
+
* @returns {string[]}
|
|
811
|
+
*/
|
|
812
|
+
const widgetsReaching = (context, importer) => {
|
|
813
|
+
const reached = new Set();
|
|
814
|
+
const seen = new Set();
|
|
815
|
+
const queue = importer ? [importer] : [];
|
|
816
|
+
|
|
817
|
+
while (queue.length) {
|
|
818
|
+
const id = /** @type {string} */ (queue.pop());
|
|
819
|
+
|
|
820
|
+
if (seen.has(id)) continue;
|
|
821
|
+
|
|
822
|
+
seen.add(id);
|
|
823
|
+
|
|
824
|
+
if (id.startsWith(WIDGET_ENTRY_PREFIX)) {
|
|
825
|
+
reached.add(id.slice(WIDGET_ENTRY_PREFIX.length));
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
queue.push(...(context.getModuleInfo(id)?.importers ?? []));
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
return [...(reached.size ? reached : widgets.map((widget) => widget.view))].sort();
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
return {
|
|
836
|
+
name: "pano-widgets",
|
|
837
|
+
|
|
838
|
+
async options(inputOptions) {
|
|
839
|
+
const config = await loadConfig(root);
|
|
840
|
+
|
|
841
|
+
namespace = options.namespace ?? config.namespace;
|
|
842
|
+
sdkDir = config.sdkDir ?? packageDir(root, "@panomc/sdk");
|
|
843
|
+
viewDirs = (options.viewDirs ?? config.viewDirs).map((dir) => path.resolve(root, dir));
|
|
844
|
+
|
|
845
|
+
const scan = await scanViews({
|
|
846
|
+
dirs: viewDirs,
|
|
847
|
+
namespace,
|
|
848
|
+
pluginId: options.pluginId ?? config.pluginId,
|
|
849
|
+
root,
|
|
850
|
+
});
|
|
851
|
+
const found = collectWidgets({ scan, namespace });
|
|
852
|
+
|
|
853
|
+
scanned = true;
|
|
854
|
+
|
|
855
|
+
if (found.length === 0) {
|
|
856
|
+
widgets = [];
|
|
857
|
+
|
|
858
|
+
return null; // nothing to build: the empty input of the config stays and nothing is written
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
const project = await resolveProject({ root, viewDirs: options.viewDirs ?? config.viewDirs, namespace });
|
|
862
|
+
const withSession = sessionControllers(project);
|
|
863
|
+
|
|
864
|
+
widgets = found.map((widget) => ({
|
|
865
|
+
...widget,
|
|
866
|
+
resolvedSession:
|
|
867
|
+
widget.session ??
|
|
868
|
+
([...controllersUsedBy(widget.file, namespace, root)].some((name) => withSession.has(name)) ? "optional" : "none"),
|
|
869
|
+
}));
|
|
870
|
+
|
|
871
|
+
return {
|
|
872
|
+
...inputOptions,
|
|
873
|
+
input: Object.fromEntries(widgets.map((widget) => [widget.view, WIDGET_ENTRY_PREFIX + widget.view])),
|
|
874
|
+
preserveEntrySignatures: "strict",
|
|
875
|
+
};
|
|
876
|
+
},
|
|
877
|
+
|
|
878
|
+
buildStart() {
|
|
879
|
+
unsupported = [];
|
|
880
|
+
|
|
881
|
+
if (widgets.length === 0) {
|
|
882
|
+
fs.rmSync(widgetsDir, { recursive: true, force: true });
|
|
883
|
+
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const { built, pinned } = svelteVersions(root, sdkDir);
|
|
888
|
+
|
|
889
|
+
if (built && pinned && /^\d/.test(pinned) && built !== pinned) {
|
|
890
|
+
this.warn(`widgets built for svelte ${built}, this SDK pins ${pinned}`);
|
|
891
|
+
}
|
|
892
|
+
},
|
|
893
|
+
|
|
894
|
+
outputOptions(output) {
|
|
895
|
+
if (widgets.length === 0) return null;
|
|
896
|
+
|
|
897
|
+
return {
|
|
898
|
+
...output,
|
|
899
|
+
format: "es",
|
|
900
|
+
dir: widgetsDir,
|
|
901
|
+
entryFileNames: "[name]-[hash].js",
|
|
902
|
+
chunkFileNames: "chunk-[hash].js",
|
|
903
|
+
// doc 06 section 3.3: relative runtime URLs, so a proxy prefix does not matter
|
|
904
|
+
paths: (/** @type {string} */ id) => (RUNTIME_FILES[id] ? widgetImportPath(id) : id),
|
|
905
|
+
};
|
|
906
|
+
},
|
|
907
|
+
|
|
908
|
+
resolveId(source, importer) {
|
|
909
|
+
if (source.startsWith(WIDGET_ENTRY_PREFIX)) {
|
|
910
|
+
return widgets.some((widget) => WIDGET_ENTRY_PREFIX + widget.view === source) ? source : null;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (widgets.length === 0 || !isHostSpecifier(source)) return null;
|
|
914
|
+
|
|
915
|
+
// every host specifier is the runtime's module: external here, a relative URL in the output (outputOptions)
|
|
916
|
+
if (RUNTIME_FILES[source]) return { id: source, external: true };
|
|
917
|
+
|
|
918
|
+
// The module graph is not linked yet, so the widget views that reach this file are named in buildEnd.
|
|
919
|
+
unsupported.push({ source, importer });
|
|
920
|
+
|
|
921
|
+
return { id: source, external: true };
|
|
922
|
+
},
|
|
923
|
+
|
|
924
|
+
buildEnd(error) {
|
|
925
|
+
if (error || unsupported.length === 0) return;
|
|
926
|
+
|
|
927
|
+
const messages = unsupported.map(({ source, importer }) => {
|
|
928
|
+
const file = importer ? path.relative(root, importer.replace(/\?.*$/, "")).replace(/\\/g, "/") : "(entry)";
|
|
929
|
+
|
|
930
|
+
return `${file} imports ${source}, which has no widget implementation. Remove ${widgetsReaching(this, importer).join(", ")} from widgets or avoid the import.`;
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
this.error(messages.length === 1 ? messages[0] : `${messages.length} imports have no widget implementation:\n ${messages.join("\n ")}`);
|
|
934
|
+
},
|
|
935
|
+
|
|
936
|
+
load(id) {
|
|
937
|
+
if (!id.startsWith(WIDGET_ENTRY_PREFIX)) return null;
|
|
938
|
+
|
|
939
|
+
const widget = widgets.find((candidate) => WIDGET_ENTRY_PREFIX + candidate.view === id);
|
|
940
|
+
|
|
941
|
+
if (!widget) return null;
|
|
942
|
+
|
|
943
|
+
const file = JSON.stringify(widget.file);
|
|
944
|
+
|
|
945
|
+
return [
|
|
946
|
+
`import Component from ${file};`,
|
|
947
|
+
"export { Component as component };",
|
|
948
|
+
widget.hasLoad ? `export { load } from ${file};` : "export const load = undefined;",
|
|
949
|
+
`export const attrs = ${JSON.stringify(widget.attrs.map(({ name, type }) => ({ name, type })))};`,
|
|
950
|
+
`export const session = ${JSON.stringify(widget.resolvedSession)};`,
|
|
951
|
+
"",
|
|
952
|
+
].join("\n");
|
|
953
|
+
},
|
|
954
|
+
|
|
955
|
+
// the `part` step, before the svelte plugin compiles the file
|
|
956
|
+
transform(code, id) {
|
|
957
|
+
if (widgets.length === 0 || !isViewFile(id)) return null;
|
|
958
|
+
|
|
959
|
+
const result = addPartAttributes(code, {
|
|
960
|
+
ns: /** @type {string} */ (namespace),
|
|
961
|
+
view: path.basename(id.replace(/\?.*$/, ""), ".svelte"),
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
return result.code === code ? null : { code: result.code, map: null };
|
|
965
|
+
},
|
|
966
|
+
|
|
967
|
+
generateBundle(_output, bundle) {
|
|
968
|
+
if (widgets.length === 0) {
|
|
969
|
+
for (const key of Object.keys(bundle)) delete bundle[key];
|
|
970
|
+
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
const modules = new Map(
|
|
975
|
+
Object.values(bundle)
|
|
976
|
+
.filter((chunk) => chunk.type === "chunk" && chunk.isEntry && chunk.facadeModuleId)
|
|
977
|
+
.map((chunk) => [/** @type {string} */ (/** @type {any} */ (chunk).facadeModuleId), /** @type {any} */ (chunk).fileName]),
|
|
978
|
+
);
|
|
979
|
+
const { built } = svelteVersions(root, sdkDir);
|
|
980
|
+
|
|
981
|
+
const index = {
|
|
982
|
+
format: WIDGETS_JSON_FORMAT,
|
|
983
|
+
svelte: built,
|
|
984
|
+
widgets: widgets.map((widget) => ({
|
|
985
|
+
view: widget.view,
|
|
986
|
+
tag: widget.tag,
|
|
987
|
+
module: modules.get(WIDGET_ENTRY_PREFIX + widget.view),
|
|
988
|
+
attrs: widget.attrs,
|
|
989
|
+
session: widget.resolvedSession,
|
|
990
|
+
})),
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
this.emitFile({ type: "asset", fileName: "widgets.json", source: JSON.stringify(index, null, 2) + "\n" });
|
|
994
|
+
},
|
|
995
|
+
|
|
996
|
+
closeBundle() {
|
|
997
|
+
if (scanned) setBadge(root, outDir, widgets.length > 0);
|
|
998
|
+
},
|
|
999
|
+
};
|
|
1000
|
+
}
|