clovie 0.2.3 → 0.2.4

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.
@@ -7,6 +7,7 @@ import { transformConfig } from './utils/transformConfig.js';
7
7
  import { Server } from '@jucie.io/engine-server';
8
8
  import { EsBuild } from '@jucie.io/engine-esbuild';
9
9
  import { LiveReload } from './services/LiveReload.js';
10
+ import { resolveRoutes } from './utils/viewsToRoutes.js';
10
11
 
11
12
  export const createClovie = async (config = {}) => {
12
13
  return new Promise((resolve, reject) => {
@@ -57,6 +58,9 @@ export const createClovie = async (config = {}) => {
57
58
  }
58
59
  }
59
60
 
61
+ opts.api = opts.api && opts.api.length ? resolveRoutes(opts.api, (...args) => clovie.useContext(...args)) : [];
62
+ opts.routes = opts.routes && opts.routes.length ? resolveRoutes(opts.routes, (...args) => clovie.useContext(...args)) : [];
63
+
60
64
  // Don't auto-execute commands - let CLI decide
61
65
  resolve(clovie);
62
66
  });
@@ -1 +1,3 @@
1
- export{ defineRoutes as defineApi } from '@jucie.io/engine-server';
1
+ import { createDefinition } from '@jucie.io/engine';
2
+
3
+ export const defineApi = createDefinition('CLOVIE_API', [Array]);
@@ -1 +1,3 @@
1
- export { defineRoutes } from '@jucie.io/engine-server';
1
+ import { createDefinition } from '@jucie.io/engine';
2
+
3
+ export const defineRoutes = createDefinition('CLOVIE_ROUTES', [Array]);
@@ -180,18 +180,17 @@ export class Compile extends ServiceProvider {
180
180
  },
181
181
 
182
182
  routes: async (opts) => {
183
- const routeConfigs = resolveRoutes(opts.routes, (...args) => this.useContext(...args));
184
- if (!routeConfigs.length) {
183
+ if (!opts.routes.length) {
185
184
  log.debug('No routes to compile');
186
185
  return [];
187
186
  }
188
187
 
189
- const progressId = this.#progressTracker.createProgressBar('Compiling Routes', routeConfigs.length, 'routes');
188
+ const progressId = this.#progressTracker.createProgressBar('Compiling Routes', opts.routes.length, 'routes');
190
189
  const promises = [];
191
- const routesLength = routeConfigs.length;
190
+ const routesLength = opts.routes.length;
192
191
 
193
192
  for (let i = 0; i < routesLength; i++) {
194
- const route = routeConfigs[i];
193
+ const route = opts.routes[i];
195
194
  if (route.repeat) {
196
195
  const data = route.repeat(opts.data);
197
196
  const entries = Array.isArray(data) ? data.entries() : Object.entries(data);
@@ -1,9 +1,7 @@
1
1
  import { ServiceProvider } from '@jucie.io/engine';
2
2
  import { normalizeToFactories } from '../utils/normalizeToFactories.js';
3
- import { viewsToRoutes, pagesToRoutes, resolveRoutes } from '../utils/viewsToRoutes.js';
4
- import { defineRoutes } from '../factories/routes.js';
5
- import { defineHooks } from '../factories/hooks.js';
6
- import { defineMiddleware } from '../factories/middleware.js';
3
+ import { viewsToRoutes, pagesToRoutes } from '../utils/viewsToRoutes.js';
4
+ import { defineRoutes as defineServerRoutes, defineHooks as defineServerHooks, defineMiddleware as defineServerMiddleware } from '@jucie.io/engine-server';
7
5
  import { extractScriptSources } from '../utils/extractScripts.js';
8
6
 
9
7
  export class Run extends ServiceProvider {
@@ -153,13 +151,13 @@ export class Run extends ServiceProvider {
153
151
  const services = { file, liveReload };
154
152
 
155
153
  const viewRoutes = viewsToRoutes(opts, services);
156
- const pageRoutes = pagesToRoutes(resolveRoutes(opts.routes, (...args) => this.useContext(...args)), opts, services);
154
+ const pageRoutes = pagesToRoutes(opts.routes, opts, services);
157
155
 
158
156
  const factories = [
159
- ...normalizeToFactories(opts.hooks, defineHooks),
160
- ...normalizeToFactories(opts.middleware, defineMiddleware),
161
- ...normalizeToFactories(opts.api, defineRoutes),
162
- ...normalizeToFactories([...viewRoutes, ...pageRoutes], defineRoutes),
157
+ ...normalizeToFactories(opts.hooks, defineServerHooks),
158
+ ...normalizeToFactories(opts.middleware, defineServerMiddleware),
159
+ ...normalizeToFactories(opts.api, defineServerRoutes),
160
+ ...normalizeToFactories([...viewRoutes, ...pageRoutes], defineServerRoutes),
163
161
  ];
164
162
 
165
163
  if (factories.length) {
@@ -1,15 +1,18 @@
1
1
  import path from 'path';
2
2
  import { existsSync, readFileSync, readdirSync } from 'fs';
3
- import { resolveRoutes } from './viewsToRoutes.js';
4
3
 
5
4
  const SCRIPT_SRC_RE = /<script[^>]+src\s*=\s*["']([^"']+)["'][^>]*>/gi;
6
5
  const EXTERNAL_RE = /^(https?:\/\/|\/\/|data:)/i;
7
6
 
8
7
  function addSource(src, scriptsDir, seen, entryPoints) {
9
8
  if (EXTERNAL_RE.test(src)) return;
10
- const sourcePath = path.resolve(scriptsDir, src);
9
+
10
+ const cleaned = src.replace(/^\/+/, '');
11
+ const sourcePath = path.resolve(scriptsDir, cleaned);
12
+
11
13
  if (seen.has(sourcePath)) return;
12
14
  seen.add(sourcePath);
15
+
13
16
  if (existsSync(sourcePath)) {
14
17
  entryPoints.push(sourcePath);
15
18
  }
@@ -38,20 +41,46 @@ function readFilesRecursively(dir) {
38
41
  return results;
39
42
  }
40
43
 
44
+ const TEMPLATE_EXTS = new Set(['.html', '.njk', '.hbs', '.pug', '.eta', '.mustache']);
45
+
46
+ function isTemplateFile(filePath) {
47
+ return TEMPLATE_EXTS.has(path.extname(filePath).toLowerCase());
48
+ }
49
+
50
+ /**
51
+ * Collect template file paths from resolved route config objects.
52
+ */
53
+ function collectRouteTemplates(routes) {
54
+ if (!routes) return [];
55
+ const templates = [];
56
+ for (const route of Array.isArray(routes) ? routes : [routes]) {
57
+ if (route?.template) templates.push(route.template);
58
+ }
59
+ return templates;
60
+ }
61
+
41
62
  /**
42
63
  * Extract local script source paths from rendered HTML and raw source files.
43
- * Scans rendered output, plus raw views/partials/route templates for a
44
- * shotgun approach that catches every possible script reference.
64
+ * Uses a shotgun approach: scans rendered output, raw views, partials,
65
+ * explicit route templates, and any additional template directories.
45
66
  *
46
67
  * @param {object} opts
47
- * @param {string[]} opts.htmlStrings - Rendered HTML content from views/routes
48
- * @param {string} opts.scriptsDir - Absolute path to scripts source directory
49
- * @param {string} [opts.viewsDir] - Absolute path to views directory
50
- * @param {string} [opts.partialsDir] - Absolute path to partials directory
51
- * @param {object[]} [opts.routes] - Route config objects with .template paths
68
+ * @param {string[]} opts.htmlStrings - Rendered HTML content from views/routes
69
+ * @param {string} opts.scriptsDir - Absolute path to scripts source directory
70
+ * @param {string} [opts.viewsDir] - Absolute path to views directory
71
+ * @param {string} [opts.partialsDir] - Absolute path to partials directory
72
+ * @param {object[]} [opts.routes] - Route config objects with .template paths
73
+ * @param {string[]} [opts.templateDirs] - Additional directories to scan for templates
52
74
  * @returns {string[]} Deduped absolute paths to source script files
53
75
  */
54
- export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, partialsDir, routes = [] }) {
76
+ export function extractScriptSources({
77
+ htmlStrings = [],
78
+ scriptsDir,
79
+ viewsDir,
80
+ partialsDir,
81
+ routes = [],
82
+ templateDirs = [],
83
+ }) {
55
84
  const seen = new Set();
56
85
  const entryPoints = [];
57
86
 
@@ -59,9 +88,10 @@ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, p
59
88
  extractFromString(html, scriptsDir, seen, entryPoints);
60
89
  }
61
90
 
62
- const sourceDirs = [viewsDir, partialsDir].filter(Boolean);
63
- for (const dir of sourceDirs) {
91
+ const scanDirs = [viewsDir, partialsDir, ...templateDirs].filter(Boolean);
92
+ for (const dir of scanDirs) {
64
93
  for (const filePath of readFilesRecursively(dir)) {
94
+ if (!isTemplateFile(filePath)) continue;
65
95
  try {
66
96
  const content = readFileSync(filePath, 'utf-8');
67
97
  extractFromString(content, scriptsDir, seen, entryPoints);
@@ -69,13 +99,13 @@ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, p
69
99
  }
70
100
  }
71
101
 
72
- for (const route of resolveRoutes(routes)) {
73
- if (route?.template && existsSync(route.template)) {
74
- try {
75
- const content = readFileSync(route.template, 'utf-8');
76
- extractFromString(content, scriptsDir, seen, entryPoints);
77
- } catch { /* skip unreadable files */ }
78
- }
102
+ for (const templatePath of collectRouteTemplates(routes)) {
103
+ const resolved = path.resolve(templatePath);
104
+ if (!existsSync(resolved)) continue;
105
+ try {
106
+ const content = readFileSync(resolved, 'utf-8');
107
+ extractFromString(content, scriptsDir, seen, entryPoints);
108
+ } catch { /* skip unreadable files */ }
79
109
  }
80
110
 
81
111
  return entryPoints;
@@ -3,9 +3,11 @@ import path from 'path';
3
3
  /**
4
4
  * Resolves route config arrays that may contain plain objects or functions.
5
5
  * Functions are called and their return values (single object or array)
6
- * are flattened into the result.
6
+ * are flattened into the result. If a factory has a _name property,
7
+ * it is prepended to each route's path.
7
8
  *
8
9
  * @param {Array} routes - Mixed array of route config objects and functions
10
+ * @param {Function} useContext - Context resolver for factory functions
9
11
  * @returns {Array} Flat array of route config objects
10
12
  */
11
13
  export function resolveRoutes(routes, useContext) {
@@ -14,9 +16,15 @@ export function resolveRoutes(routes, useContext) {
14
16
  const resolved = [];
15
17
  for (const item of routes) {
16
18
  if (typeof item === 'function') {
19
+ const prefix = item._name ? `/${item._name}` : '';
17
20
  const result = item(useContext);
18
- if (Array.isArray(result)) resolved.push(...result);
19
- else if (result) resolved.push(result);
21
+ const items = Array.isArray(result) ? result : result ? [result] : [];
22
+ for (const route of items) {
23
+ resolved.push(prefix && route.path
24
+ ? { ...route, path: path.posix.join(prefix, route.path) }
25
+ : route
26
+ );
27
+ }
20
28
  } else {
21
29
  resolved.push(item);
22
30
  }
@@ -24,6 +32,7 @@ export function resolveRoutes(routes, useContext) {
24
32
  return resolved;
25
33
  }
26
34
 
35
+
27
36
  /**
28
37
  * Scans the views directory and produces engine-server route definitions.
29
38
  * Each template file becomes a GET route whose handler renders the template.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clovie",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Vintage web dev tooling with modern quality of life",
5
5
  "keywords": [
6
6
  "static-site-generator",