@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +46 -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 +72 -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,237 @@
1
+ /**
2
+ * A plugin's own CSS (doc 03 section 3.1): the `<style>` blocks of the components that end up in the
3
+ * client bundle are concatenated into `client/plugin.css`, wrapped in `@layer pano-plugin`, and the
4
+ * package index records `styles.own`. A plugin with no `<style>` emits no file.
5
+ *
6
+ * The CSS is taken in one of two ways, so the preset does not have to be switched:
7
+ *
8
+ * - `rollup-plugin-svelte` with `emitCss: true` hands every component's CSS to rollup as a virtual
9
+ * `<Component>.css` module; this plugin collects its code and turns the module into an empty one;
10
+ * - with `emitCss: false` (what the preset uses today) the CSS of a component is thrown away by
11
+ * that plugin, so this plugin compiles the component's `<style>` itself, with the same file name
12
+ * (`path.relative(process.cwd(), id)`) and the same options (`css: 'external'`), which gives the
13
+ * same Svelte hash class as the compiled markup carries.
14
+ *
15
+ * `plugin-css.js` also owns the small registry that `fallback-css.js` shares: the two files of the
16
+ * package (`fallback.css`, `plugin.css`) are hashed together into `styles.hash`.
17
+ */
18
+ import { createHash } from 'node:crypto';
19
+ import fs from 'node:fs';
20
+ import path from 'node:path';
21
+ import { compile } from 'svelte/compiler';
22
+ import { getBuildContext } from '../rollup/meta.js';
23
+ import { addRootClass } from './semantic-classes.js';
24
+
25
+ /** Package path of the fallback sheet, as `pano-plugin.json` states it. */
26
+ export const FALLBACK_PATH = 'client/fallback.css';
27
+ /** Package path of the plugin's own sheet, as `pano-plugin.json` states it. */
28
+ export const OWN_PATH = 'client/plugin.css';
29
+
30
+ /**
31
+ * @typedef {object} StyleRecord
32
+ * @property {string} [fallback] content of client/fallback.css
33
+ * @property {string} [own] content of client/plugin.css
34
+ * @property {boolean} [icons]
35
+ */
36
+
37
+ /** The files of the build that is running, by absolute plugin root. @type {Map<string, StyleRecord>} */
38
+ const records = new Map();
39
+
40
+ /** @param {string} root @returns {StyleRecord} */
41
+ function recordOf(root) {
42
+ const key = path.resolve(root);
43
+
44
+ if (!records.has(key)) records.set(key, {});
45
+
46
+ return /** @type {StyleRecord} */ (records.get(key));
47
+ }
48
+
49
+ /**
50
+ * First 8 hex digits of the sha1 over the fallback sheet and the plugin's own sheet (those that exist).
51
+ *
52
+ * @param {{ fallback?: string, own?: string }} files
53
+ * @returns {string}
54
+ */
55
+ export function styleHash(files) {
56
+ return createHash('sha1')
57
+ .update(files.fallback ?? '')
58
+ .update(files.own ?? '')
59
+ .digest('hex')
60
+ .slice(0, 8);
61
+ }
62
+
63
+ /**
64
+ * Rewrites `styles` of the shared build context from the files recorded so far.
65
+ *
66
+ * @param {string} root
67
+ */
68
+ function syncContext(root) {
69
+ const record = recordOf(root);
70
+ const context = getBuildContext(root);
71
+
72
+ if (record.fallback === undefined && record.own === undefined) {
73
+ context.styles = null;
74
+
75
+ return;
76
+ }
77
+
78
+ /** @type {Record<string, any>} */
79
+ const styles = {};
80
+
81
+ if (record.fallback !== undefined) styles.fallback = FALLBACK_PATH;
82
+ if (record.own !== undefined) styles.own = OWN_PATH;
83
+
84
+ styles.hash = styleHash(record);
85
+
86
+ if (record.icons !== undefined) styles.icons = record.icons;
87
+
88
+ context.styles = /** @type {any} */ (styles);
89
+ }
90
+
91
+ /**
92
+ * Records one emitted style file (and `icons` for the fallback sheet) for `pano-plugin.json`.
93
+ *
94
+ * @param {string} root plugin root
95
+ * @param {'fallback' | 'own'} kind
96
+ * @param {string} content
97
+ * @param {{ icons?: boolean }} [extra]
98
+ */
99
+ export function recordStyleFile(root, kind, content, extra = {}) {
100
+ const record = recordOf(root);
101
+
102
+ record[kind] = content;
103
+
104
+ if (kind === 'fallback' && extra.icons !== undefined) record.icons = extra.icons;
105
+
106
+ syncContext(root);
107
+ }
108
+
109
+ /**
110
+ * Forgets one style file (start of a build cycle).
111
+ *
112
+ * @param {string} root plugin root
113
+ * @param {'fallback' | 'own'} kind
114
+ */
115
+ export function clearStyleFile(root, kind) {
116
+ const record = recordOf(root);
117
+
118
+ delete record[kind];
119
+
120
+ if (kind === 'fallback') delete record.icons;
121
+
122
+ syncContext(root);
123
+ }
124
+
125
+ /** @param {string} id @returns {string} */
126
+ const withoutQuery = (id) => id.split('?')[0].replace(/\\/g, '/');
127
+
128
+ /**
129
+ * The CSS of one component, compiled the way rollup-plugin-svelte compiles it.
130
+ *
131
+ * @param {string} source
132
+ * @param {string} file absolute path
133
+ * @param {string} ns
134
+ * @returns {string | null} null when the component has no CSS or cannot be compiled (the real compile reports it)
135
+ */
136
+ export function componentCss(source, file, ns) {
137
+ if (!/<style[\s>]/.test(source)) return null;
138
+
139
+ let code = source;
140
+
141
+ if (!file.includes('/node_modules/')) {
142
+ try {
143
+ // The build adds the root class to the markup before the compile; a selector on it must survive.
144
+ code = addRootClass(source, { ns, view: path.basename(file, '.svelte') }).code;
145
+ } catch {
146
+ code = source;
147
+ }
148
+ }
149
+
150
+ try {
151
+ const compiled = compile(code, {
152
+ filename: path.relative(process.cwd(), file),
153
+ generate: 'client',
154
+ css: 'external',
155
+ });
156
+ const css = compiled.css?.code?.trim();
157
+
158
+ return css ? css : null;
159
+ } catch {
160
+ return null;
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Wraps component sheets into the `pano-plugin` layer.
166
+ *
167
+ * @param {[string, string][]} entries [relative path, css] in output order
168
+ * @returns {string} empty when there is nothing
169
+ */
170
+ export function layerOwnCss(entries) {
171
+ const parts = entries.filter(([, css]) => css.trim()).map(([name, css]) => `/* ${name} */\n${css.trim()}`);
172
+
173
+ if (parts.length === 0) return '';
174
+
175
+ return `@layer pano-plugin {\n${parts.join('\n\n')}\n}\n`;
176
+ }
177
+
178
+ /**
179
+ * Client-build plugin: collects the component CSS and emits `plugin.css` (doc 03 section 3.1).
180
+ *
181
+ * @param {{ ns: string, root?: string, outDir?: string, side?: string }} options
182
+ * @returns {import('rollup').Plugin}
183
+ */
184
+ export function panoPluginCss(options) {
185
+ if (options?.side !== undefined && options.side !== 'client') return { name: 'pano-plugin-css' };
186
+
187
+ const root = path.resolve(options?.root ?? process.cwd());
188
+ const ns = options?.ns ?? 'plugin';
189
+ /** @type {Map<string, string>} svelte file (absolute, slashes) -> css */
190
+ const components = new Map();
191
+
192
+ return {
193
+ name: 'pano-plugin-css',
194
+
195
+ buildStart() {
196
+ components.clear();
197
+ clearStyleFile(root, 'own');
198
+ },
199
+
200
+ transform(code, id) {
201
+ const file = withoutQuery(id);
202
+
203
+ if (file.endsWith('.svelte')) {
204
+ const css = componentCss(code, file, ns);
205
+
206
+ if (css) components.set(file, css);
207
+
208
+ return null;
209
+ }
210
+
211
+ // emitCss: true -> rollup-plugin-svelte's virtual `<Component>.css` module
212
+ if (file.endsWith('.css')) {
213
+ const sibling = file.replace(/\.css$/, '.svelte');
214
+
215
+ if (fs.existsSync(sibling) && !fs.existsSync(file)) {
216
+ if (code.trim()) components.set(sibling, code);
217
+
218
+ return { code: '', map: null };
219
+ }
220
+ }
221
+
222
+ return null;
223
+ },
224
+
225
+ generateBundle() {
226
+ const entries = [...components.entries()]
227
+ .map(([file, css]) => [path.relative(root, file).replace(/\\/g, '/'), css])
228
+ .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
229
+ const source = layerOwnCss(/** @type {[string, string][]} */ (entries));
230
+
231
+ if (!source) return;
232
+
233
+ this.emitFile({ type: 'asset', fileName: 'plugin.css', source });
234
+ recordStyleFile(root, 'own', source);
235
+ },
236
+ };
237
+ }
@@ -0,0 +1,206 @@
1
+ // Small source scanners shared by check-static.js, check-locales.mjs and check-theme.js.
2
+ // No AST: the rules are deliberately simple and each one has a unit test in scan.test.js.
3
+
4
+ const blank = (m) => m.replace(/[^\n]/g, ' ');
5
+
6
+ export function splitSvelte(source) {
7
+ const scripts = [];
8
+ let markup = source.replace(/<script\b([^>]*)>([\s\S]*?)<\/script>/g, (m, attrs, code, off) => {
9
+ scripts.push({
10
+ attrs,
11
+ code,
12
+ offset: off + m.indexOf('>') + 1,
13
+ module: /\b(module|context\s*=\s*["']module["'])/.test(attrs),
14
+ });
15
+ return blank(m);
16
+ });
17
+ const styles = [];
18
+ markup = markup.replace(/<style\b[^>]*>[\s\S]*?<\/style>/g, (m, off) => {
19
+ styles.push({ index: off });
20
+ return blank(m);
21
+ });
22
+ markup = markup.replace(/<!--[\s\S]*?-->/g, blank);
23
+ return { markup, scripts, styles };
24
+ }
25
+
26
+ // Opening tags of markup: [{ name, attrs, index }]; honours quotes and {braces} in attributes.
27
+ export function parseTags(markup) {
28
+ const tags = [];
29
+ let i = 0;
30
+ while (i < markup.length) {
31
+ if (markup[i] === '{') {
32
+ i = skipBraces(markup, i);
33
+ continue;
34
+ }
35
+ if (markup[i] === '<' && /[A-Za-z]/.test(markup[i + 1] ?? '')) {
36
+ const m = /^<([A-Za-z][\w.:-]*)/.exec(markup.slice(i, i + 80));
37
+ const name = m[1];
38
+ let j = i + m[0].length;
39
+ let quote = '';
40
+ while (j < markup.length) {
41
+ const c = markup[j];
42
+ if (quote) {
43
+ if (c === quote) quote = '';
44
+ } else if (c === '"' || c === "'") quote = c;
45
+ else if (c === '{') {
46
+ j = skipBraces(markup, j);
47
+ continue;
48
+ } else if (c === '>') break;
49
+ j++;
50
+ }
51
+ tags.push({ name, attrs: markup.slice(i + m[0].length, j), index: i });
52
+ i = j + 1;
53
+ continue;
54
+ }
55
+ i++;
56
+ }
57
+ return tags;
58
+ }
59
+
60
+ function skipBraces(s, start) {
61
+ let depth = 0;
62
+ let quote = '';
63
+ for (let i = start; i < s.length; i++) {
64
+ const c = s[i];
65
+ if (quote) {
66
+ if (c === '\\') i++;
67
+ else if (c === quote) quote = '';
68
+ continue;
69
+ }
70
+ if (c === '"' || c === "'" || c === '`') quote = c;
71
+ else if (c === '{') depth++;
72
+ else if (c === '}' && --depth === 0) return i + 1;
73
+ }
74
+ return s.length;
75
+ }
76
+
77
+ // Text nodes with letters that sit outside {...} and outside tags.
78
+ export function hardcodedTexts(markup) {
79
+ const found = [];
80
+ let i = 0;
81
+ let textStart = 0;
82
+ const flush = (end) => {
83
+ const text = markup.slice(textStart, end).replace(/&[a-zA-Z#0-9]+;/g, ' ');
84
+ if (/\p{L}/u.test(text)) found.push({ index: textStart, text: text.trim().slice(0, 60) });
85
+ };
86
+ while (i < markup.length) {
87
+ const c = markup[i];
88
+ if (c === '{') {
89
+ flush(i);
90
+ i = skipBraces(markup, i);
91
+ textStart = i;
92
+ } else if (c === '<' && /[A-Za-z\/!]/.test(markup[i + 1] ?? '')) {
93
+ flush(i);
94
+ let quote = '';
95
+ let j = i + 1;
96
+ while (j < markup.length) {
97
+ const d = markup[j];
98
+ if (quote) {
99
+ if (d === quote) quote = '';
100
+ } else if (d === '"' || d === "'") quote = d;
101
+ else if (d === '{') {
102
+ j = skipBraces(markup, j);
103
+ continue;
104
+ } else if (d === '>') break;
105
+ j++;
106
+ }
107
+ i = j + 1;
108
+ textStart = i;
109
+ } else i++;
110
+ }
111
+ flush(markup.length);
112
+ return found;
113
+ }
114
+
115
+ // {@html expr} occurrences: [{ expr, index }]
116
+ export function htmlSinks(markup) {
117
+ const out = [];
118
+ const re = /\{@html\s/g;
119
+ let m;
120
+ while ((m = re.exec(markup))) {
121
+ const end = skipBraces(markup, m.index);
122
+ out.push({ expr: markup.slice(m.index + m[0].length, end - 1).trim(), index: m.index });
123
+ }
124
+ return out;
125
+ }
126
+
127
+ // Replace comments and string contents so brace/keyword scans are not fooled.
128
+ function maskJs(code) {
129
+ let out = '';
130
+ let i = 0;
131
+ while (i < code.length) {
132
+ const c = code[i];
133
+ const n = code[i + 1];
134
+ if (c === '/' && n === '/') {
135
+ while (i < code.length && code[i] !== '\n') ((out += ' '), i++);
136
+ } else if (c === '/' && n === '*') {
137
+ const end = code.indexOf('*/', i + 2);
138
+ const stop = end === -1 ? code.length : end + 2;
139
+ out += blank(code.slice(i, stop));
140
+ i = stop;
141
+ } else if (c === '"' || c === "'" || c === '`') {
142
+ out += c;
143
+ i++;
144
+ while (i < code.length && code[i] !== c) {
145
+ if (code[i] === '\\') ((out += ' '), i++);
146
+ out += code[i] === '\n' ? '\n' : ' ';
147
+ i++;
148
+ }
149
+ out += c;
150
+ i++;
151
+ } else {
152
+ out += c;
153
+ i++;
154
+ }
155
+ }
156
+ return out;
157
+ }
158
+
159
+ // Browser-global reads at module top level: brace depth 0 and no arrow / function in the
160
+ // statement before the identifier. Returns [{ name, index }]. `names` e.g. ['localStorage'].
161
+ export function topLevelGlobals(code, names) {
162
+ const masked = maskJs(code);
163
+ const hits = [];
164
+ const re = new RegExp(`(?<![\\w$.])(${names.join('|')})\\b(?!\\s*:)`, 'g');
165
+ let m;
166
+ while ((m = re.exec(masked))) {
167
+ let depth = 0;
168
+ for (let k = 0; k < m.index; k++) {
169
+ if (masked[k] === '{') depth++;
170
+ else if (masked[k] === '}') depth--;
171
+ }
172
+ if (depth !== 0) continue;
173
+ let s = m.index;
174
+ while (s > 0 && masked[s - 1] !== ';' && masked[s - 1] !== '}' && masked[s - 1] !== '{') s--;
175
+ const stmt = masked.slice(s, m.index);
176
+ if (/=>|\bfunction\b/.test(stmt)) continue;
177
+ // import specifiers and object keys are not reads
178
+ if (/^\s*import\b/.test(stmt)) continue;
179
+ hits.push({ name: m[1], index: m.index });
180
+ }
181
+ return hits;
182
+ }
183
+
184
+ // Static class tokens of a tag's attributes: class="a b {x}" and string literals in class={...}.
185
+ export function classTokens(attrs) {
186
+ const tokens = [];
187
+ const re = /(?:^|\s)class(?::[\w-]+)?\s*=\s*(?:"([^"]*)"|'([^']*)'|\{([\s\S]*?)\}(?=\s|$|\/))/g;
188
+ let m;
189
+ while ((m = re.exec(attrs))) {
190
+ if (m[1] !== undefined || m[2] !== undefined) {
191
+ const val = (m[1] ?? m[2]).replace(/\{[^}]*\}/g, ' ');
192
+ tokens.push(...val.split(/\s+/).filter(Boolean));
193
+ } else {
194
+ for (const s of m[3].matchAll(/(["'`])((?:\\.|(?!\1).)*)\1/g))
195
+ tokens.push(
196
+ ...s[2]
197
+ .replace(/\$\{[^}]*\}/g, ' ')
198
+ .split(/\s+/)
199
+ .filter(Boolean),
200
+ );
201
+ }
202
+ }
203
+ const dirs = attrs.matchAll(/(?:^|\s)class:([\w-]+)/g);
204
+ for (const d of dirs) tokens.push(d[1]);
205
+ return tokens;
206
+ }