@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.
Files changed (89) hide show
  1. package/assets/pano-bootstrap.css +17903 -0
  2. package/bin/__tests__/new/e2e.test.js +111 -0
  3. package/bin/__tests__/new/helpers.js +76 -0
  4. package/bin/__tests__/new/new.test.js +472 -0
  5. package/bin/new.js +530 -0
  6. package/bin/pano-plugin.js +162 -0
  7. package/bin/templates/plugin/README.md +30 -0
  8. package/bin/templates/plugin/_gitignore +15 -0
  9. package/bin/templates/plugin/build.gradle.kts +389 -0
  10. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
  11. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
  12. package/bin/templates/plugin/gradle.properties +55 -0
  13. package/bin/templates/plugin/gradlew +249 -0
  14. package/bin/templates/plugin/gradlew.bat +92 -0
  15. package/bin/templates/plugin/package.json.tpl +14 -0
  16. package/bin/templates/plugin/rollup.config.js +2 -0
  17. package/bin/templates/plugin/settings.gradle.kts +5 -0
  18. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
  19. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
  20. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
  21. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
  22. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
  23. package/bin/templates/plugin/src/main/resources/config.conf +1 -0
  24. package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
  25. package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
  26. package/bin/templates/plugin/src/main/resources/logo.png +0 -0
  27. package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
  28. package/bin/ui.js +96 -0
  29. package/package.json +48 -4
  30. package/src/config.js +191 -0
  31. package/src/controller/__tests__/controller.test.js +647 -0
  32. package/src/controller/index.js +604 -0
  33. package/src/dev.js +208 -0
  34. package/src/index.js +22 -0
  35. package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
  36. package/src/rollup/__tests__/controllers/fixture.js +143 -0
  37. package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
  38. package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
  39. package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
  40. package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
  41. package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
  42. package/src/rollup/__tests__/preset/preset.test.js +418 -0
  43. package/src/rollup/__tests__/rules/_build.js +39 -0
  44. package/src/rollup/__tests__/rules/_fixture.js +83 -0
  45. package/src/rollup/__tests__/rules/helpers.test.js +148 -0
  46. package/src/rollup/__tests__/rules/rules.test.js +259 -0
  47. package/src/rollup/__tests__/rules/stamp.test.js +113 -0
  48. package/src/rollup/__tests__/samples/samples.test.js +481 -0
  49. package/src/rollup/__tests__/seams/config.test.js +78 -0
  50. package/src/rollup/__tests__/seams/lock.test.js +124 -0
  51. package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
  52. package/src/rollup/__tests__/seams/stubs.test.js +52 -0
  53. package/src/rollup/__tests__/views/helpers.js +82 -0
  54. package/src/rollup/__tests__/views/views.test.js +522 -0
  55. package/src/rollup/__tests__/widgets/helpers.js +117 -0
  56. package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
  57. package/src/rollup/cli-build.js +90 -0
  58. package/src/rollup/cli-check.js +280 -0
  59. package/src/rollup/cli-classes.js +75 -0
  60. package/src/rollup/client-gen.js +67 -0
  61. package/src/rollup/controllers.js +434 -0
  62. package/src/rollup/entry.js +250 -0
  63. package/src/rollup/helpers.js +193 -0
  64. package/src/rollup/index.js +601 -0
  65. package/src/rollup/integrate.js +205 -0
  66. package/src/rollup/lock.js +230 -0
  67. package/src/rollup/meta.js +479 -0
  68. package/src/rollup/rules.js +950 -0
  69. package/src/rollup/samples.js +667 -0
  70. package/src/rollup/stamp.js +410 -0
  71. package/src/rollup/types.js +248 -0
  72. package/src/rollup/views.js +1205 -0
  73. package/src/rollup/widgets.js +1000 -0
  74. package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
  75. package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
  76. package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
  77. package/src/styles/__tests__/fallback/plugins.test.js +301 -0
  78. package/src/styles/__tests__/lint/check-styles.test.js +423 -0
  79. package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
  80. package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
  81. package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
  82. package/src/styles/bootstrap-classes.js +155 -0
  83. package/src/styles/check-styles.js +626 -0
  84. package/src/styles/fallback-css.js +1182 -0
  85. package/src/styles/plugin-css.js +237 -0
  86. package/src/styles/scan.js +206 -0
  87. package/src/styles/semantic-classes.js +436 -0
  88. package/src/styles/tokens.map.js +53 -0
  89. package/README.md +0 -3
@@ -0,0 +1,601 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
4
+ import svelte from 'rollup-plugin-svelte';
5
+ import resolve from '@rollup/plugin-node-resolve';
6
+ import del from 'rollup-plugin-delete';
7
+ import terser from '@rollup/plugin-terser';
8
+ import { loadConfig } from '../config.js';
9
+ import {
10
+ DEFAULT_CONTROLLERS_DIR,
11
+ ENTRY_ID,
12
+ CONTROLLERS_ID,
13
+ WRAPPER_RESOLVED_ID,
14
+ panoEntry,
15
+ panoControllersModule,
16
+ listControllerFiles,
17
+ } from './entry.js';
18
+ import { panoViews } from './views.js';
19
+ import { panoRules } from './rules.js';
20
+ import { panoHelpers } from './helpers.js';
21
+ import { panoStamp } from './stamp.js';
22
+ import { panoControllers } from './controllers.js';
23
+ import { panoMeta } from './meta.js';
24
+ import { panoTypes } from './types.js';
25
+ import { panoSamples } from './samples.js';
26
+ import { panoWidgets, hasWidgetViews } from './widgets.js';
27
+ import { panoClientGen } from './client-gen.js';
28
+ import { panoSemanticClasses } from '../styles/semantic-classes.js';
29
+ import { createClassPolicy, panoBuildContext, viewClassInfo } from './integrate.js';
30
+ import { panoFallbackCss } from '../styles/fallback-css.js';
31
+ import { panoPluginCss } from '../styles/plugin-css.js';
32
+
33
+ /** Where the build writes the plugin UI package (relative to the plugin root). */
34
+ export const DEFAULT_OUT_DIR = 'src/main/resources/plugin-ui';
35
+
36
+ const SIDES = ['server', 'client', 'controllers', 'widgets'];
37
+ const PLUGIN_API_SPECIFIER = '@panomc/sdk/plugin-api';
38
+
39
+ /**
40
+ * `export default panoPlugin();` in a plugin's rollup.config.js builds the server and client
41
+ * bundles, the standalone controllers and (on request) the widgets, composing the kit
42
+ * sub-plugins of doc 02's file map in this order: config, entry, views, rules/helpers/
43
+ * stamp, controllers/meta/types, lock, samples/widgets/client-gen, styles.
44
+ *
45
+ * Returns a Promise (the plugin's `pano.plugin.js` is loaded with `import()`); rollup accepts a
46
+ * promise as the default export of a config file.
47
+ *
48
+ * Defaults (today's behaviour of pano-boilerplate-plugin/rollup.config.js):
49
+ * - output `src/main/resources/plugin-ui/{server,client}` (`server.mjs`, `client.mjs`, `manifest.json`),
50
+ * the output folder is emptied once per build (by the client build, which runs first);
51
+ * - client: `svelte`, `svelte/*`, `svelte-i18n`, `@panomc/sdk`, `@panomc/sdk/*` stay external (the host
52
+ * import map provides them), unless `BUNDLE_SDK=true`; server: nothing external;
53
+ * - `PANO_SDK_DIR` (local delivery) aliases `@panomc/sdk*` to that folder and its pinned svelte version
54
+ * is checked against the installed one (a mismatch throws);
55
+ * - the plugin id is read from `gradle.properties`; `src/main.js` missing = `export default class extends PanoPlugin {}`;
56
+ * - `DEV=true` builds unminified with Svelte dev mode.
57
+ *
58
+ * Every kit sub-plugin also receives `{ side, outDir, root }` (all absolute) besides its documented
59
+ * options; `side` is `'server'` or `'client'` for the two UI bundles, so a plugin that writes package files
60
+ * once can do it on `client` only. The sub-plugins that write package files (rules, helpers, meta, types,
61
+ * samples, client-gen, fallback/plugin css) only run in the client build; entry, views, stamp and the
62
+ * semantic classes run in both.
63
+ *
64
+ * @param {{
65
+ * side?: 'server' | 'client' | 'controllers' | 'widgets',
66
+ * plugins?: import('rollup').Plugin[],
67
+ * bundle?: (id: string, importer?: string) => boolean,
68
+ * root?: string,
69
+ * outDir?: string,
70
+ * minify?: boolean,
71
+ * dev?: boolean,
72
+ * }} [options]
73
+ * `side`: build only that target (default: client, server, controllers when the plugin has any, widgets when a view has `widget`);
74
+ * `plugins`: extra rollup plugins for the server and client builds, placed before the kit's own (so they
75
+ * can redirect an import first); `bundle`: return true for a specifier that is normally external but must
76
+ * be bundled in the client build; `root`: plugin root (default `process.cwd()`); `outDir`: package folder
77
+ * (default `src/main/resources/plugin-ui`); `minify` / `dev`: override `DEV`.
78
+ * @returns {Promise<import('rollup').RollupOptions[]>}
79
+ */
80
+ export async function panoPlugin(options = {}) {
81
+ if (options.side !== undefined && !SIDES.includes(options.side)) {
82
+ throw new Error(
83
+ `[pano-plugin] panoPlugin({ side }) must be one of ${SIDES.join(', ')}, got "${options.side}"`,
84
+ );
85
+ }
86
+
87
+ const root = path.resolve(options.root ?? process.cwd());
88
+ const config = await loadConfig(root);
89
+ const outDir = path.resolve(root, options.outDir ?? DEFAULT_OUT_DIR);
90
+ const dev = options.dev ?? (!!process.env.DEV && process.env.DEV !== 'false');
91
+ const minify = options.minify ?? !dev;
92
+ const bundleSdk = process.env.BUNDLE_SDK === 'true';
93
+ const sdkDir = config.sdkDir ?? findPackageDir(root, '@panomc/sdk');
94
+
95
+ checkSvelteVersion({ root, sdkDir: config.sdkDir ?? sdkDir });
96
+
97
+ const viewDirs = config.viewDirs.map((dir) => path.resolve(root, dir));
98
+ const controllersDir = path.resolve(root, DEFAULT_CONTROLLERS_DIR);
99
+ const mainFile = path.resolve(root, 'src/main.js');
100
+ const common = { root, outDir };
101
+ const ids = { pluginId: config.pluginId, namespace: config.namespace };
102
+ const hasControllers = listControllerFiles(controllersDir).length > 0;
103
+ const pluginApiFactory = await loadPluginApiFactory(sdkDir);
104
+
105
+ // What the sub-plugins found about the views (rules: import mode, helpers: closure, stamp: controller
106
+ // versions) is handed over in one object per build and copied into the package index by panoBuildContext.
107
+ /** @type {Record<'server' | 'client', Record<string, any>>} */
108
+ const contexts = { server: {}, client: {} };
109
+
110
+ // The class rules of the build: the readable copy and the compiled view carry the root class
111
+ // (doc 03 section 1.2); the lock keeps the classes of every view (section 1.3).
112
+ /** @type {ReturnType<typeof panoViews> | null} */
113
+ let viewsPlugin = null;
114
+ const policy = createClassPolicy({
115
+ root,
116
+ ns: config.namespace,
117
+ scan: () => viewsPlugin?.api.scan() ?? null,
118
+ styles: config.styles,
119
+ });
120
+
121
+ /**
122
+ * rollup-plugin-svelte `preprocess` entry: the root class on every view, so the compiled markup has it.
123
+ *
124
+ * @param {'server' | 'client'} side
125
+ */
126
+ function classPreprocess(side) {
127
+ return panoSemanticClasses({
128
+ ns: config.namespace,
129
+ // rollup-plugin-svelte hands the preprocessors a path relative to the current directory
130
+ views: (file) => {
131
+ const absolute = path.resolve(file);
132
+
133
+ return viewDirs.some((dir) => absolute.startsWith(dir + path.sep))
134
+ ? path.basename(absolute, '.svelte')
135
+ : null;
136
+ },
137
+ // both bundles compile the same sources: say it once
138
+ warn: side === 'client' ? (message) => console.warn(`[pano-plugin] ${message}`) : () => {},
139
+ });
140
+ }
141
+
142
+ /**
143
+ * @param {'server' | 'client'} side
144
+ * @returns {import('rollup').Plugin[]}
145
+ */
146
+ function kitPlugins(side) {
147
+ const client = side === 'client';
148
+ const context = contexts[side];
149
+ const own = { ...common, side };
150
+ const views = panoViews({
151
+ ...own,
152
+ ...ids,
153
+ dirs: viewDirs,
154
+ transformSource: (source, ctx) => viewClassInfo(source, ctx).code,
155
+ classes: (info) => policy.infoOf(info).classes,
156
+ });
157
+
158
+ if (client) viewsPlugin = views;
159
+
160
+ return [
161
+ panoEntry({ ...own, ...ids, controllersDir, main: mainFile }),
162
+ ...(pluginApiFactory ? [pluginApiFactory(config.pluginId)] : []),
163
+ ...(options.plugins ?? []),
164
+ ...(config.sdkDir ? [sdkAliasPlugin(config.sdkDir)] : []),
165
+ panoControllersModule({ ...own, dir: controllersDir }),
166
+ views,
167
+ ...(client
168
+ ? [
169
+ panoRules({ ...own, viewDirs, controllersDir, context }),
170
+ panoHelpers({ ...own, viewDirs, context }),
171
+ ]
172
+ : []),
173
+ panoStamp({ ...own, viewDirs, context }),
174
+ ...(client
175
+ ? [
176
+ panoBuildContext({ root, context, scan: () => views.api.scan(), policy }),
177
+ panoMeta({ ...own, ...ids }),
178
+ panoTypes({ ...own, dir: controllersDir }),
179
+ panoSamples({ ...own, viewDirs }),
180
+ panoClientGen({ ...own, ...ids }),
181
+ panoFallbackCss({
182
+ ...own,
183
+ ns: config.namespace,
184
+ viewsDir: viewDirs,
185
+ safelist: config.styles.safelist,
186
+ icons: config.styles.icons,
187
+ }),
188
+ panoPluginCss({ ...own, ns: config.namespace }),
189
+ ]
190
+ : []),
191
+ ];
192
+ }
193
+
194
+ /**
195
+ * @param {boolean} browser
196
+ * @returns {import('rollup').Plugin}
197
+ */
198
+ function nodeResolve(browser) {
199
+ return resolve({
200
+ browser,
201
+ rootDir: root,
202
+ dedupe: browser ? ['svelte', '@panomc/sdk'] : ['svelte'],
203
+ // Local delivery: the sdk folder holds the svelte version the plugin is pinned to.
204
+ ...(config.sdkDir ? { modulePaths: [path.join(config.sdkDir, 'node_modules')] } : {}),
205
+ });
206
+ }
207
+
208
+ /** @returns {import('rollup').Plugin[]} */
209
+ function tail(dirName, withManifest) {
210
+ return [
211
+ ...(minify ? [terser()] : []),
212
+ ...(withManifest ? [manifestPlugin(dirName)] : []),
213
+ ];
214
+ }
215
+
216
+ const output = {
217
+ format: /** @type {const} */ ('es'),
218
+ chunkFileNames: '[name]-[hash].js',
219
+ // The entry module state must live in ONE query-less chunk: the host imports client.mjs with a
220
+ // cache-busting query while lazy chunks import './client.mjs' query-less, and the browser keys its module
221
+ // map by full URL. client.mjs / server.mjs is therefore a pure re-export facade (pano:entry) and the
222
+ // wrapper plus the author's src/main.js are forced into the shared 'main' chunk.
223
+ manualChunks(/** @type {string} */ id) {
224
+ if (id === WRAPPER_RESOLVED_ID || path.resolve(id) === mainFile) return 'main';
225
+ },
226
+ };
227
+
228
+ // Svelte's own internals import each other in circles; that is noise, not a problem of the plugin.
229
+ /** @type {import('rollup').WarningHandlerWithDefault} */
230
+ const onwarn = (warning, defaultHandler) => {
231
+ if (
232
+ warning.code === 'CIRCULAR_DEPENDENCY' &&
233
+ (warning.ids ?? []).every((id) => id.includes('node_modules'))
234
+ ) {
235
+ return;
236
+ }
237
+
238
+ defaultHandler(warning);
239
+ };
240
+
241
+ /** @type {Record<string, import('rollup').RollupOptions>} */
242
+ const builds = {
243
+ server: {
244
+ input: ENTRY_ID,
245
+ output: {
246
+ ...output,
247
+ dir: path.join(outDir, 'server'),
248
+ entryFileNames: 'server.mjs',
249
+ },
250
+ plugins: [
251
+ ...kitPlugins('server'),
252
+ nodeResolve(false),
253
+ svelte({
254
+ compilerOptions: { generate: 'server', css: 'external' },
255
+ emitCss: false,
256
+ preprocess: classPreprocess('server'),
257
+ }),
258
+ ...tail('server', true),
259
+ ],
260
+ preserveEntrySignatures: 'strict',
261
+ },
262
+
263
+ client: {
264
+ input: ENTRY_ID,
265
+ output: {
266
+ ...output,
267
+ dir: path.join(outDir, 'client'),
268
+ entryFileNames: 'client.mjs',
269
+ },
270
+ // Bare 'svelte'/'svelte/*', 'svelte-i18n' and '@panomc/sdk*' specifiers stay EXTERNAL in both dev and
271
+ // production: the host injects an import map that resolves them to stable /runtime shim modules, and each
272
+ // shim re-exports the HOST bundle's own live module instance. Host pages and plugins therefore share a
273
+ // single Svelte runtime and a single SDK instance. Bundling a private SDK copy would split store and
274
+ // context state, so the SDK is never bundled in normal builds (BUNDLE_SDK=true is the escape hatch).
275
+ // The match is exact/subpath, NOT a prefix: third-party packages such as 'svelte-select' must be bundled.
276
+ external: (id, importer) => {
277
+ if (bundleSdk) return false;
278
+ if (options.bundle?.(id, importer)) return false;
279
+
280
+ return isHostSpecifier(id);
281
+ },
282
+ plugins: [
283
+ ...kitPlugins('client'),
284
+ nodeResolve(true),
285
+ svelte({
286
+ // emitCss: true hands the component CSS to panoPluginCss (plugin.css); svelte sets css: 'external' itself
287
+ compilerOptions: { generate: 'client', dev },
288
+ emitCss: true,
289
+ preprocess: classPreprocess('client'),
290
+ }),
291
+ ...tail('client', true),
292
+ ],
293
+ preserveEntrySignatures: 'strict',
294
+ },
295
+
296
+ // Standalone controllers: controllers/controllers.mjs. The input is the virtual `pano:controllers`
297
+ // module (a name -> definition map); panoControllers() may replace it from its `options` hook.
298
+ controllers: {
299
+ input: CONTROLLERS_ID,
300
+ output: {
301
+ format: 'es',
302
+ dir: path.join(outDir, 'controllers'),
303
+ entryFileNames: 'controllers.mjs',
304
+ inlineDynamicImports: true,
305
+ },
306
+ plugins: [
307
+ panoControllersModule({ ...common, side: 'controllers', dir: controllersDir }),
308
+ panoControllers({ ...common, side: 'controllers', dir: controllersDir }),
309
+ nodeResolve(false),
310
+ ...tail('controllers', false),
311
+ ],
312
+ },
313
+
314
+ // Widgets (doc 06 section 3): panoWidgets() owns the input through its `options` hook; until it sets
315
+ // one this target builds an empty module.
316
+ widgets: {
317
+ input: '\0pano-widgets-empty',
318
+ output: {
319
+ format: 'es',
320
+ dir: path.join(outDir, 'widgets'),
321
+ entryFileNames: '[name].mjs',
322
+ },
323
+ plugins: [
324
+ {
325
+ name: 'pano-widgets-empty',
326
+ resolveId: (source) => (source === '\0pano-widgets-empty' ? source : null),
327
+ load: (id) => (id === '\0pano-widgets-empty' ? 'export default {};\n' : null),
328
+ },
329
+ panoWidgets({ ...common, side: 'widgets', ...ids }),
330
+ nodeResolve(true),
331
+ svelte({
332
+ compilerOptions: { generate: 'client', css: 'external', dev },
333
+ emitCss: false,
334
+ preprocess: classPreprocess('server'),
335
+ }),
336
+ ...tail('widgets', false),
337
+ ],
338
+ },
339
+ };
340
+
341
+ // The default set: client first (only its scan of the views knows the part classes the lock holds, so a
342
+ // server build run before it would stop on the lock of the previous build), then server, the controllers
343
+ // when the plugin has any and the widgets when a view declares `widget`.
344
+ const hasWidgets =
345
+ !options.side &&
346
+ (await hasWidgetViews({
347
+ pluginId: config.pluginId,
348
+ namespace: config.namespace,
349
+ root,
350
+ viewDirs: config.viewDirs,
351
+ }));
352
+ const selected = options.side
353
+ ? [options.side]
354
+ : [
355
+ 'client',
356
+ 'server',
357
+ ...(hasControllers ? ['controllers'] : []),
358
+ ...(hasWidgets ? ['widgets'] : []),
359
+ ];
360
+
361
+ const configs = selected.map((name) => ({ ...builds[name], onwarn }));
362
+
363
+ // The output folder is emptied once, by the first build that runs. The widgets build never empties it:
364
+ // it completes a package the other builds wrote.
365
+ if (selected[0] !== 'widgets') {
366
+ configs[0].plugins = [
367
+ del({ targets: [`${outDir.replace(/\\/g, '/')}/*`], runOnce: true, force: true }),
368
+ ...(configs[0].plugins ?? []),
369
+ ];
370
+ }
371
+
372
+ return configs;
373
+ }
374
+
375
+ /**
376
+ * Specifiers the host provides through its import map (client build).
377
+ *
378
+ * @param {string} id
379
+ * @returns {boolean}
380
+ */
381
+ function isHostSpecifier(id) {
382
+ if (id === PLUGIN_API_SPECIFIER) return false; // a virtual module of the kit, bundled
383
+ return (
384
+ id === 'svelte' ||
385
+ id.startsWith('svelte/') ||
386
+ id === 'svelte-i18n' ||
387
+ id === '@panomc/sdk' ||
388
+ id.startsWith('@panomc/sdk/')
389
+ );
390
+ }
391
+
392
+ /**
393
+ * Writes `manifest.json` (the list of emitted files) into the output folder of a build.
394
+ *
395
+ * @param {string} name
396
+ * @returns {import('rollup').Plugin}
397
+ */
398
+ function manifestPlugin(name) {
399
+ return {
400
+ name: `pano-manifest-${name}`,
401
+ writeBundle(options, bundle) {
402
+ fs.writeFileSync(
403
+ path.join(options.dir, 'manifest.json'),
404
+ JSON.stringify(Object.keys(bundle), null, 2),
405
+ );
406
+ },
407
+ };
408
+ }
409
+
410
+ /**
411
+ * Nearest `node_modules/<name>` folder above `from`.
412
+ *
413
+ * @param {string} from
414
+ * @param {string} name package name
415
+ * @returns {string | null}
416
+ */
417
+ export function findPackageDir(from, name) {
418
+ let dir = path.resolve(from);
419
+
420
+ for (;;) {
421
+ const candidate = path.join(dir, 'node_modules', name);
422
+
423
+ if (fs.existsSync(path.join(candidate, 'package.json'))) return fs.realpathSync(candidate);
424
+
425
+ const parent = path.dirname(dir);
426
+
427
+ if (parent === dir) return null;
428
+
429
+ dir = parent;
430
+ }
431
+ }
432
+
433
+ /**
434
+ * Loads `pluginApi` from the sdk's `build/plugin-api-rollup.js`, when the sdk has it.
435
+ *
436
+ * @param {string | null} sdkDir
437
+ * @returns {Promise<((pluginId: string) => import('rollup').Plugin) | null>}
438
+ */
439
+ async function loadPluginApiFactory(sdkDir) {
440
+ if (!sdkDir) return null;
441
+
442
+ const file = path.join(sdkDir, 'build/plugin-api-rollup.js');
443
+
444
+ if (!fs.existsSync(file)) return null;
445
+
446
+ const module = await import(pathToFileURL(file).href);
447
+
448
+ return typeof module.pluginApi === 'function' ? module.pluginApi : null;
449
+ }
450
+
451
+ /**
452
+ * The svelte COMPILER a plugin builds with must match the runtime the Pano host serves in the browser
453
+ * (compiled output and runtime are only compatible at the exact same version). @panomc/sdk pins the
454
+ * version, so the plugin must not declare svelte itself.
455
+ *
456
+ * @param {{ root: string, sdkDir: string | null }} input
457
+ */
458
+ export function checkSvelteVersion({ root, sdkDir }) {
459
+ /** @param {string} file */
460
+ const read = (file) => JSON.parse(fs.readFileSync(file, 'utf8'));
461
+
462
+ let sdkPin = null;
463
+ let installed = null;
464
+ let ownDecl = null;
465
+
466
+ try {
467
+ if (sdkDir) sdkPin = read(path.join(sdkDir, 'package.json')).dependencies?.svelte ?? null;
468
+ } catch {
469
+ // sdk not readable: rollup fails on its own with a clearer error.
470
+ }
471
+
472
+ const svelteDir = findPackageDir(root, 'svelte');
473
+
474
+ try {
475
+ const dir = svelteDir ?? (sdkDir ? path.join(sdkDir, 'node_modules/svelte') : null);
476
+
477
+ if (dir) installed = read(path.join(dir, 'package.json')).version;
478
+ } catch {
479
+ // svelte missing entirely: rollup-plugin-svelte fails on its own.
480
+ }
481
+
482
+ try {
483
+ const own = read(path.join(root, 'package.json'));
484
+
485
+ ownDecl =
486
+ own.dependencies?.svelte ??
487
+ own.devDependencies?.svelte ??
488
+ own.peerDependencies?.svelte ??
489
+ null;
490
+ } catch {
491
+ // no readable package.json: nothing to validate.
492
+ }
493
+
494
+ if (ownDecl) {
495
+ console.warn(
496
+ `[pano] WARNING: package.json declares svelte ${ownDecl}, but the svelte version comes from @panomc/sdk. ` +
497
+ `A local override can drift from the Pano host runtime and break the plugin at hydration: remove the svelte entry and re-install.`,
498
+ );
499
+ }
500
+
501
+ // The sdk pins an exact version; only enforce when it is one (not a range).
502
+ if (sdkPin && /^\d/.test(sdkPin) && installed && installed !== sdkPin) {
503
+ throw new Error(
504
+ `[pano] installed svelte is ${installed} but @panomc/sdk requires exactly ${sdkPin}. ` +
505
+ `Compiled plugin output is only compatible with the Pano host runtime at the same version. ` +
506
+ `Remove any svelte override from package.json and re-install.`,
507
+ );
508
+ }
509
+
510
+ if (!sdkPin && installed) {
511
+ console.warn(
512
+ `[pano] WARNING: the installed @panomc/sdk does not pin a svelte version; building with svelte ${installed}. ` +
513
+ `Make sure it matches the Pano host runtime version.`,
514
+ );
515
+ }
516
+ }
517
+
518
+ /**
519
+ * Local delivery (`PANO_SDK_DIR`): resolves `@panomc/sdk` and `@panomc/sdk/<subpath>` to files of that
520
+ * folder through its package.json `exports` map, as a published install would.
521
+ *
522
+ * @param {string} sdkDir absolute sdk package folder
523
+ * @returns {import('rollup').Plugin}
524
+ */
525
+ function sdkAliasPlugin(sdkDir) {
526
+ /** @type {Record<string, any>} */
527
+ let exportsMap = {};
528
+
529
+ try {
530
+ exportsMap = JSON.parse(fs.readFileSync(path.join(sdkDir, 'package.json'), 'utf8')).exports ?? {};
531
+ } catch {
532
+ // no exports map: fall back to plain file paths below.
533
+ }
534
+
535
+ /** @param {any} target */
536
+ const pick = (target) => {
537
+ if (typeof target === 'string') return target;
538
+ if (!target || typeof target !== 'object') return null;
539
+
540
+ for (const key of ['svelte', 'import', 'default']) {
541
+ if (key in target) {
542
+ const found = pick(target[key]);
543
+
544
+ if (found) return found;
545
+ }
546
+ }
547
+
548
+ return null;
549
+ };
550
+
551
+ return {
552
+ name: 'pano-sdk-alias',
553
+ resolveId(source) {
554
+ if (source !== '@panomc/sdk' && !source.startsWith('@panomc/sdk/')) return null;
555
+
556
+ const subpath = source === '@panomc/sdk' ? '.' : `./${source.slice('@panomc/sdk/'.length)}`;
557
+ let target = pick(exportsMap[subpath]);
558
+
559
+ if (!target) {
560
+ for (const [pattern, value] of Object.entries(exportsMap)) {
561
+ const star = pattern.indexOf('*');
562
+
563
+ if (star < 0) continue;
564
+
565
+ const prefix = pattern.slice(0, star);
566
+ const suffix = pattern.slice(star + 1);
567
+
568
+ if (subpath.startsWith(prefix) && subpath.endsWith(suffix) && subpath.length >= prefix.length + suffix.length) {
569
+ const matched = subpath.slice(prefix.length, subpath.length - suffix.length);
570
+ const picked = pick(value);
571
+
572
+ if (picked) {
573
+ target = picked.replace('*', matched);
574
+ break;
575
+ }
576
+ }
577
+ }
578
+ }
579
+
580
+ if (!target) return null;
581
+
582
+ return path.resolve(sdkDir, target);
583
+ },
584
+ };
585
+ }
586
+
587
+ export { panoEntry, panoControllersModule } from './entry.js';
588
+ export { panoViews } from './views.js';
589
+ export { panoRules } from './rules.js';
590
+ export { panoHelpers } from './helpers.js';
591
+ export { panoStamp } from './stamp.js';
592
+ export { panoControllers } from './controllers.js';
593
+ export { panoMeta } from './meta.js';
594
+ export { panoTypes } from './types.js';
595
+ export { panoSamples } from './samples.js';
596
+ export { panoWidgets } from './widgets.js';
597
+ export { panoClientGen } from './client-gen.js';
598
+ export { readLock, writeLockSection, diffSection } from './lock.js';
599
+ export { panoSemanticClasses } from '../styles/semantic-classes.js';
600
+ export { panoFallbackCss } from '../styles/fallback-css.js';
601
+ export { panoPluginCss } from '../styles/plugin-css.js';