clovie 0.2.1 → 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.
package/lib/services/Compile.js
CHANGED
|
@@ -6,6 +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 { resolveRoutes } from '../utils/viewsToRoutes.js';
|
|
9
10
|
|
|
10
11
|
export class Compile extends ServiceProvider {
|
|
11
12
|
|
|
@@ -179,17 +180,18 @@ export class Compile extends ServiceProvider {
|
|
|
179
180
|
},
|
|
180
181
|
|
|
181
182
|
routes: async (opts) => {
|
|
182
|
-
|
|
183
|
+
const routeConfigs = resolveRoutes(opts.routes, (...args) => this.useContext(...args));
|
|
184
|
+
if (!routeConfigs.length) {
|
|
183
185
|
log.debug('No routes to compile');
|
|
184
186
|
return [];
|
|
185
187
|
}
|
|
186
|
-
|
|
187
|
-
const progressId = this.#progressTracker.createProgressBar('Compiling Routes',
|
|
188
|
+
|
|
189
|
+
const progressId = this.#progressTracker.createProgressBar('Compiling Routes', routeConfigs.length, 'routes');
|
|
188
190
|
const promises = [];
|
|
189
|
-
const routesLength =
|
|
191
|
+
const routesLength = routeConfigs.length;
|
|
190
192
|
|
|
191
193
|
for (let i = 0; i < routesLength; i++) {
|
|
192
|
-
const route =
|
|
194
|
+
const route = routeConfigs[i];
|
|
193
195
|
if (route.repeat) {
|
|
194
196
|
const data = route.repeat(opts.data);
|
|
195
197
|
const entries = Array.isArray(data) ? data.entries() : Object.entries(data);
|
|
@@ -216,7 +218,7 @@ export class Compile extends ServiceProvider {
|
|
|
216
218
|
}
|
|
217
219
|
|
|
218
220
|
const results = await Promise.all(promises);
|
|
219
|
-
this.#progressTracker.completeProgress(progressId, `✅ Compiled ${
|
|
221
|
+
this.#progressTracker.completeProgress(progressId, `✅ Compiled ${routeConfigs.length} routes`);
|
|
220
222
|
|
|
221
223
|
return results.filter(Boolean);
|
|
222
224
|
},
|
package/lib/services/Run.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ServiceProvider } from '@jucie.io/engine';
|
|
2
2
|
import { normalizeToFactories } from '../utils/normalizeToFactories.js';
|
|
3
|
-
import { viewsToRoutes, pagesToRoutes } from '../utils/viewsToRoutes.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,7 +153,7 @@ export class Run extends ServiceProvider {
|
|
|
153
153
|
const services = { file, liveReload };
|
|
154
154
|
|
|
155
155
|
const viewRoutes = viewsToRoutes(opts, services);
|
|
156
|
-
const pageRoutes = pagesToRoutes(opts.routes, opts, services);
|
|
156
|
+
const pageRoutes = pagesToRoutes(resolveRoutes(opts.routes, (...args) => this.useContext(...args)), opts, services);
|
|
157
157
|
|
|
158
158
|
const factories = [
|
|
159
159
|
...normalizeToFactories(opts.hooks, defineHooks),
|
|
@@ -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,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,
|