@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,522 @@
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import { build, buildError, cleanup, exists, MAIN, makePlugin, read } from "./helpers.js";
3
+ import { scanViews } from "../../views.js";
4
+
5
+ afterEach(cleanup);
6
+
7
+ const HELLO = `<script module>
8
+ export const view = { path: "/hello" };
9
+ </script>
10
+ <h1>Hello</h1>
11
+ `;
12
+
13
+ const CARD = `<script module>
14
+ export const view = {
15
+ contract: 2,
16
+ block: true,
17
+ slot: ["demo:shelf", "other:shelf"],
18
+ sidebar: "profile",
19
+ id: "demo-card",
20
+ priority: 5,
21
+ home: { label: "Card home" },
22
+ };
23
+ </script>
24
+ <script>
25
+ import Price from "./Price.svelte";
26
+ import { t } from "svelte-i18n";
27
+
28
+ /** @type {{ product: Product, settings?: any, size: number }} */
29
+ let { product, settings, size = 3, ...rest } = $props();
30
+ </script>
31
+ <Price {product} />
32
+ <PluginSlot id="demo:card:footer" props={{ product }} />
33
+ {#if product}
34
+ <div><PluginSlot id={"demo:card:aside"} /></div>
35
+ {/if}
36
+ <Hook name="demo:card:top" />
37
+ `;
38
+
39
+ const PRICE = `<script>
40
+ /** @type {number} */
41
+ export let amount;
42
+ /** @type {string} */
43
+ export let currency = "USD";
44
+ export let note;
45
+ </script>
46
+ <span>{amount}{currency}</span>
47
+ `;
48
+
49
+ const fixture = (extra = {}) =>
50
+ makePlugin({
51
+ "src/main.js": MAIN,
52
+ "src/theme/views/HelloPage.svelte": HELLO,
53
+ "src/theme/views/ProductCard.svelte": CARD,
54
+ "src/theme/views/Price.svelte": PRICE,
55
+ ...extra,
56
+ });
57
+
58
+ describe("panoViews registration", () => {
59
+ test("one file is one view with its page, in pano:views and views.json", async () => {
60
+ const root = makePlugin({ "src/main.js": MAIN, "src/theme/views/HelloPage.svelte": HELLO });
61
+ const { code } = await build(root);
62
+
63
+ expect(code).toContain('"name": "demo:HelloPage"');
64
+ expect(code).toContain('"pluginId": "pano-plugin-demo"');
65
+ expect(code).toContain('"path":"/hello"');
66
+
67
+ const json = JSON.parse(read(root, "out/contract/views.json"));
68
+
69
+ expect(json).toEqual({
70
+ namespace: "demo",
71
+ pluginId: "pano-plugin-demo",
72
+ version: "1.2.3",
73
+ sdk: 2,
74
+ views: {
75
+ "demo:HelloPage": {
76
+ kind: "page",
77
+ contract: 1,
78
+ source: "src/views/HelloPage.svelte",
79
+ props: {},
80
+ uses: [],
81
+ slots: [],
82
+ hooks: [],
83
+ block: false,
84
+ inject: null,
85
+ page: { path: "/hello" },
86
+ home: null,
87
+ widget: null,
88
+ },
89
+ },
90
+ });
91
+ expect(read(root, "out/contract/src/views/HelloPage.svelte")).toBe(HELLO);
92
+ });
93
+
94
+ test("a view with widget gets block: true in its record (doc 06 section 3.1)", async () => {
95
+ const root = makePlugin({
96
+ "src/main.js": MAIN,
97
+ "src/theme/views/GoalWidget.svelte": `<script module>\n export const view = { widget: true };\n</script>\n<p>goal</p>\n`,
98
+ "src/theme/views/TaggedWidget.svelte": `<script module>\n export const view = { widget: { tag: "pano-demo-tagged" } };\n</script>\n<p>tagged</p>\n`,
99
+ "src/theme/views/OffWidget.svelte": `<script module>\n export const view = { widget: false };\n</script>\n<p>off</p>\n`,
100
+ });
101
+
102
+ await build(root);
103
+
104
+ const { views } = JSON.parse(read(root, "out/contract/views.json"));
105
+
106
+ expect(views["demo:GoalWidget"].block).toBe(true);
107
+ expect(views["demo:GoalWidget"].widget).toBe(true);
108
+ expect(views["demo:TaggedWidget"].block).toBe(true);
109
+ expect(views["demo:OffWidget"].block).toBe(false);
110
+ });
111
+
112
+ test("props, uses, slots, hooks, injections and home are read without running the file", async () => {
113
+ const root = fixture();
114
+
115
+ await build(root);
116
+
117
+ const { views } = JSON.parse(read(root, "out/contract/views.json"));
118
+
119
+ expect(views["demo:ProductCard"]).toEqual({
120
+ kind: "component",
121
+ contract: 2,
122
+ source: "src/views/ProductCard.svelte",
123
+ props: {
124
+ product: { type: "Product", required: true },
125
+ settings: { type: "any", required: false },
126
+ size: { type: "number", required: false },
127
+ },
128
+ uses: ["demo:Price"],
129
+ slots: ["demo:card:aside", "demo:card:footer"],
130
+ hooks: ["demo:card:top"],
131
+ block: true,
132
+ inject: [
133
+ { slot: "demo:shelf", id: "demo-card", priority: 5 },
134
+ { slot: "other:shelf", id: "demo-card", priority: 5 },
135
+ { sidebar: "profile", id: "demo-card", priority: 5 },
136
+ ],
137
+ page: null,
138
+ home: { label: "Card home" },
139
+ widget: null,
140
+ });
141
+ expect(views["demo:Price"].props).toEqual({
142
+ amount: { type: "number", required: true },
143
+ currency: { type: "string", required: false },
144
+ note: { type: "any", required: true },
145
+ });
146
+ });
147
+
148
+ test("pano:views holds a lazy component for every view, sorted, with the owner id", async () => {
149
+ const root = fixture();
150
+ const { code } = await build(root);
151
+
152
+ expect(code).toMatch(/"component": \(\) => import\(/);
153
+ expect(code.indexOf("demo:HelloPage")).toBeLessThan(code.indexOf("demo:Price"));
154
+ expect(code.indexOf("demo:Price")).toBeLessThan(code.indexOf("demo:ProductCard"));
155
+ expect(code.match(/"pluginId": "pano-plugin-demo"/g)).toHaveLength(3);
156
+ });
157
+
158
+ test("a bare file with no export const view is a component", async () => {
159
+ const root = makePlugin({ "src/main.js": MAIN, "src/theme/views/Plain.svelte": "<p>x</p>" });
160
+ const scan = await scanViews({ dirs: ["src/theme/views"], namespace: "demo", pluginId: "pano-plugin-demo", root });
161
+
162
+ expect(scan.views.get("Plain").record.kind).toBe("component");
163
+ expect(scan.views.get("Plain").record.contract).toBe(1);
164
+ });
165
+ });
166
+
167
+ describe("panoViews proxies", () => {
168
+ test("an import of a view file becomes a proxy, the registry keeps the raw component", async () => {
169
+ const root = fixture({
170
+ "src/main.js": `import views from "pano:views";\nimport Card from "./theme/views/ProductCard.svelte";\nexport { Card };\nexport default views;\n`,
171
+ });
172
+ const { code } = await build(root);
173
+
174
+ expect(code).toContain('createViewProxy("demo:ProductCard"');
175
+ // the card imports Price, which is proxied too
176
+ expect(code).toContain('createViewProxy("demo:Price"');
177
+ // pano:views imports the raw files: HelloPage has no importer but the registry and no proxy
178
+ expect(code).not.toContain('createViewProxy("demo:HelloPage"');
179
+ });
180
+
181
+ test("the proxy re-exports the named exports of the view and its default", async () => {
182
+ const root = fixture({
183
+ "src/main.js": `import Hello, { view } from "./theme/views/HelloPage.svelte";\nexport { Hello, view };\n`,
184
+ });
185
+ const { code } = await build(root);
186
+
187
+ expect(code).toContain('createViewProxy("demo:HelloPage"');
188
+ expect(code).toContain("view = ");
189
+ });
190
+
191
+ test("files that are not views stay as they are", async () => {
192
+ const root = fixture({
193
+ "src/Other.svelte": "<p>other</p>",
194
+ "src/main.js": `import Other from "./Other.svelte";\nexport default Other;\n`,
195
+ });
196
+ const { code } = await build(root);
197
+
198
+ expect(code).not.toContain("createViewProxy(");
199
+ });
200
+ });
201
+
202
+ describe("panoViews contract files", () => {
203
+ test("every view is copied to contract/src, after transformSource", async () => {
204
+ const root = fixture();
205
+ const seen = [];
206
+
207
+ await build(root, {
208
+ transformSource: (source, ctx) => {
209
+ seen.push(ctx.view);
210
+
211
+ return `<!-- ${ctx.ns}:${ctx.view} -->\n${source}`;
212
+ },
213
+ });
214
+
215
+ expect(seen.sort()).toEqual(["HelloPage", "Price", "ProductCard"]);
216
+ expect(read(root, "out/contract/src/views/Price.svelte")).toBe(`<!-- demo:Price -->\n${PRICE}`);
217
+ // the lock and views.json are built from the original, not the transformed text
218
+ expect(JSON.parse(read(root, "out/contract/views.json")).views["demo:Price"].props.amount.required).toBe(true);
219
+ });
220
+
221
+ test("the folder layout under src/theme is kept and a removed view leaves no stale copy", async () => {
222
+ const root = makePlugin({
223
+ "src/main.js": MAIN,
224
+ "src/theme/pages/Home.svelte": "<p>h</p>",
225
+ "src/theme/components/store/Badge.svelte": "<p>b</p>",
226
+ });
227
+
228
+ await build(root, { dirs: ["src/theme/pages", "src/theme/components"] });
229
+
230
+ expect(exists(root, "out/contract/src/pages/Home.svelte")).toBe(true);
231
+ expect(exists(root, "out/contract/src/components/store/Badge.svelte")).toBe(true);
232
+
233
+ const views = JSON.parse(read(root, "out/contract/views.json")).views;
234
+
235
+ expect(views["demo:Badge"].source).toBe("src/components/store/Badge.svelte");
236
+
237
+ // delete one file and rebuild
238
+ const fs = await import("node:fs");
239
+
240
+ fs.rmSync(`${root}/src/theme/components/store/Badge.svelte`);
241
+ await build(root, { dirs: ["src/theme/pages", "src/theme/components"] });
242
+
243
+ expect(exists(root, "out/contract/src/components/store/Badge.svelte")).toBe(false);
244
+ expect(exists(root, "out/contract/src/pages/Home.svelte")).toBe(true);
245
+ });
246
+
247
+ test("a missing view folder is a warning, not an error", async () => {
248
+ const root = makePlugin({ "src/main.js": MAIN });
249
+ const { logs } = await build(root);
250
+
251
+ expect(logs.join("\n")).toContain("view folder src/theme/views does not exist");
252
+ });
253
+
254
+ test("an unknown key in export const view is a warning", async () => {
255
+ const root = makePlugin({
256
+ "src/main.js": MAIN,
257
+ "src/theme/views/A.svelte": `<script module>export const view = { pth: "/a" };</script>`,
258
+ });
259
+ const { logs } = await build(root);
260
+
261
+ expect(logs.join("\n")).toContain('unknown key "pth"');
262
+ });
263
+ });
264
+
265
+ describe("panoViews errors name file, line and fix", () => {
266
+ test("duplicate basename", async () => {
267
+ const root = makePlugin({
268
+ "src/main.js": MAIN,
269
+ "src/theme/pages/Card.svelte": "<p/>",
270
+ "src/theme/components/Card.svelte": "<p/>",
271
+ });
272
+ const error = await buildError(root, { dirs: ["src/theme/pages", "src/theme/components"] });
273
+
274
+ expect(error.message).toBe(
275
+ 'src/theme/components/Card.svelte:1 view name "Card" is already used by src/theme/pages/Card.svelte — the file name is the view name, so rename one of the two files',
276
+ );
277
+ });
278
+
279
+ test("non-literal view", async () => {
280
+ const root = makePlugin({
281
+ "src/main.js": MAIN,
282
+ "src/theme/views/A.svelte": `<script module>\n const p = "/a";\n export const view = { path: p };\n</script>`,
283
+ });
284
+ const error = await buildError(root);
285
+
286
+ expect(error.message).toContain("src/theme/views/A.svelte:3 export const view is not a literal (view.path)");
287
+ expect(error.message).toContain("move computed values elsewhere");
288
+ });
289
+
290
+ test("a view that is not an object literal", async () => {
291
+ const root = makePlugin({
292
+ "src/main.js": MAIN,
293
+ "src/theme/views/A.svelte": `<script module>\n export const view = makeView();\n</script>`,
294
+ });
295
+ const error = await buildError(root);
296
+
297
+ expect(error.message).toContain("src/theme/views/A.svelte:2 export const view must be an object literal");
298
+ });
299
+
300
+ test("a call nested in the view names the key", async () => {
301
+ const root = makePlugin({
302
+ "src/main.js": MAIN,
303
+ "src/theme/views/A.svelte": `<script module>\n export const view = { home: { label: t("x") } };\n</script>`,
304
+ });
305
+ const error = await buildError(root);
306
+
307
+ expect(error.message).toContain("export const view is not a literal (view.home.label)");
308
+ });
309
+
310
+ test("reserved namespace", async () => {
311
+ const root = makePlugin({ "src/main.js": MAIN, "src/theme/views/A.svelte": "<p/>" });
312
+ const error = await buildError(root, { namespace: "core" });
313
+
314
+ expect(error.message).toContain('namespace "core" of pano-plugin-demo is reserved');
315
+ expect(error.message).toContain("set another one with namespace in pano.plugin.js");
316
+ });
317
+
318
+ test("slot id outside the own namespace", async () => {
319
+ const root = makePlugin({
320
+ "src/main.js": MAIN,
321
+ "src/theme/views/Checkout.svelte": `<div>\n<PluginSlot id="shop:pay" />\n</div>`,
322
+ });
323
+ const error = await buildError(root);
324
+
325
+ expect(error.message).toContain('src/theme/views/Checkout.svelte:2 <PluginSlot id="shop:pay"> is outside this plugin\'s namespace');
326
+ expect(error.message).toContain('rename it to "demo:pay"');
327
+ });
328
+
329
+ test("non-literal PluginSlot id", async () => {
330
+ const root = makePlugin({
331
+ "src/main.js": MAIN,
332
+ "src/theme/views/Checkout.svelte": `<script>let id = "demo:x";</script>\n<PluginSlot {id} />`,
333
+ });
334
+ const error = await buildError(root);
335
+
336
+ expect(error.message).toContain("src/theme/views/Checkout.svelte:2 <PluginSlot> needs a literal id");
337
+ });
338
+
339
+ test("PluginSlot id from an expression of a variable", async () => {
340
+ const root = makePlugin({
341
+ "src/main.js": MAIN,
342
+ "src/theme/views/Checkout.svelte": `<script>let id = "demo:x";</script>\n<PluginSlot id={id} />`,
343
+ });
344
+ const error = await buildError(root);
345
+
346
+ expect(error.message).toContain("<PluginSlot> needs a literal id");
347
+ });
348
+
349
+ test("view literal in a js file naming no view", async () => {
350
+ const root = makePlugin({
351
+ "src/main.js": MAIN,
352
+ "src/theme/register.js": `export default [\n { id: "x", view: "demo:Ghost" },\n];\n`,
353
+ "src/theme/views/A.svelte": "<p/>",
354
+ });
355
+ const error = await buildError(root);
356
+
357
+ expect(error.message).toBe(
358
+ 'src/theme/register.js:2 view: "demo:Ghost" names no view of this plugin — create Ghost.svelte in a view folder, or fix the name',
359
+ );
360
+ });
361
+
362
+ test("a literal naming a view of another plugin is left alone", async () => {
363
+ const root = makePlugin({
364
+ "src/main.js": MAIN,
365
+ "src/theme/register.js": `export default [{ view: "other:Thing" }, { view: "demo:A" }];\n`,
366
+ "src/theme/views/A.svelte": "<p/>",
367
+ });
368
+
369
+ await build(root);
370
+ });
371
+
372
+ test("a view file name that is not a view name", async () => {
373
+ const root = makePlugin({ "src/main.js": MAIN, "src/theme/views/product-card.svelte": "<p/>" });
374
+ const error = await buildError(root);
375
+
376
+ expect(error.message).toContain("src/theme/views/product-card.svelte:1 view file names must start with an uppercase letter");
377
+ expect(error.message).toContain("rename it, for example to ProductCard.svelte");
378
+ });
379
+
380
+ test("a syntax error in a view names the file", async () => {
381
+ const root = makePlugin({ "src/main.js": MAIN, "src/theme/views/A.svelte": "<script>\n let = ;\n</script>" });
382
+ const error = await buildError(root);
383
+
384
+ expect(error.message).toContain("src/theme/views/A.svelte:");
385
+ });
386
+ });
387
+
388
+ describe("panoViews lock", () => {
389
+ const lockOf = (root) => JSON.parse(read(root, "pano-plugin.lock.json"));
390
+
391
+ test("a build writes the views section; props and slots are recorded", async () => {
392
+ const root = fixture();
393
+
394
+ await build(root);
395
+
396
+ const lock = lockOf(root);
397
+
398
+ expect(lock.format).toBe(1);
399
+ expect(lock.views["demo:ProductCard"]).toEqual({
400
+ contract: 2,
401
+ props: { product: { required: true }, settings: { required: false }, size: { required: false } },
402
+ slots: ["demo:card:aside", "demo:card:footer"],
403
+ hooks: ["demo:card:top"],
404
+ });
405
+ });
406
+
407
+ test("removing a prop without raising contract fails the build with the exact message", async () => {
408
+ const root = fixture();
409
+
410
+ await build(root);
411
+
412
+ const fs = await import("node:fs");
413
+
414
+ fs.writeFileSync(
415
+ `${root}/src/theme/views/ProductCard.svelte`,
416
+ CARD.replace("let { product, settings, size = 3, ...rest }", "let { product, size = 3, ...rest }").replace("contract: 2,", "contract: 2,"),
417
+ );
418
+
419
+ const error = await buildError(root);
420
+
421
+ expect(error.message).toBe(
422
+ 'demo:ProductCard: prop "settings" removed — set contract: 3 in ProductCard.svelte (never published? delete pano-plugin.lock.json)',
423
+ );
424
+ });
425
+
426
+ test("the same change in a watch build is one warning and the lock stays", async () => {
427
+ const root = fixture();
428
+
429
+ await build(root);
430
+
431
+ const before = read(root, "pano-plugin.lock.json");
432
+ const fs = await import("node:fs");
433
+
434
+ fs.writeFileSync(`${root}/src/theme/views/ProductCard.svelte`, CARD.replace("settings, ", ""));
435
+
436
+ const { logs } = await build(root, { watch: true });
437
+
438
+ expect(logs.filter((line) => line.startsWith("demo:"))).toEqual([
439
+ 'demo:ProductCard: prop "settings" removed — set contract: 3 in ProductCard.svelte (never published? delete pano-plugin.lock.json)',
440
+ ]);
441
+ expect(read(root, "pano-plugin.lock.json")).toBe(before);
442
+ });
443
+
444
+ test("raising the contract passes and rewrites the lock", async () => {
445
+ const root = fixture();
446
+
447
+ await build(root);
448
+
449
+ const fs = await import("node:fs");
450
+
451
+ fs.writeFileSync(`${root}/src/theme/views/ProductCard.svelte`, CARD.replace("settings, ", "").replace("contract: 2", "contract: 3"));
452
+ await build(root);
453
+
454
+ expect(lockOf(root).views["demo:ProductCard"].contract).toBe(3);
455
+ expect(lockOf(root).views["demo:ProductCard"].props.settings).toBeUndefined();
456
+ });
457
+
458
+ test("a new required prop, and changed slots, need a raised contract", async () => {
459
+ const root = fixture();
460
+
461
+ await build(root);
462
+
463
+ const fs = await import("node:fs");
464
+
465
+ fs.writeFileSync(`${root}/src/theme/views/ProductCard.svelte`, CARD.replace("size = 3", "size, label"));
466
+
467
+ expect((await buildError(root)).message).toBe(
468
+ 'demo:ProductCard: required prop "label" added — set contract: 3 in ProductCard.svelte (never published? delete pano-plugin.lock.json)',
469
+ );
470
+
471
+ fs.writeFileSync(`${root}/src/theme/views/ProductCard.svelte`, CARD.replace('<PluginSlot id="demo:card:footer" props={{ product }} />', ""));
472
+
473
+ expect((await buildError(root)).message).toContain("demo:ProductCard: slots changed — set contract: 3");
474
+ });
475
+
476
+ test("a new optional prop and a new view only update the lock", async () => {
477
+ const root = fixture();
478
+
479
+ await build(root);
480
+
481
+ const fs = await import("node:fs");
482
+
483
+ fs.writeFileSync(`${root}/src/theme/views/ProductCard.svelte`, CARD.replace("size = 3", "size = 3, badge = null"));
484
+ fs.writeFileSync(`${root}/src/theme/views/Extra.svelte`, "<p/>");
485
+ await build(root);
486
+
487
+ expect(lockOf(root).views["demo:ProductCard"].props.badge).toEqual({ required: false });
488
+ expect(lockOf(root).views["demo:Extra"]).toBeDefined();
489
+ });
490
+
491
+ test("classes written by the style step survive a rewrite", async () => {
492
+ const root = fixture();
493
+
494
+ await build(root);
495
+
496
+ const fs = await import("node:fs");
497
+ const lock = lockOf(root);
498
+
499
+ lock.views["demo:Price"].classes = ["demo-price"];
500
+ fs.writeFileSync(`${root}/pano-plugin.lock.json`, JSON.stringify(lock));
501
+ fs.writeFileSync(`${root}/src/theme/views/Price.svelte`, PRICE.replace("export let note;", "export let note;\n export let extra = 1;"));
502
+ await build(root);
503
+
504
+ expect(lockOf(root).views["demo:Price"].classes).toEqual(["demo-price"]);
505
+ expect(lockOf(root).views["demo:Price"].props.extra).toEqual({ required: false });
506
+ });
507
+
508
+ test("lock: false leaves the lock file alone", async () => {
509
+ const root = fixture();
510
+
511
+ await build(root, { lock: false });
512
+
513
+ expect(exists(root, "pano-plugin.lock.json")).toBe(false);
514
+ });
515
+
516
+ test("a corrupt lock file fails with the fix", async () => {
517
+ const root = fixture({ "pano-plugin.lock.json": "{nope" });
518
+ const error = await buildError(root);
519
+
520
+ expect(error.message).toContain("pano-plugin.lock.json is not valid JSON");
521
+ });
522
+ });
@@ -0,0 +1,117 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { rollup } from 'rollup';
6
+ import { panoPlugin } from '../../index.js';
7
+
8
+ const here = path.dirname(fileURLToPath(import.meta.url));
9
+
10
+ export const themeCore = path.resolve(here, '../../../../../..');
11
+ export const fixture = path.join(themeCore, 'test-fixtures/plugin-widgets');
12
+ export const pluginMin = path.join(themeCore, 'test-fixtures/plugin-min');
13
+ export const sdkDir = path.join(themeCore, 'packages/sdk');
14
+
15
+ /** @type {string[]} */
16
+ const made = [];
17
+
18
+ export function tempDir(prefix = 'pano-widgets-') {
19
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
20
+
21
+ made.push(dir);
22
+
23
+ return dir;
24
+ }
25
+
26
+ export function cleanup() {
27
+ while (made.length) fs.rmSync(made.pop(), { recursive: true, force: true });
28
+ }
29
+
30
+ /** Copies the fixture plugin (without built output) into a temp folder and applies `files` (path -> content, null = delete). */
31
+ export function copyFixture(files = {}) {
32
+ const root = tempDir('pano-widgets-plugin-');
33
+
34
+ fs.cpSync(fixture, root, {
35
+ recursive: true,
36
+ filter: (source) => !source.includes(`${path.sep}src${path.sep}main${path.sep}resources`),
37
+ });
38
+
39
+ for (const [file, content] of Object.entries(files)) {
40
+ const target = path.join(root, file);
41
+
42
+ if (content === null) fs.rmSync(target, { force: true });
43
+ else {
44
+ fs.mkdirSync(path.dirname(target), { recursive: true });
45
+ fs.writeFileSync(target, content);
46
+ }
47
+ }
48
+
49
+ return root;
50
+ }
51
+
52
+ export const read = (root, file) => fs.readFileSync(path.join(root, file), 'utf8');
53
+
54
+ /** Every file under `dir` by relative path (empty when the folder does not exist). */
55
+ export function filesOf(dir) {
56
+ /** @type {Record<string, string>} */
57
+ const files = {};
58
+
59
+ if (!fs.existsSync(dir)) return files;
60
+
61
+ const walk = (current) => {
62
+ for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
63
+ const file = path.join(current, entry.name);
64
+
65
+ if (entry.isDirectory()) walk(file);
66
+ else files[path.relative(dir, file).replace(/\\/g, '/')] = fs.readFileSync(file, 'utf8');
67
+ }
68
+ };
69
+
70
+ walk(dir);
71
+
72
+ return files;
73
+ }
74
+
75
+ /** Runs one `panoPlugin()` side to disk. @param {import('rollup').RollupOptions[]} configs */
76
+ export async function write(configs) {
77
+ for (const config of configs) {
78
+ const bundle = await rollup(config);
79
+
80
+ await bundle.write(/** @type {any} */ (config.output));
81
+ await bundle.close();
82
+ }
83
+ }
84
+
85
+ /** The configs of `panoPlugin()` for given sides, without the clean-up plugin (it would empty the folder again). */
86
+ export async function sides(root, outDir, names, options = {}) {
87
+ const configs = [];
88
+
89
+ for (const side of names) {
90
+ const [config] = await panoPlugin({ root, outDir, side, minify: false, ...options });
91
+
92
+ config.plugins = (config.plugins ?? []).filter((plugin) => plugin && plugin.name !== 'delete');
93
+ configs.push(config);
94
+ }
95
+
96
+ return configs;
97
+ }
98
+
99
+ /** Builds the widgets side and returns the written files of `widgets/`. */
100
+ export async function buildWidgets(root, options = {}) {
101
+ const out = tempDir('pano-widgets-out-');
102
+
103
+ await write(await sides(root, out, ['widgets'], options));
104
+
105
+ return { out, files: filesOf(path.join(out, 'widgets')) };
106
+ }
107
+
108
+ /** The message of the error a build throws, without the rollup plugin prefix. */
109
+ export async function buildError(root, options = {}) {
110
+ try {
111
+ await buildWidgets(root, options);
112
+ } catch (error) {
113
+ return String(error.message).replace(/^\[plugin pano-widgets\] /, '');
114
+ }
115
+
116
+ throw new Error('expected the widgets build to fail');
117
+ }