@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,422 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { rollup } from 'rollup';
|
|
5
|
+
import { RUNTIME_FILES, widgetImportPath } from '@panomc/widget-host/runtime-map';
|
|
6
|
+
import { SPECIFIER_MODULES } from '@panomc/widget-host';
|
|
7
|
+
import { panoPlugin } from '../../index.js';
|
|
8
|
+
import {
|
|
9
|
+
addPartAttributes,
|
|
10
|
+
collectWidgets,
|
|
11
|
+
controllersUsedBy,
|
|
12
|
+
defaultTagOf,
|
|
13
|
+
hasWidgetViews,
|
|
14
|
+
readLoad,
|
|
15
|
+
sessionControllers,
|
|
16
|
+
} from '../../widgets.js';
|
|
17
|
+
import { resolveProject } from '../../rules.js';
|
|
18
|
+
import {
|
|
19
|
+
buildError,
|
|
20
|
+
buildWidgets,
|
|
21
|
+
cleanup,
|
|
22
|
+
copyFixture,
|
|
23
|
+
filesOf,
|
|
24
|
+
fixture,
|
|
25
|
+
pluginMin,
|
|
26
|
+
read,
|
|
27
|
+
sdkDir,
|
|
28
|
+
sides,
|
|
29
|
+
tempDir,
|
|
30
|
+
write,
|
|
31
|
+
} from './helpers.js';
|
|
32
|
+
|
|
33
|
+
/** @type {string | undefined} */
|
|
34
|
+
let previousSdkDir;
|
|
35
|
+
|
|
36
|
+
beforeAll(() => {
|
|
37
|
+
previousSdkDir = process.env.PANO_SDK_DIR;
|
|
38
|
+
process.env.PANO_SDK_DIR = sdkDir;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(() => {
|
|
42
|
+
if (previousSdkDir === undefined) delete process.env.PANO_SDK_DIR;
|
|
43
|
+
else process.env.PANO_SDK_DIR = previousSdkDir;
|
|
44
|
+
|
|
45
|
+
cleanup();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const installedSvelte = JSON.parse(fs.readFileSync(path.join(sdkDir, 'node_modules/svelte/package.json'), 'utf8')).version;
|
|
49
|
+
|
|
50
|
+
describe('the widgets build of the fixture plugin', () => {
|
|
51
|
+
/** @type {Awaited<ReturnType<typeof buildWidgets>>} */
|
|
52
|
+
let built;
|
|
53
|
+
/** @type {Record<string, any>} */
|
|
54
|
+
let index;
|
|
55
|
+
|
|
56
|
+
beforeAll(async () => {
|
|
57
|
+
built = await buildWidgets(fixture);
|
|
58
|
+
index = JSON.parse(built.files['widgets.json']);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('two widget views build: one module each, one shared chunk, widgets.json', () => {
|
|
62
|
+
const names = Object.keys(built.files).sort();
|
|
63
|
+
|
|
64
|
+
expect(names.filter((name) => /^GoalWidget-[\w-]+\.js$/.test(name))).toHaveLength(1);
|
|
65
|
+
expect(names.filter((name) => /^StatsWidget-[\w-]+\.js$/.test(name))).toHaveLength(1);
|
|
66
|
+
expect(names.filter((name) => /^chunk-[\w-]+\.js$/.test(name))).toHaveLength(1);
|
|
67
|
+
expect(names).toContain('widgets.json');
|
|
68
|
+
expect(names).toHaveLength(4);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('widgets.json: format 1, the svelte pin, one record per widget', () => {
|
|
72
|
+
expect(index.format).toBe(1);
|
|
73
|
+
expect(index.svelte).toBe(installedSvelte);
|
|
74
|
+
expect(index.widgets.map((widget) => widget.view)).toEqual(['GoalWidget', 'StatsWidget']);
|
|
75
|
+
|
|
76
|
+
const [goal, stats] = index.widgets;
|
|
77
|
+
|
|
78
|
+
expect(goal).toEqual({
|
|
79
|
+
view: 'GoalWidget',
|
|
80
|
+
tag: 'goal',
|
|
81
|
+
module: Object.keys(built.files).find((name) => name.startsWith('GoalWidget-')),
|
|
82
|
+
attrs: [{ name: 'title', attribute: 'title', type: 'string' }],
|
|
83
|
+
session: 'none',
|
|
84
|
+
});
|
|
85
|
+
expect(stats.tag).toBe('numbers');
|
|
86
|
+
expect(stats.module).toMatch(/^StatsWidget-[\w-]+\.js$/);
|
|
87
|
+
// primitive props only, kebab-cased attribute names; the object prop `stats` comes from load()
|
|
88
|
+
expect(stats.attrs).toEqual([
|
|
89
|
+
{ name: 'limit', attribute: 'limit', type: 'number' },
|
|
90
|
+
{ name: 'compact', attribute: 'compact', type: 'boolean' },
|
|
91
|
+
{ name: 'goalId', attribute: 'goal-id', type: 'number' },
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('session is inferred from the controllers of the view (host.session -> optional)', () => {
|
|
96
|
+
expect(index.widgets[0].session).toBe('none');
|
|
97
|
+
expect(index.widgets[1].session).toBe('optional');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('a module exports { component, load, attrs, session }', async () => {
|
|
101
|
+
const config = (await sides(fixture, tempDir(), ['widgets']))[0];
|
|
102
|
+
const bundle = await rollup(config);
|
|
103
|
+
const { output } = await bundle.generate(/** @type {any} */ (config.output));
|
|
104
|
+
|
|
105
|
+
await bundle.close();
|
|
106
|
+
|
|
107
|
+
const entries = output.filter((chunk) => chunk.type === 'chunk' && chunk.isEntry);
|
|
108
|
+
|
|
109
|
+
expect(entries).toHaveLength(2);
|
|
110
|
+
|
|
111
|
+
for (const entry of entries) expect([...entry.exports].sort()).toEqual(['attrs', 'component', 'load', 'session']);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('host specifiers become the relative runtime URLs of doc 06 section 3.3', () => {
|
|
115
|
+
const goal = built.files[Object.keys(built.files).find((name) => name.startsWith('GoalWidget-'))];
|
|
116
|
+
const prefix = '../../../../../widgets/runtime/';
|
|
117
|
+
|
|
118
|
+
expect(widgetImportPath('@panomc/sdk/toasts')).toBe(`${prefix}host/toasts.js`);
|
|
119
|
+
expect(goal).toContain(`'${prefix}svelte/internal-client.js'`);
|
|
120
|
+
expect(goal).toContain(`'${prefix}host/toasts.js'`);
|
|
121
|
+
expect(goal).toContain(`'${prefix}host/utils-language.js'`);
|
|
122
|
+
expect(built.files[Object.keys(built.files).find((name) => name.startsWith('StatsWidget-'))]).toContain(
|
|
123
|
+
`'${prefix}host/controllers.js'`,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
for (const [name, code] of Object.entries(built.files)) {
|
|
127
|
+
if (!name.endsWith('.js')) continue;
|
|
128
|
+
|
|
129
|
+
for (const match of code.matchAll(/(?:from|import)\s*['"]([^'"]+)['"]/g)) {
|
|
130
|
+
expect(match[1].startsWith('.')).toBe(true); // nothing bare is left: no import map needed
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('the part step copies semantic classes to part attributes in the widget bundle', () => {
|
|
136
|
+
const goal = built.files[Object.keys(built.files).find((name) => name.startsWith('GoalWidget-'))];
|
|
137
|
+
|
|
138
|
+
expect(goal).toMatch(/part="[^"]*goals-goal-widget__bar[^"]*"/);
|
|
139
|
+
expect(goal).toMatch(/part="[^"]*goals-goal-widget__title[^"]*"/);
|
|
140
|
+
// the root class of the view is a part too
|
|
141
|
+
expect(goal).toMatch(/part="[^"]*goals-goal-widget goals-goal-widget__card|part="goals-goal-widget /);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('a minified build keeps the relative runtime URLs and the exports', async () => {
|
|
145
|
+
const minified = await buildWidgets(fixture, { minify: true });
|
|
146
|
+
const goal = minified.files[Object.keys(minified.files).find((name) => name.startsWith('GoalWidget-'))];
|
|
147
|
+
|
|
148
|
+
expect(goal).toContain('../../../../../widgets/runtime/host/toasts.js');
|
|
149
|
+
expect(goal).toMatch(/export\s*\{[^}]*\bcomponent\b/);
|
|
150
|
+
expect(JSON.parse(minified.files['widgets.json']).widgets).toHaveLength(2);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('widgets.json and the modules are deterministic', async () => {
|
|
154
|
+
const again = await buildWidgets(fixture);
|
|
155
|
+
|
|
156
|
+
expect(again.files).toEqual(built.files);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('the target exists only when a view has widget', () => {
|
|
161
|
+
test('no widget view: no widgets/ directory, nothing written', async () => {
|
|
162
|
+
const { out, files } = await buildWidgets(pluginMin);
|
|
163
|
+
|
|
164
|
+
expect(files).toEqual({});
|
|
165
|
+
expect(fs.existsSync(path.join(out, 'widgets'))).toBe(false);
|
|
166
|
+
expect(fs.readdirSync(out)).toEqual([]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('no widget view: a stale widgets/ directory is removed', async () => {
|
|
170
|
+
const out = tempDir();
|
|
171
|
+
|
|
172
|
+
fs.mkdirSync(path.join(out, 'widgets'));
|
|
173
|
+
fs.writeFileSync(path.join(out, 'widgets/old.js'), '//');
|
|
174
|
+
|
|
175
|
+
await write(await sides(pluginMin, out, ['widgets']));
|
|
176
|
+
|
|
177
|
+
expect(fs.existsSync(path.join(out, 'widgets'))).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('hasWidgetViews tells the preset whether to build it', async () => {
|
|
181
|
+
expect(await hasWidgetViews({ pluginId: 'pano-plugin-goals', root: fixture })).toBe(true);
|
|
182
|
+
expect(await hasWidgetViews({ pluginId: 'pano-plugin-min', root: pluginMin })).toBe(false);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('the client bundle is byte-identical with or without the widgets target', async () => {
|
|
186
|
+
for (const root of [fixture, pluginMin]) {
|
|
187
|
+
const alone = tempDir();
|
|
188
|
+
const together = tempDir();
|
|
189
|
+
|
|
190
|
+
await write(await sides(root, alone, ['server', 'client']));
|
|
191
|
+
await write(await sides(root, together, ['server', 'client', 'widgets']));
|
|
192
|
+
|
|
193
|
+
const before = filesOf(path.join(alone, 'client'));
|
|
194
|
+
const after = filesOf(path.join(together, 'client'));
|
|
195
|
+
|
|
196
|
+
expect(Object.keys(before).length).toBeGreaterThan(0);
|
|
197
|
+
expect(after).toEqual(before);
|
|
198
|
+
expect(filesOf(path.join(together, 'server'))).toEqual(filesOf(path.join(alone, 'server')));
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('the client bundle never carries part attributes', async () => {
|
|
203
|
+
const out = tempDir();
|
|
204
|
+
|
|
205
|
+
await write(await sides(fixture, out, ['client']));
|
|
206
|
+
|
|
207
|
+
for (const code of Object.values(filesOf(path.join(out, 'client')))) expect(code).not.toMatch(/part="/);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('badges.widgets is true after the widgets build, false without widgets', async () => {
|
|
211
|
+
const withWidgets = tempDir();
|
|
212
|
+
|
|
213
|
+
await write(await sides(fixture, withWidgets, ['server', 'client', 'widgets']));
|
|
214
|
+
expect(JSON.parse(read(withWidgets, 'pano-plugin.json')).badges.widgets).toBe(true);
|
|
215
|
+
|
|
216
|
+
const withoutWidgets = tempDir();
|
|
217
|
+
|
|
218
|
+
await write(await sides(pluginMin, withoutWidgets, ['server', 'client', 'widgets']));
|
|
219
|
+
expect(JSON.parse(read(withoutWidgets, 'pano-plugin.json')).badges.widgets).toBe(false);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('badges.widgets is true when the client build runs after the widgets build', async () => {
|
|
223
|
+
const out = tempDir();
|
|
224
|
+
|
|
225
|
+
await write(await sides(fixture, out, ['widgets', 'server', 'client']));
|
|
226
|
+
expect(JSON.parse(read(out, 'pano-plugin.json')).badges.widgets).toBe(true);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('a view that has no widget key is not built even next to widget views', async () => {
|
|
230
|
+
const { files } = await buildWidgets(fixture);
|
|
231
|
+
|
|
232
|
+
expect(Object.keys(files).some((name) => name.startsWith('GoalsPage'))).toBe(false);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe('build errors', () => {
|
|
237
|
+
test('an import with no widget implementation names the file, the import and the view', async () => {
|
|
238
|
+
const root = copyFixture({
|
|
239
|
+
'src/theme/views/GoalWidget.svelte': read(fixture, 'src/theme/views/GoalWidget.svelte').replace(
|
|
240
|
+
"import { percent }",
|
|
241
|
+
"import { Thing } from '@panomc/sdk/not-there';\n import { percent }",
|
|
242
|
+
),
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(await buildError(root)).toContain(
|
|
246
|
+
'src/theme/views/GoalWidget.svelte imports @panomc/sdk/not-there, which has no widget implementation. Remove GoalWidget from widgets or avoid the import.',
|
|
247
|
+
);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test('an unsupported import in a shared helper names every widget view that reaches it', async () => {
|
|
251
|
+
const root = copyFixture({
|
|
252
|
+
'src/theme/lib/percent.js': `import { x } from 'svelte/compiler';\n${read(fixture, 'src/theme/lib/percent.js')}`,
|
|
253
|
+
});
|
|
254
|
+
const message = await buildError(root);
|
|
255
|
+
|
|
256
|
+
expect(message).toContain('src/theme/lib/percent.js imports svelte/compiler, which has no widget implementation.');
|
|
257
|
+
expect(message).toContain('Remove GoalWidget, StatsWidget from widgets or avoid the import.');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test('a required object prop that load() does not return', async () => {
|
|
261
|
+
const goal = read(fixture, 'src/theme/views/GoalWidget.svelte');
|
|
262
|
+
const withoutLoad = copyFixture({
|
|
263
|
+
'src/theme/views/GoalWidget.svelte': goal.replace(/\n export const load =.*\n/, '\n'),
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
expect(await buildError(withoutLoad)).toContain(
|
|
267
|
+
'GoalWidget needs prop "data": return it from load() or remove widget from its view metadata',
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
const wrongKey = copyFixture({
|
|
271
|
+
'src/theme/views/GoalWidget.svelte': goal.replace('({ data })', '({ other: data })'),
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
expect(await buildError(wrongKey)).toContain('GoalWidget needs prop "data"');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test('a load() the build cannot read is trusted', async () => {
|
|
278
|
+
const goal = read(fixture, 'src/theme/views/GoalWidget.svelte');
|
|
279
|
+
const root = copyFixture({
|
|
280
|
+
'src/theme/views/GoalWidget.svelte': goal.replace(/export const load =.*\n/, 'export const load = (event) => fetchSomething(event);\n'),
|
|
281
|
+
});
|
|
282
|
+
const { files } = await buildWidgets(root);
|
|
283
|
+
|
|
284
|
+
expect(Object.keys(files).some((name) => name.startsWith('GoalWidget-'))).toBe(true);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('two views that make one tag name both', async () => {
|
|
288
|
+
const root = copyFixture({
|
|
289
|
+
'src/theme/views/GoalBlock.svelte': `<script module>\n export const view = { widget: true };\n</script>\n\n<p>block</p>\n`,
|
|
290
|
+
});
|
|
291
|
+
const message = await buildError(root);
|
|
292
|
+
|
|
293
|
+
expect(message).toContain('GoalBlock and GoalWidget both make the tag <pano-goals-goal>');
|
|
294
|
+
expect(message).toContain('src/theme/views/GoalWidget.svelte');
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test('widget metadata that cannot be read', async () => {
|
|
298
|
+
const root = copyFixture({
|
|
299
|
+
'src/theme/views/StatsWidget.svelte': read(fixture, 'src/theme/views/StatsWidget.svelte').replace(
|
|
300
|
+
"{ widget: { tag: 'numbers' } }",
|
|
301
|
+
"{ widget: { tag: 'Bad Tag' } }",
|
|
302
|
+
),
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
expect(await buildError(root)).toContain('view.widget.tag must be lowercase words joined by "-"');
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
describe('session', () => {
|
|
310
|
+
test('the author can set it', async () => {
|
|
311
|
+
const root = copyFixture({
|
|
312
|
+
'src/theme/views/GoalWidget.svelte': read(fixture, 'src/theme/views/GoalWidget.svelte').replace(
|
|
313
|
+
'widget: true',
|
|
314
|
+
"widget: { session: 'required' }",
|
|
315
|
+
),
|
|
316
|
+
});
|
|
317
|
+
const { files } = await buildWidgets(root);
|
|
318
|
+
const index = JSON.parse(files['widgets.json']);
|
|
319
|
+
|
|
320
|
+
expect(index.widgets.find((widget) => widget.view === 'GoalWidget')).toMatchObject({ tag: 'goal', session: 'required' });
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('a controller that calls host.session, directly or through a sibling', async () => {
|
|
324
|
+
const project = await resolveProject({ root: fixture });
|
|
325
|
+
|
|
326
|
+
expect([...sessionControllers(project)]).toEqual(['stats']);
|
|
327
|
+
|
|
328
|
+
const root = copyFixture({
|
|
329
|
+
'src/theme/controllers/wrapper.js': `import { defineController } from '@panomc/plugin-kit/controller';\nexport default defineController({ name: 'wrapper', version: 1, state: () => ({}), actions: ({ use }) => ({ go() { use('stats').actions.refresh(); } }) });\n`,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect([...sessionControllers(await resolveProject({ root }))].sort()).toEqual(['stats', 'wrapper']);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('controllersUsedBy reads literal names in the view and its relative imports', () => {
|
|
336
|
+
const stats = path.join(fixture, 'src/theme/views/StatsWidget.svelte');
|
|
337
|
+
|
|
338
|
+
expect([...controllersUsedBy(stats, 'goals', fixture)]).toEqual(['stats']);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe('parts', () => {
|
|
343
|
+
test('the root class and every semantic class become part names', () => {
|
|
344
|
+
const { code, parts } = addPartAttributes(
|
|
345
|
+
'<div class="card min-box__body">\n <p class="min-box__text is-open">x</p>\n <span class="plain">y</span>\n</div>\n',
|
|
346
|
+
{ ns: 'min', view: 'Box' },
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
expect(parts).toBe(2);
|
|
350
|
+
expect(code).toContain('<div part="min-box min-box__body" class="min-box card min-box__body">');
|
|
351
|
+
expect(code).toContain('<p part="min-box__text" class="min-box__text is-open">');
|
|
352
|
+
expect(code).not.toContain('<span part');
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test('literal classes of expressions and class: directives count, an existing part stays', () => {
|
|
356
|
+
const { code } = addPartAttributes(
|
|
357
|
+
'<div class={[\'min-box__a\', cond && \'min-box__b\']} class:min-box__c={on}>x</div>\n<i part="mine" class="min-box__d"></i>\n',
|
|
358
|
+
{ ns: 'min', view: 'Box' },
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
expect(code).toContain('part="min-box min-box__a min-box__b min-box__c"');
|
|
362
|
+
expect(code).toContain('<i part="mine"');
|
|
363
|
+
expect(code.match(/part=/g)).toHaveLength(2);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test('the step is idempotent and leaves a source without elements alone', () => {
|
|
367
|
+
const once = addPartAttributes('<div class="min-box__a">x</div>', { ns: 'min', view: 'Box' }).code;
|
|
368
|
+
|
|
369
|
+
expect(addPartAttributes(once, { ns: 'min', view: 'Box' }).code).toBe(once);
|
|
370
|
+
expect(addPartAttributes('<script>let a = 1;</script>', { ns: 'min', view: 'Box' })).toEqual({
|
|
371
|
+
code: '<script>let a = 1;</script>',
|
|
372
|
+
parts: 0,
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
describe('reading a view', () => {
|
|
378
|
+
test('readLoad: the keys a load() returns', () => {
|
|
379
|
+
const shape = (script) => readLoad(`<script module>\n${script}\n</script>\n`);
|
|
380
|
+
|
|
381
|
+
expect(shape('export const load = () => ({ a: 1, b })').shape).toEqual({ keys: new Set(['a', 'b']), open: false });
|
|
382
|
+
expect(shape('export async function load() { if (x) return { a }; return { b }; }').shape.keys).toEqual(new Set(['a', 'b']));
|
|
383
|
+
expect(shape('export const load = () => fetch(x).then((r) => ({ r }))').shape).toEqual({ keys: new Set(['r']), open: false });
|
|
384
|
+
expect(shape('export const load = () => ({ ...base, a })').shape.open).toBe(true);
|
|
385
|
+
expect(shape('function go() { return { z: 1 }; }\nexport { go as load };').shape.keys).toEqual(new Set(['z']));
|
|
386
|
+
expect(shape('export const view = {};').hasLoad).toBe(false);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test('defaultTagOf strips Widget / Block', () => {
|
|
390
|
+
expect(defaultTagOf('GoalWidget')).toBe('goal');
|
|
391
|
+
expect(defaultTagOf('RecentBuyersWidget')).toBe('recent-buyers');
|
|
392
|
+
expect(defaultTagOf('NavCart')).toBe('nav-cart');
|
|
393
|
+
expect(defaultTagOf('TopBlock')).toBe('top');
|
|
394
|
+
expect(defaultTagOf('Widget')).toBe('widget');
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test('collectWidgets reports the tag as the element name', async () => {
|
|
398
|
+
const { scanViews } = await import('../../views.js');
|
|
399
|
+
const scan = await scanViews({ dirs: ['src/theme/views'], namespace: 'goals', pluginId: 'pano-plugin-goals', root: fixture });
|
|
400
|
+
|
|
401
|
+
expect(collectWidgets({ scan, namespace: 'goals' }).map((widget) => widget.element)).toEqual([
|
|
402
|
+
'pano-goals-goal',
|
|
403
|
+
'pano-goals-numbers',
|
|
404
|
+
]);
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
describe('the runtime table', () => {
|
|
409
|
+
test('every SDK facade of the widget host has a runtime file, and the preset aliases exactly those', () => {
|
|
410
|
+
for (const specifier of Object.keys(SPECIFIER_MODULES)) expect(RUNTIME_FILES[specifier]).toBeTruthy();
|
|
411
|
+
|
|
412
|
+
expect(Object.keys(RUNTIME_FILES).filter((specifier) => specifier.startsWith('@panomc/sdk')).sort()).toEqual(
|
|
413
|
+
Object.keys(SPECIFIER_MODULES).sort(),
|
|
414
|
+
);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
test('panoPlugin() hands the widgets side to panoWidgets', async () => {
|
|
418
|
+
const [config] = await panoPlugin({ root: fixture, outDir: tempDir(), side: 'widgets' });
|
|
419
|
+
|
|
420
|
+
expect(config.plugins.some((plugin) => plugin?.name === 'pano-widgets')).toBe(true);
|
|
421
|
+
});
|
|
422
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `pano-plugin build` and the shared config loader of `dev`.
|
|
7
|
+
*
|
|
8
|
+
* The plugin's `rollup.config.js` (`export default panoPlugin();`) is imported and every config it
|
|
9
|
+
* returns is built and written, in order, as `rollup -c` would. A failed lock comparison, a build
|
|
10
|
+
* rule or a missing import ends the command with exit code 1 and the message of the failing plugin.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const CONFIG_FILE = "rollup.config.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} root
|
|
17
|
+
* @param {string} [file] config file, relative to `root`
|
|
18
|
+
* @returns {Promise<import('rollup').RollupOptions[]>}
|
|
19
|
+
*/
|
|
20
|
+
export async function loadRollupConfigs(root, file = CONFIG_FILE) {
|
|
21
|
+
const configFile = path.resolve(root, file);
|
|
22
|
+
|
|
23
|
+
if (!fs.existsSync(configFile)) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`no ${file} in ${root} — create it with: import { panoPlugin } from '@panomc/plugin-kit/rollup'; export default panoPlugin();`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let exported = (await import(pathToFileURL(configFile).href)).default;
|
|
30
|
+
|
|
31
|
+
if (typeof exported === "function") exported = exported({});
|
|
32
|
+
|
|
33
|
+
exported = await exported;
|
|
34
|
+
|
|
35
|
+
return Array.isArray(exported) ? exported : [exported];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {unknown} error
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
export function describeError(error) {
|
|
43
|
+
const e = /** @type {any} */ (error);
|
|
44
|
+
const where = e?.loc?.file ?? e?.id;
|
|
45
|
+
const position = e?.loc?.line ? `:${e.loc.line}` : "";
|
|
46
|
+
const message = String(e?.message ?? e);
|
|
47
|
+
|
|
48
|
+
// panoViews and the rules already start their messages with file:line
|
|
49
|
+
return where && !message.includes(path.basename(where)) ? `${message} (${where}${position})` : message;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {{ root?: string, config?: string, out?: (line: string) => void, err?: (line: string) => void }} [options]
|
|
54
|
+
* @returns {Promise<number>}
|
|
55
|
+
*/
|
|
56
|
+
export async function runBuild(options = {}) {
|
|
57
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
58
|
+
const out = options.out ?? ((line) => process.stdout.write(`${line}\n`));
|
|
59
|
+
const err = options.err ?? ((line) => process.stderr.write(`${line}\n`));
|
|
60
|
+
const { rollup } = await import("rollup");
|
|
61
|
+
/** @type {import('rollup').RollupBuild | null} */
|
|
62
|
+
let bundle = null;
|
|
63
|
+
let built = 0;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const configs = await loadRollupConfigs(root, options.config);
|
|
67
|
+
|
|
68
|
+
for (const config of configs) {
|
|
69
|
+
bundle = await rollup(config);
|
|
70
|
+
|
|
71
|
+
for (const output of [].concat(/** @type {any} */ (config.output ?? []))) {
|
|
72
|
+
await bundle.write(output);
|
|
73
|
+
built += 1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await bundle.close();
|
|
77
|
+
bundle = null;
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (bundle) await bundle.close().catch(() => {});
|
|
81
|
+
|
|
82
|
+
err(`error: ${describeError(error).replace(/^\[pano-plugin\] /, "")}`);
|
|
83
|
+
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
out(`build: ${built} bundle${built === 1 ? "" : "s"} written`);
|
|
88
|
+
|
|
89
|
+
return 0;
|
|
90
|
+
}
|