@skafform/vite-plugin 0.1.4 → 0.1.6

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 (2) hide show
  1. package/dist/index.js +77 -0
  2. package/package.json +5 -1
package/dist/index.js CHANGED
@@ -9,6 +9,10 @@ var VIRTUAL_SERVER_INIT = "virtual:skafform/server-init";
9
9
  var RESOLVED_SERVER_INIT = "\0virtual:skafform/server-init";
10
10
  var VIRTUAL_ADMIN_SECTIONS = "virtual:skafform/admin-sections";
11
11
  var RESOLVED_ADMIN_SECTIONS = "\0virtual:skafform/admin-sections";
12
+ var VIRTUAL_WIDGET_REGISTRY = "virtual:skafform/widget-registry";
13
+ var RESOLVED_WIDGET_REGISTRY = "\0virtual:skafform/widget-registry";
14
+ var VIRTUAL_WIDGET_LOADERS = "virtual:skafform/widget-loaders";
15
+ var RESOLVED_WIDGET_LOADERS = "\0virtual:skafform/widget-loaders";
12
16
  var norm = (p) => p.replace(/\\/g, "/");
13
17
  function loadBrickIndex(root) {
14
18
  const bricksDir = resolve(root, "bricks");
@@ -178,6 +182,8 @@ ${lightVars}
178
182
  if (id === VIRTUAL_CONFIG) return RESOLVED_CONFIG;
179
183
  if (id === VIRTUAL_SERVER_INIT) return RESOLVED_SERVER_INIT;
180
184
  if (id === VIRTUAL_ADMIN_SECTIONS) return RESOLVED_ADMIN_SECTIONS;
185
+ if (id === VIRTUAL_WIDGET_REGISTRY) return RESOLVED_WIDGET_REGISTRY;
186
+ if (id === VIRTUAL_WIDGET_LOADERS) return RESOLVED_WIDGET_LOADERS;
181
187
  if (id.startsWith(".") && importer) {
182
188
  const themeDir = norm(resolve(root, `themes/${config.theme}`));
183
189
  const componentsDir = themeDir + "/components";
@@ -216,6 +222,77 @@ ${lightVars}
216
222
  }
217
223
  return `export default ${JSON.stringify(sections)}`;
218
224
  }
225
+ if (id === RESOLVED_WIDGET_REGISTRY) {
226
+ const bricksConfig = getBricksConfig();
227
+ if (!bricksConfig) return "export const widgetRegistry = {}";
228
+ const zones = {};
229
+ for (const [brickName, brick] of Object.entries(bricksConfig.bricks ?? {})) {
230
+ for (const widget of brick.widgets ?? []) {
231
+ if (!zones[widget.zone]) zones[widget.zone] = [];
232
+ zones[widget.zone].push({ brick: brickName, component: widget.component });
233
+ }
234
+ }
235
+ for (const widgets of Object.values(zones)) {
236
+ widgets.sort((a, b) => a.brick.localeCompare(b.brick));
237
+ }
238
+ const imports = [];
239
+ const entries = [];
240
+ let i = 0;
241
+ for (const [zone, widgets] of Object.entries(zones)) {
242
+ const items = [];
243
+ for (const w of widgets) {
244
+ const absPath = norm(resolve(root, w.component));
245
+ const basename = absPath.split("/").pop().replace(/\.[^.]+$/, "");
246
+ const varName = `wr_${i++}`;
247
+ imports.push(`import * as ${varName} from ${JSON.stringify(absPath)}`);
248
+ items.push(` { brick: ${JSON.stringify(w.brick)}, basename: ${JSON.stringify(basename)}, Component: ${varName}.default, schema: ${varName}.schema ?? [] }`);
249
+ }
250
+ entries.push(` ${JSON.stringify(zone)}: [
251
+ ${items.join(",\n")}
252
+ ]`);
253
+ }
254
+ return [
255
+ ...imports,
256
+ `export const widgetRegistry = {
257
+ ${entries.join(",\n")}
258
+ }`
259
+ ].join("\n");
260
+ }
261
+ if (id === RESOLVED_WIDGET_LOADERS) {
262
+ const bricksConfig = getBricksConfig();
263
+ if (!bricksConfig) return "export async function runWidgetLoaders() { return {} }";
264
+ const widgets = [];
265
+ let i = 0;
266
+ for (const [brickName, brick] of Object.entries(bricksConfig.bricks ?? {})) {
267
+ for (const widget of brick.widgets ?? []) {
268
+ const absPath = norm(resolve(root, widget.component));
269
+ const basename = absPath.split("/").pop().replace(/\.[^.]+$/, "");
270
+ const key = `${widget.zone}:${brickName}:${basename}`;
271
+ widgets.push({ key, absPath, varName: `wl_${i++}`, brickName, basename });
272
+ }
273
+ }
274
+ if (widgets.length === 0) {
275
+ return "export async function runWidgetLoaders() { return {} }";
276
+ }
277
+ return [
278
+ ...widgets.map((w) => `import * as ${w.varName} from ${JSON.stringify(w.absPath)}`),
279
+ `const _loaders = [`,
280
+ ...widgets.map((w) => ` { key: ${JSON.stringify(w.key)}, brick: ${JSON.stringify(w.brickName)}, basename: ${JSON.stringify(w.basename)}, fn: ${w.varName}.loader ?? null },`),
281
+ `]`,
282
+ `export async function runWidgetLoaders(request, dbWidgets) {`,
283
+ ` const _all = Object.values(dbWidgets ?? {}).flat()`,
284
+ ` const results = {}`,
285
+ ` await Promise.all(`,
286
+ ` _loaders.filter(w => w.fn !== null).map(async ({ key, fn, brick, basename }) => {`,
287
+ ` const _inst = _all.find(w => w.brick === brick && w.component.split('/').pop()?.replace(/\\.[^.]+$/, '') === basename)`,
288
+ ` const _config = _inst?.config ?? {}`,
289
+ ` try { results[key] = await fn({ request, config: _config }) } catch { results[key] = null }`,
290
+ ` })`,
291
+ ` )`,
292
+ ` return results`,
293
+ `}`
294
+ ].join("\n");
295
+ }
219
296
  if (id === RESOLVED_SERVER_INIT) {
220
297
  const bricksConfig = getBricksConfig();
221
298
  if (!bricksConfig) return "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skafform/vite-plugin",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -13,6 +13,10 @@
13
13
  "base.css",
14
14
  "types.d.ts"
15
15
  ],
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org",
18
+ "access": "public"
19
+ },
16
20
  "scripts": {
17
21
  "build": "tsup",
18
22
  "dev": "tsup --watch"