clovie 0.2.2 → 0.2.3

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.
@@ -6,7 +6,7 @@ import { createProgressTracker } from '../utils/progress.js';
6
6
 
7
7
  import { replaceRouteParams } from '../utils/routeMatch.js';
8
8
  import { formatOutputPath } from '../utils/outputPath.js';
9
- import { isFactory } from '../utils/normalizeToFactories.js';
9
+ import { resolveRoutes } from '../utils/viewsToRoutes.js';
10
10
 
11
11
  export class Compile extends ServiceProvider {
12
12
 
@@ -180,17 +180,12 @@ export class Compile extends ServiceProvider {
180
180
  },
181
181
 
182
182
  routes: async (opts) => {
183
- if (!opts.routes || opts.routes.length === 0) {
184
- log.debug('No routes to compile');
185
- return [];
186
- }
187
-
188
- const routeConfigs = opts.routes.filter(r => !isFactory(r));
183
+ const routeConfigs = resolveRoutes(opts.routes, (...args) => this.useContext(...args));
189
184
  if (!routeConfigs.length) {
190
185
  log.debug('No routes to compile');
191
186
  return [];
192
187
  }
193
-
188
+
194
189
  const progressId = this.#progressTracker.createProgressBar('Compiling Routes', routeConfigs.length, 'routes');
195
190
  const promises = [];
196
191
  const routesLength = routeConfigs.length;
@@ -1,6 +1,6 @@
1
1
  import { ServiceProvider } from '@jucie.io/engine';
2
- import { normalizeToFactories, normalizeRoutesToFactories } from '../utils/normalizeToFactories.js';
3
- import { viewsToRoutes } from '../utils/viewsToRoutes.js';
2
+ import { normalizeToFactories } from '../utils/normalizeToFactories.js';
3
+ import { viewsToRoutes, pagesToRoutes, resolveRoutes } from '../utils/viewsToRoutes.js';
4
4
  import { defineRoutes } from '../factories/routes.js';
5
5
  import { defineHooks } from '../factories/hooks.js';
6
6
  import { defineMiddleware } from '../factories/middleware.js';
@@ -153,13 +153,13 @@ export class Run extends ServiceProvider {
153
153
  const services = { file, liveReload };
154
154
 
155
155
  const viewRoutes = viewsToRoutes(opts, services);
156
+ const pageRoutes = pagesToRoutes(resolveRoutes(opts.routes, (...args) => this.useContext(...args)), opts, services);
156
157
 
157
158
  const factories = [
158
159
  ...normalizeToFactories(opts.hooks, defineHooks),
159
160
  ...normalizeToFactories(opts.middleware, defineMiddleware),
160
161
  ...normalizeToFactories(opts.api, defineRoutes),
161
- ...normalizeRoutesToFactories(opts.routes, defineRoutes, opts, services),
162
- ...normalizeToFactories(viewRoutes, defineRoutes),
162
+ ...normalizeToFactories([...viewRoutes, ...pageRoutes], defineRoutes),
163
163
  ];
164
164
 
165
165
  if (factories.length) {
@@ -1,5 +1,6 @@
1
1
  import path from 'path';
2
2
  import { existsSync, readFileSync, readdirSync } from 'fs';
3
+ import { resolveRoutes } from './viewsToRoutes.js';
3
4
 
4
5
  const SCRIPT_SRC_RE = /<script[^>]+src\s*=\s*["']([^"']+)["'][^>]*>/gi;
5
6
  const EXTERNAL_RE = /^(https?:\/\/|\/\/|data:)/i;
@@ -68,7 +69,7 @@ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, p
68
69
  }
69
70
  }
70
71
 
71
- for (const route of routes) {
72
+ for (const route of resolveRoutes(routes)) {
72
73
  if (route?.template && existsSync(route.template)) {
73
74
  try {
74
75
  const content = readFileSync(route.template, 'utf-8');
@@ -1,5 +1,4 @@
1
1
  import { definitionType } from '@jucie.io/engine';
2
- import { pagesToRoutes } from './viewsToRoutes.js';
3
2
 
4
3
  /**
5
4
  * Normalizes a config value into an array of engine-server factory definitions.
@@ -44,48 +43,10 @@ export function normalizeToFactories(value, factoryFn) {
44
43
  return factories;
45
44
  }
46
45
 
47
- export function isFactory(value) {
46
+ function isFactory(value) {
48
47
  return typeof value === 'function' && definitionType(value) !== undefined;
49
48
  }
50
49
 
51
- /**
52
- * Like normalizeToFactories, but raw route config objects are converted
53
- * to engine-server route objects via pagesToRoutes before wrapping.
54
- * Factory definitions pass through as-is.
55
- *
56
- * @param {*} value - The routes config value (raw configs, factories, or intermixed)
57
- * @param {Function} factoryFn - The define* factory to wrap converted routes with
58
- * @param {object} opts - Clovie config opts (passed to pagesToRoutes)
59
- * @param {object} services - Injected services { file, liveReload }
60
- * @returns {Array} Array of factory definitions ready for server.use()
61
- */
62
- export function normalizeRoutesToFactories(value, factoryFn, opts, services) {
63
- if (!value) return [];
64
- if (isFactory(value)) return [value];
65
- if (!Array.isArray(value)) return [wrapRaw(factoryFn, pagesToRoutes([value], opts, services))];
66
-
67
- const raw = [];
68
- const factories = [];
69
-
70
- for (const item of value) {
71
- if (isFactory(item)) {
72
- if (raw.length) {
73
- factories.push(wrapRaw(factoryFn, pagesToRoutes([...raw], opts, services)));
74
- raw.length = 0;
75
- }
76
- factories.push(item);
77
- } else {
78
- raw.push(item);
79
- }
80
- }
81
-
82
- if (raw.length) {
83
- factories.push(wrapRaw(factoryFn, pagesToRoutes(raw, opts, services)));
84
- }
85
-
86
- return factories;
87
- }
88
-
89
50
  function wrapRaw(factoryFn, data) {
90
51
  return factoryFn(() => data);
91
52
  }
@@ -1,5 +1,29 @@
1
1
  import path from 'path';
2
2
 
3
+ /**
4
+ * Resolves route config arrays that may contain plain objects or functions.
5
+ * Functions are called and their return values (single object or array)
6
+ * are flattened into the result.
7
+ *
8
+ * @param {Array} routes - Mixed array of route config objects and functions
9
+ * @returns {Array} Flat array of route config objects
10
+ */
11
+ export function resolveRoutes(routes, useContext) {
12
+ if (!routes) return [];
13
+
14
+ const resolved = [];
15
+ for (const item of routes) {
16
+ if (typeof item === 'function') {
17
+ const result = item(useContext);
18
+ if (Array.isArray(result)) resolved.push(...result);
19
+ else if (result) resolved.push(result);
20
+ } else {
21
+ resolved.push(item);
22
+ }
23
+ }
24
+ return resolved;
25
+ }
26
+
3
27
  /**
4
28
  * Scans the views directory and produces engine-server route definitions.
5
29
  * Each template file becomes a GET route whose handler renders the template.
@@ -39,7 +63,7 @@ export function pagesToRoutes(pages, opts, services) {
39
63
  if (!pages || !pages.length) return [];
40
64
 
41
65
  return pages.map(page => ({
42
- method: 'GET',
66
+ method: page.method || 'GET',
43
67
  path: page.path,
44
68
  handler: createTemplateHandler(
45
69
  page.template,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clovie",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Vintage web dev tooling with modern quality of life",
5
5
  "keywords": [
6
6
  "static-site-generator",