@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.
- 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 +46 -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 +72 -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,434 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { rollup } from "rollup";
|
|
5
|
+
import nodeResolve from "@rollup/plugin-node-resolve";
|
|
6
|
+
import terser from "@rollup/plugin-terser";
|
|
7
|
+
import { loadConfig } from "../config.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Standalone controllers (doc 02 section 2 and 5.1): the second build of a plugin.
|
|
11
|
+
*
|
|
12
|
+
* Every default export directly under `src/theme/controllers` (not `_*.js`, not `types.js`, no
|
|
13
|
+
* sub-folders) is bundled, together with the controller core of this kit and every npm package
|
|
14
|
+
* the controllers import, into ONE minified ES module with no import statement:
|
|
15
|
+
*
|
|
16
|
+
* <outDir>/controllers/controllers.mjs
|
|
17
|
+
* export const controllers name -> definition ({ cart: ControllerDefinition, ... })
|
|
18
|
+
* export const createHost = createFetchHost
|
|
19
|
+
* export function create(name, host, params?, initial?) -> Controller
|
|
20
|
+
* export const meta { format, pluginId, namespace, controllers: { name: { version, scope } } }
|
|
21
|
+
*
|
|
22
|
+
* `create` behaves like the registry of the theme: an `app` controller is one per host (the second
|
|
23
|
+
* call returns the first one, whatever `params` it gets), an `instance` controller is new on every
|
|
24
|
+
* call, `ctx.use(name)` reaches a sibling made on the same host.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const CORE_FILE = fileURLToPath(new URL("../controller/index.js", import.meta.url));
|
|
28
|
+
const ENTRY_ID = "pano:controllers-standalone";
|
|
29
|
+
const RESOLVED_ENTRY_ID = "\0" + ENTRY_ID;
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_CONTROLLERS_DIR = "src/theme/controllers";
|
|
32
|
+
export const DEFAULT_OUT_DIR = "src/main/resources/plugin-ui";
|
|
33
|
+
|
|
34
|
+
/** Specifiers a controller must never reach (rule V2, doc 02 section 5.4). */
|
|
35
|
+
const FORBIDDEN = /^(svelte(\/.*)?|svelte-i18n|@panomc\/sdk(\/.*)?|\$app\/.*|.*\.svelte)$/;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {object} ControllersOptions
|
|
39
|
+
* @property {string} [root] plugin root, default the current directory
|
|
40
|
+
* @property {string} [dir] controllers folder, default `src/theme/controllers`
|
|
41
|
+
* @property {string} [outDir] package folder, default `src/main/resources/plugin-ui`
|
|
42
|
+
* @property {string} [pluginId] default: `pluginId` of gradle.properties
|
|
43
|
+
* @property {string} [namespace] default: derived like the rest of the kit
|
|
44
|
+
* @property {import('rollup').Plugin[]} [plugins] extra plugins for the standalone build (the preset passes its rules here)
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {ControllersOptions} options
|
|
49
|
+
* @returns {{ root: string, dir: string, outDir: string }}
|
|
50
|
+
*/
|
|
51
|
+
export function resolvePaths(options = {}) {
|
|
52
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
root,
|
|
56
|
+
dir: path.resolve(root, options.dir ?? DEFAULT_CONTROLLERS_DIR),
|
|
57
|
+
outDir: path.resolve(root, options.outDir ?? DEFAULT_OUT_DIR),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The public controller files, sorted: `*.js` directly in the folder, without `_*.js` and `types.js`.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} dir absolute controllers folder
|
|
65
|
+
* @returns {string[]} absolute paths
|
|
66
|
+
*/
|
|
67
|
+
export function listControllerFiles(dir) {
|
|
68
|
+
if (!fs.existsSync(dir)) return [];
|
|
69
|
+
|
|
70
|
+
return fs
|
|
71
|
+
.readdirSync(dir, { withFileTypes: true })
|
|
72
|
+
.filter(
|
|
73
|
+
(entry) =>
|
|
74
|
+
entry.isFile() &&
|
|
75
|
+
entry.name.endsWith(".js") &&
|
|
76
|
+
!entry.name.startsWith("_") &&
|
|
77
|
+
entry.name !== "types.js",
|
|
78
|
+
)
|
|
79
|
+
.map((entry) => path.join(dir, entry.name))
|
|
80
|
+
.sort();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Every `.js` file under the folder that holds the controllers and the view helpers (`src/theme`),
|
|
85
|
+
* so watch mode sees helper edits too.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} dir absolute controllers folder
|
|
88
|
+
* @returns {string[]}
|
|
89
|
+
*/
|
|
90
|
+
export function themeSourceFiles(dir) {
|
|
91
|
+
const base = path.dirname(dir);
|
|
92
|
+
/** @type {string[]} */
|
|
93
|
+
const out = [];
|
|
94
|
+
|
|
95
|
+
const walk = (folder) => {
|
|
96
|
+
let entries;
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
entries = fs.readdirSync(folder, { withFileTypes: true });
|
|
100
|
+
} catch {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (entry.name === "node_modules") continue;
|
|
106
|
+
|
|
107
|
+
const full = path.join(folder, entry.name);
|
|
108
|
+
|
|
109
|
+
if (entry.isDirectory()) walk(full);
|
|
110
|
+
else if (entry.name.endsWith(".js")) out.push(full);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (fs.existsSync(base)) walk(base);
|
|
115
|
+
|
|
116
|
+
return out.sort();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Writes a file through a temporary name and a rename, and not at all when the content is the same.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} file
|
|
123
|
+
* @param {string} text
|
|
124
|
+
* @returns {boolean} true when the file was written
|
|
125
|
+
*/
|
|
126
|
+
export function writeIfChanged(file, text) {
|
|
127
|
+
if (fs.existsSync(file) && fs.readFileSync(file, "utf8") === text) return false;
|
|
128
|
+
|
|
129
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
130
|
+
|
|
131
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
132
|
+
|
|
133
|
+
fs.writeFileSync(temporary, text);
|
|
134
|
+
fs.renameSync(temporary, file);
|
|
135
|
+
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {string} root
|
|
141
|
+
* @param {string} file
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
function relative(root, file) {
|
|
145
|
+
return path.relative(root, file).split(path.sep).join("/");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Source of the virtual entry module of the standalone build.
|
|
150
|
+
*
|
|
151
|
+
* @param {{ files: string[], pluginId: string, namespace: string, root: string, dir: string }} input
|
|
152
|
+
* @returns {string}
|
|
153
|
+
*/
|
|
154
|
+
function entrySource({ files, pluginId, namespace, root, dir }) {
|
|
155
|
+
const imports = files
|
|
156
|
+
.map((file, index) => `import d${index} from ${JSON.stringify(file)};`)
|
|
157
|
+
.join("\n");
|
|
158
|
+
const list = files
|
|
159
|
+
.map((file, index) => `[${JSON.stringify(path.basename(file))}, d${index}]`)
|
|
160
|
+
.join(", ");
|
|
161
|
+
const header = JSON.stringify({ format: 1, pluginId, namespace });
|
|
162
|
+
|
|
163
|
+
return `import { createFetchHost, nullHost } from ${JSON.stringify(CORE_FILE)};
|
|
164
|
+
${imports}
|
|
165
|
+
|
|
166
|
+
const list = [${list}];
|
|
167
|
+
export const controllers = {};
|
|
168
|
+
for (const [file, definition] of list) {
|
|
169
|
+
if (!definition || typeof definition.create !== "function" || typeof definition.name !== "string") {
|
|
170
|
+
throw new Error("controllers/" + file + ": the default export must be defineController({ ... })");
|
|
171
|
+
}
|
|
172
|
+
if (controllers[definition.name]) {
|
|
173
|
+
throw new Error("controllers/" + file + ": the controller name '" + definition.name + "' is already used by another file");
|
|
174
|
+
}
|
|
175
|
+
controllers[definition.name] = definition;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const createHost = createFetchHost;
|
|
179
|
+
|
|
180
|
+
export const meta = Object.freeze({
|
|
181
|
+
...${header},
|
|
182
|
+
controllers: Object.fromEntries(
|
|
183
|
+
Object.entries(controllers).map(([name, definition]) => [name, { version: definition.version, scope: definition.scope }]),
|
|
184
|
+
),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const perHost = new WeakMap();
|
|
188
|
+
|
|
189
|
+
export function create(name, host, params, initial) {
|
|
190
|
+
const definition = Object.prototype.hasOwnProperty.call(controllers, name) ? controllers[name] : undefined;
|
|
191
|
+
if (!definition) {
|
|
192
|
+
throw new Error("controller '" + meta.namespace + "/" + name + "' is not in this package (available: " + (Object.keys(controllers).join(", ") || "none") + ")");
|
|
193
|
+
}
|
|
194
|
+
const theHost = host || nullHost;
|
|
195
|
+
const shared = definition.scope === "app";
|
|
196
|
+
let cache = null;
|
|
197
|
+
if (shared) {
|
|
198
|
+
cache = perHost.get(theHost);
|
|
199
|
+
if (!cache) perHost.set(theHost, (cache = new Map()));
|
|
200
|
+
const hit = cache.get(name);
|
|
201
|
+
if (hit) return hit;
|
|
202
|
+
}
|
|
203
|
+
const controller = definition.create(theHost, params, initial, {
|
|
204
|
+
namespace: meta.namespace,
|
|
205
|
+
use: (other) => create(other, theHost, {}),
|
|
206
|
+
});
|
|
207
|
+
if (shared) {
|
|
208
|
+
cache.set(name, controller);
|
|
209
|
+
const destroy = controller.destroy;
|
|
210
|
+
controller.destroy = () => {
|
|
211
|
+
if (cache.get(name) === controller) cache.delete(name);
|
|
212
|
+
destroy();
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return controller;
|
|
216
|
+
}
|
|
217
|
+
`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @param {Map<string, string>} parents resolved module id -> the module that first imported it
|
|
222
|
+
* @param {string} importer
|
|
223
|
+
* @param {string} themeRoot
|
|
224
|
+
* @returns {string} "controllers/cart.js -> stores/session.js"
|
|
225
|
+
*/
|
|
226
|
+
function importChain(parents, importer, themeRoot) {
|
|
227
|
+
/** @type {string[]} */
|
|
228
|
+
const ids = [];
|
|
229
|
+
const seen = new Set();
|
|
230
|
+
/** @type {string | undefined} */
|
|
231
|
+
let current = importer;
|
|
232
|
+
|
|
233
|
+
while (current && !seen.has(current) && !current.startsWith("\0")) {
|
|
234
|
+
seen.add(current);
|
|
235
|
+
ids.unshift(current);
|
|
236
|
+
current = parents.get(current);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return ids.map((id) => relative(themeRoot, id)).join(" -> ");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The plugin that keeps the standalone build closed: it provides the entry and the controller core,
|
|
244
|
+
* refuses framework imports (rule V2) and remembers who imported what for the error chain.
|
|
245
|
+
*
|
|
246
|
+
* @param {{ files: string[], pluginId: string, namespace: string, root: string, dir: string }} input
|
|
247
|
+
* @returns {import('rollup').Plugin}
|
|
248
|
+
*/
|
|
249
|
+
function standalonePlugin(input) {
|
|
250
|
+
const themeRoot = path.dirname(input.dir);
|
|
251
|
+
/** @type {Map<string, string>} */
|
|
252
|
+
const parents = new Map();
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
name: "pano-controllers-standalone",
|
|
256
|
+
async resolveId(source, importer, options) {
|
|
257
|
+
if (source === ENTRY_ID) return RESOLVED_ENTRY_ID;
|
|
258
|
+
if (source === "@panomc/plugin-kit/controller") return CORE_FILE;
|
|
259
|
+
if (!importer) return null;
|
|
260
|
+
|
|
261
|
+
if (FORBIDDEN.test(source)) {
|
|
262
|
+
this.error(
|
|
263
|
+
`${importChain(parents, importer, themeRoot)} imports ${source} — controllers are framework-free; use host.session() for the session, host.t() for text, host.request() for the API and host.navigate() for navigation.`,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
|
|
268
|
+
|
|
269
|
+
if (resolved && !resolved.external && !parents.has(resolved.id)) parents.set(resolved.id, importer);
|
|
270
|
+
|
|
271
|
+
return resolved;
|
|
272
|
+
},
|
|
273
|
+
load(id) {
|
|
274
|
+
return id === RESOLVED_ENTRY_ID ? entrySource(input) : null;
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @typedef {object} ControllersBuild
|
|
281
|
+
* @property {string | null} file absolute path of controllers.mjs, `null` when the plugin has no public controller
|
|
282
|
+
* @property {string[]} sources absolute paths of the controller files
|
|
283
|
+
* @property {number} stamp changes with every build (used to import a fresh copy)
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
let stampCounter = 0;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Runs the standalone build and writes `<outDir>/controllers/controllers.mjs` (and removes a stale one
|
|
290
|
+
* when the plugin has no public controller left).
|
|
291
|
+
*
|
|
292
|
+
* @param {ControllersOptions} [options]
|
|
293
|
+
* @returns {Promise<ControllersBuild>}
|
|
294
|
+
*/
|
|
295
|
+
export async function buildControllers(options = {}) {
|
|
296
|
+
const { root, dir, outDir } = resolvePaths(options);
|
|
297
|
+
const target = path.join(outDir, "controllers", "controllers.mjs");
|
|
298
|
+
const files = listControllerFiles(dir);
|
|
299
|
+
|
|
300
|
+
stampCounter += 1;
|
|
301
|
+
|
|
302
|
+
if (files.length === 0) {
|
|
303
|
+
fs.rmSync(target, { force: true });
|
|
304
|
+
|
|
305
|
+
return { file: null, sources: [], stamp: stampCounter };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
let { pluginId, namespace } = options;
|
|
309
|
+
|
|
310
|
+
if (!pluginId || !namespace) {
|
|
311
|
+
const config = await loadConfig(root);
|
|
312
|
+
|
|
313
|
+
pluginId = pluginId || config.pluginId;
|
|
314
|
+
namespace = namespace || config.namespace;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const input = { files, pluginId, namespace, root, dir };
|
|
318
|
+
|
|
319
|
+
const bundle = await rollup({
|
|
320
|
+
input: ENTRY_ID,
|
|
321
|
+
external: [],
|
|
322
|
+
treeshake: true,
|
|
323
|
+
onwarn(warning) {
|
|
324
|
+
if (warning.code === "UNRESOLVED_IMPORT") {
|
|
325
|
+
const importer = warning.ids?.[0] ?? warning.id;
|
|
326
|
+
const source = warning.exporter;
|
|
327
|
+
|
|
328
|
+
throw new Error(
|
|
329
|
+
`[pano-plugin] ${importer ? relative(path.dirname(input.dir), importer) : "a controller"} imports '${source}', which cannot be resolved — packages are bundled into controllers.mjs, so install it (bun add ${source}) or import a relative file.`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (warning.code === "CIRCULAR_DEPENDENCY" || warning.code === "EMPTY_BUNDLE") return;
|
|
334
|
+
|
|
335
|
+
console.warn(`[pano-plugin] controllers: ${warning.message}`);
|
|
336
|
+
},
|
|
337
|
+
plugins: [
|
|
338
|
+
standalonePlugin(input),
|
|
339
|
+
...(options.plugins ?? []),
|
|
340
|
+
nodeResolve({ browser: true, preferBuiltins: false }),
|
|
341
|
+
],
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
const { output } = await bundle.generate({
|
|
346
|
+
format: "es",
|
|
347
|
+
inlineDynamicImports: true,
|
|
348
|
+
plugins: [terser()],
|
|
349
|
+
});
|
|
350
|
+
const chunk = output.find((item) => item.type === "chunk");
|
|
351
|
+
|
|
352
|
+
if (!chunk || chunk.type !== "chunk") {
|
|
353
|
+
throw new Error("[pano-plugin] controllers: the standalone build produced no module");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (chunk.imports.length > 0 || chunk.dynamicImports.length > 0) {
|
|
357
|
+
throw new Error(
|
|
358
|
+
`[pano-plugin] controllers.mjs must not import anything, found ${[...chunk.imports, ...chunk.dynamicImports].join(", ")}`,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
writeIfChanged(target, chunk.code.endsWith("\n") ? chunk.code : chunk.code + "\n");
|
|
363
|
+
} finally {
|
|
364
|
+
await bundle.close();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return { file: target, sources: files, stamp: stampCounter };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** @type {Map<string, Promise<ControllersBuild>>} */
|
|
371
|
+
const builds = new Map();
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @param {ControllersOptions} options
|
|
375
|
+
* @returns {string}
|
|
376
|
+
*/
|
|
377
|
+
function buildKey(options) {
|
|
378
|
+
const { root, dir, outDir } = resolvePaths(options);
|
|
379
|
+
|
|
380
|
+
return [root, dir, outDir].join("\0");
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* The standalone build of the current build cycle, started once and shared by the plugins of the
|
|
385
|
+
* same configuration (controllers, types and meta all need the fresh file).
|
|
386
|
+
*
|
|
387
|
+
* @param {ControllersOptions} [options]
|
|
388
|
+
* @returns {Promise<ControllersBuild>}
|
|
389
|
+
*/
|
|
390
|
+
export function ensureControllersBuilt(options = {}) {
|
|
391
|
+
const key = buildKey(options);
|
|
392
|
+
let promise = builds.get(key);
|
|
393
|
+
|
|
394
|
+
if (!promise) {
|
|
395
|
+
promise = buildControllers(options);
|
|
396
|
+
promise.catch(() => {});
|
|
397
|
+
builds.set(key, promise);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return promise;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Starts a new build cycle: the next `ensureControllersBuilt` builds again.
|
|
405
|
+
*
|
|
406
|
+
* @param {ControllersOptions} [options]
|
|
407
|
+
*/
|
|
408
|
+
export function resetControllersBuild(options = {}) {
|
|
409
|
+
builds.delete(buildKey(options));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Second build: controllers/controllers.mjs, one self-contained ES module exporting controllers,
|
|
414
|
+
* createHost, create, meta (doc 02 section 5.1). Runs in `writeBundle` of the configuration it is
|
|
415
|
+
* listed in (after plugins that clean the output folder), and watches `src/theme`.
|
|
416
|
+
*
|
|
417
|
+
* @param {ControllersOptions} [options]
|
|
418
|
+
* @returns {import('rollup').Plugin}
|
|
419
|
+
*/
|
|
420
|
+
export function panoControllers(options = {}) {
|
|
421
|
+
const { dir } = resolvePaths(options);
|
|
422
|
+
|
|
423
|
+
return {
|
|
424
|
+
name: "pano-controllers",
|
|
425
|
+
buildStart() {
|
|
426
|
+
resetControllersBuild(options);
|
|
427
|
+
|
|
428
|
+
for (const file of themeSourceFiles(dir)) this.addWatchFile(file);
|
|
429
|
+
},
|
|
430
|
+
async writeBundle() {
|
|
431
|
+
await ensureControllersBuilt(options);
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Entry wrapper and the `pano:controllers` virtual module (doc 02 section 4, doc 01 section 2).
|
|
6
|
+
*
|
|
7
|
+
* The build input is the virtual module `pano:entry`, a pure re-export facade over the wrapper
|
|
8
|
+
* (see the long note in `index.js`). The wrapper imports the author's `src/main.js` (or, when it is
|
|
9
|
+
* missing, a default `export default class extends PanoPlugin {}`), exports `panoSdk = 2` and
|
|
10
|
+
* makes `onLoad()` register, in this order:
|
|
11
|
+
*
|
|
12
|
+
* 1. `this.pano.controllers?.register(pluginId, namespace, controllers)`
|
|
13
|
+
* 2. `this.pano.views?.add(views)`
|
|
14
|
+
* 3. the author's own `onLoad`
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Virtual module ids. */
|
|
18
|
+
export const ENTRY_ID = 'pano:entry';
|
|
19
|
+
export const VIEWS_ID = 'pano:views';
|
|
20
|
+
export const CONTROLLERS_ID = 'pano:controllers';
|
|
21
|
+
|
|
22
|
+
/** Resolved (internal) ids; the build's `manualChunks` uses them to keep the entry in one shared chunk. */
|
|
23
|
+
export const FACADE_RESOLVED_ID = '\0pano-entry-facade';
|
|
24
|
+
export const WRAPPER_RESOLVED_ID = '\0pano-entry-wrapper';
|
|
25
|
+
export const DEFAULT_MAIN_RESOLVED_ID = '\0pano-default-main';
|
|
26
|
+
const VIEWS_FALLBACK_RESOLVED_ID = '\0pano-views-fallback';
|
|
27
|
+
const CONTROLLERS_RESOLVED_ID = '\0pano-controllers';
|
|
28
|
+
|
|
29
|
+
/** Default controllers folder (relative to the plugin root). */
|
|
30
|
+
export const DEFAULT_CONTROLLERS_DIR = 'src/theme/controllers';
|
|
31
|
+
|
|
32
|
+
/** Files of the controllers folder that are never controllers. */
|
|
33
|
+
const NOT_CONTROLLERS = new Set(['types.js']);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} file
|
|
37
|
+
* @returns {string} forward-slash form, safe inside a JS string on every platform
|
|
38
|
+
*/
|
|
39
|
+
function posix(file) {
|
|
40
|
+
return file.replace(/\\/g, '/');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The public controllers of a folder: every `*.js` file directly in it, except `_*.js` and
|
|
45
|
+
* `types.js`; sub-folders are private code and are skipped. Sorted by name.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} dir absolute folder
|
|
48
|
+
* @returns {{ name: string, file: string }[]} `name` = file name without `.js`
|
|
49
|
+
*/
|
|
50
|
+
export function listControllerFiles(dir) {
|
|
51
|
+
let entries;
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
55
|
+
} catch {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return entries
|
|
60
|
+
.filter(
|
|
61
|
+
(entry) =>
|
|
62
|
+
entry.isFile() &&
|
|
63
|
+
entry.name.endsWith('.js') &&
|
|
64
|
+
!entry.name.startsWith('_') &&
|
|
65
|
+
!NOT_CONTROLLERS.has(entry.name),
|
|
66
|
+
)
|
|
67
|
+
.map((entry) => ({ name: entry.name.slice(0, -3), file: path.join(dir, entry.name) }))
|
|
68
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Source of the virtual module `pano:controllers`: a default export mapping controller name to
|
|
73
|
+
* the file's default export.
|
|
74
|
+
*
|
|
75
|
+
* @param {{ name: string, file: string }[]} files
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
78
|
+
export function controllersModuleSource(files) {
|
|
79
|
+
const imports = files.map(
|
|
80
|
+
(item, index) => `import controller${index} from ${JSON.stringify(posix(item.file))};`,
|
|
81
|
+
);
|
|
82
|
+
const entries = files.map((item, index) => ` ${JSON.stringify(item.name)}: controller${index},`);
|
|
83
|
+
|
|
84
|
+
return `${imports.join('\n')}${imports.length ? '\n' : ''}export default {\n${entries.join('\n')}${entries.length ? '\n' : ''}};\n`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Source of the entry wrapper (doc 01 section 2).
|
|
89
|
+
*
|
|
90
|
+
* @param {{ pluginId: string, namespace: string, mainId: string }} input `mainId` = the module the
|
|
91
|
+
* author's entry resolves to (the real `src/main.js` or the default one)
|
|
92
|
+
* @returns {string}
|
|
93
|
+
*/
|
|
94
|
+
export function wrapperSource({ pluginId, namespace, mainId }) {
|
|
95
|
+
const main = JSON.stringify(posix(mainId));
|
|
96
|
+
|
|
97
|
+
return [
|
|
98
|
+
`import Author from ${main};`,
|
|
99
|
+
`import views from ${JSON.stringify(VIEWS_ID)};`,
|
|
100
|
+
`import controllers from ${JSON.stringify(CONTROLLERS_ID)};`,
|
|
101
|
+
`export * from ${main};`,
|
|
102
|
+
'',
|
|
103
|
+
'// Pano SDK level of this bundle: the host loads modules without it as legacy plugins.',
|
|
104
|
+
'export const panoSdk = 2;',
|
|
105
|
+
'',
|
|
106
|
+
`const pluginId = ${JSON.stringify(pluginId)};`,
|
|
107
|
+
`const namespace = ${JSON.stringify(namespace)};`,
|
|
108
|
+
'',
|
|
109
|
+
'export default class extends Author {',
|
|
110
|
+
' onLoad(...args) {',
|
|
111
|
+
' // Order is part of the contract: controllers, then views, then the author\'s onLoad.',
|
|
112
|
+
' this.pano?.controllers?.register(pluginId, namespace, controllers);',
|
|
113
|
+
' this.pano?.views?.add(views);',
|
|
114
|
+
'',
|
|
115
|
+
' return super.onLoad?.(...args);',
|
|
116
|
+
' }',
|
|
117
|
+
'}',
|
|
118
|
+
'',
|
|
119
|
+
].join('\n');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Source of the entry used when the plugin has no `src/main.js`. */
|
|
123
|
+
export function defaultMainSource() {
|
|
124
|
+
return [
|
|
125
|
+
"import { PanoPlugin } from '@panomc/sdk';",
|
|
126
|
+
'',
|
|
127
|
+
'export default class extends PanoPlugin {}',
|
|
128
|
+
'',
|
|
129
|
+
].join('\n');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Entry wrapper plugin: resolves `pano:entry` (facade over the wrapper), the wrapper itself, the
|
|
134
|
+
* default `src/main.js`, and a last-resort empty `pano:views` module (used only while no other plugin
|
|
135
|
+
* resolves `pano:views`; `panoViews()` takes precedence).
|
|
136
|
+
*
|
|
137
|
+
* @param {{ pluginId: string, namespace: string, controllersDir?: string, root?: string, main?: string }} [options]
|
|
138
|
+
* `main` = path of the author's entry, default `<root>/src/main.js`
|
|
139
|
+
* @returns {import('rollup').Plugin}
|
|
140
|
+
*/
|
|
141
|
+
export function panoEntry(options = /** @type {any} */ ({})) {
|
|
142
|
+
const { pluginId, namespace } = options;
|
|
143
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
144
|
+
const mainFile = path.resolve(root, options.main ?? 'src/main.js');
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
name: 'pano-entry',
|
|
148
|
+
|
|
149
|
+
async resolveId(source, importer, resolveOptions) {
|
|
150
|
+
if (source === ENTRY_ID) return FACADE_RESOLVED_ID;
|
|
151
|
+
|
|
152
|
+
if (
|
|
153
|
+
source === WRAPPER_RESOLVED_ID ||
|
|
154
|
+
source === DEFAULT_MAIN_RESOLVED_ID ||
|
|
155
|
+
source === FACADE_RESOLVED_ID
|
|
156
|
+
) {
|
|
157
|
+
return source;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (source === VIEWS_ID) {
|
|
161
|
+
// Last resort only: when no other plugin (panoViews) resolves pano:views, the plugin has
|
|
162
|
+
// no views and the module is empty.
|
|
163
|
+
const other = await this.resolve(source, importer, {
|
|
164
|
+
...resolveOptions,
|
|
165
|
+
skipSelf: true,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return other ?? VIEWS_FALLBACK_RESOLVED_ID;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return null;
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
load(id) {
|
|
175
|
+
if (id === FACADE_RESOLVED_ID) {
|
|
176
|
+
return (
|
|
177
|
+
`export * from ${JSON.stringify(WRAPPER_RESOLVED_ID)};\n` +
|
|
178
|
+
`export { default } from ${JSON.stringify(WRAPPER_RESOLVED_ID)};\n`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (id === WRAPPER_RESOLVED_ID) {
|
|
183
|
+
if (!pluginId || !namespace) {
|
|
184
|
+
this.error('[pano-plugin] panoEntry needs pluginId and namespace');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const exists = fs.existsSync(mainFile);
|
|
188
|
+
|
|
189
|
+
if (exists) this.addWatchFile(mainFile);
|
|
190
|
+
|
|
191
|
+
return wrapperSource({
|
|
192
|
+
pluginId,
|
|
193
|
+
namespace,
|
|
194
|
+
mainId: exists ? mainFile : DEFAULT_MAIN_RESOLVED_ID,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (id === DEFAULT_MAIN_RESOLVED_ID) return defaultMainSource();
|
|
199
|
+
|
|
200
|
+
if (id === VIEWS_FALLBACK_RESOLVED_ID) return 'export default [];\n';
|
|
201
|
+
|
|
202
|
+
return null;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Virtual module `pano:controllers`: the default export of every file directly under
|
|
209
|
+
* `src/theme/controllers` (`_*.js`, `types.js` and sub-folders skipped), as `{ [fileName]: definition }`.
|
|
210
|
+
*
|
|
211
|
+
* @param {{ dir?: string, root?: string }} [options] controllers folder (default
|
|
212
|
+
* `src/theme/controllers`, relative to `root`)
|
|
213
|
+
* @returns {import('rollup').Plugin}
|
|
214
|
+
*/
|
|
215
|
+
export function panoControllersModule(options = {}) {
|
|
216
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
217
|
+
const dir = path.resolve(root, options.dir ?? DEFAULT_CONTROLLERS_DIR);
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
name: 'pano-controllers-module',
|
|
221
|
+
|
|
222
|
+
resolveId: {
|
|
223
|
+
order: 'pre',
|
|
224
|
+
handler(source) {
|
|
225
|
+
if (source === CONTROLLERS_ID) return CONTROLLERS_RESOLVED_ID;
|
|
226
|
+
return null;
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
load(id) {
|
|
231
|
+
if (id !== CONTROLLERS_RESOLVED_ID) return null;
|
|
232
|
+
|
|
233
|
+
const files = listControllerFiles(dir);
|
|
234
|
+
|
|
235
|
+
if (fs.existsSync(dir)) this.addWatchFile(dir);
|
|
236
|
+
|
|
237
|
+
for (const item of files) {
|
|
238
|
+
const text = fs.readFileSync(item.file, 'utf8');
|
|
239
|
+
|
|
240
|
+
if (!/\bexport\s+default\b|\bas\s+default\b/.test(text)) {
|
|
241
|
+
this.error(
|
|
242
|
+
`${path.relative(root, item.file)} has no default export: every file directly in ${posix(path.relative(root, dir))}/ is a public controller, so it must "export default defineController({...})"; rename a private helper to _${path.basename(item.file)} or move it into a sub-folder`,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return controllersModuleSource(files);
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|