clovie 0.2.3 → 0.2.5
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/factories/api.js +3 -1
- package/lib/factories/routes.js +3 -1
- package/lib/services/Compile.js +4 -5
- package/lib/services/Run.js +17 -8
- package/lib/utils/extractScripts.js +49 -19
- package/lib/utils/viewsToRoutes.js +12 -3
- package/package.json +1 -1
package/lib/factories/api.js
CHANGED
package/lib/factories/routes.js
CHANGED
package/lib/services/Compile.js
CHANGED
|
@@ -180,18 +180,17 @@ export class Compile extends ServiceProvider {
|
|
|
180
180
|
},
|
|
181
181
|
|
|
182
182
|
routes: async (opts) => {
|
|
183
|
-
|
|
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',
|
|
188
|
+
const progressId = this.#progressTracker.createProgressBar('Compiling Routes', opts.routes.length, 'routes');
|
|
190
189
|
const promises = [];
|
|
191
|
-
const routesLength =
|
|
190
|
+
const routesLength = opts.routes.length;
|
|
192
191
|
|
|
193
192
|
for (let i = 0; i < routesLength; i++) {
|
|
194
|
-
const route =
|
|
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);
|
package/lib/services/Run.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { ServiceProvider } from '@jucie.io/engine';
|
|
2
2
|
import { normalizeToFactories } from '../utils/normalizeToFactories.js';
|
|
3
3
|
import { viewsToRoutes, pagesToRoutes, resolveRoutes } from '../utils/viewsToRoutes.js';
|
|
4
|
-
import { defineRoutes } from '
|
|
5
|
-
import { defineHooks } from '../factories/hooks.js';
|
|
6
|
-
import { defineMiddleware } from '../factories/middleware.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 {
|
|
@@ -26,6 +24,7 @@ export class Run extends ServiceProvider {
|
|
|
26
24
|
|
|
27
25
|
serve: async () => {
|
|
28
26
|
const opts = configurator.opts;
|
|
27
|
+
await this.#normalizeRoutes(opts);
|
|
29
28
|
const server = useContext('server');
|
|
30
29
|
if (!server) {
|
|
31
30
|
throw new Error('Server service not available. Use type: "server" in opts.');
|
|
@@ -51,6 +50,8 @@ export class Run extends ServiceProvider {
|
|
|
51
50
|
|
|
52
51
|
log.info('🚀 Starting development server...');
|
|
53
52
|
|
|
53
|
+
await this.#normalizeRoutes(configurator.opts);
|
|
54
|
+
|
|
54
55
|
// Build initial version
|
|
55
56
|
await this.#build(configurator.opts);
|
|
56
57
|
|
|
@@ -88,6 +89,12 @@ export class Run extends ServiceProvider {
|
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
91
|
|
|
92
|
+
async #normalizeRoutes(opts) {
|
|
93
|
+
opts.api = opts.api && opts.api.length ? await resolveRoutes(opts.api, (...args) => this.useContext(...args), opts) : [];
|
|
94
|
+
opts.routes = opts.routes && opts.routes.length ? await resolveRoutes(opts.routes, (...args) => this.useContext(...args), opts) : [];
|
|
95
|
+
return opts;
|
|
96
|
+
}
|
|
97
|
+
|
|
91
98
|
async #build(opts) {
|
|
92
99
|
const [file, compile, log] = this.useContext('file', 'compile', 'log');
|
|
93
100
|
|
|
@@ -97,6 +104,8 @@ export class Run extends ServiceProvider {
|
|
|
97
104
|
log.info('🚀 Starting compilation...');
|
|
98
105
|
const startTime = Date.now();
|
|
99
106
|
|
|
107
|
+
opts = await this.#normalizeRoutes(opts);
|
|
108
|
+
|
|
100
109
|
// Phase 1: partials must load before templates can render
|
|
101
110
|
await compile.partials(opts);
|
|
102
111
|
|
|
@@ -153,13 +162,13 @@ export class Run extends ServiceProvider {
|
|
|
153
162
|
const services = { file, liveReload };
|
|
154
163
|
|
|
155
164
|
const viewRoutes = viewsToRoutes(opts, services);
|
|
156
|
-
const pageRoutes = pagesToRoutes(
|
|
165
|
+
const pageRoutes = pagesToRoutes(opts.routes, opts, services);
|
|
157
166
|
|
|
158
167
|
const factories = [
|
|
159
|
-
...normalizeToFactories(opts.hooks,
|
|
160
|
-
...normalizeToFactories(opts.middleware,
|
|
161
|
-
...normalizeToFactories(opts.api,
|
|
162
|
-
...normalizeToFactories([...viewRoutes, ...pageRoutes],
|
|
168
|
+
...normalizeToFactories(opts.hooks, defineServerHooks),
|
|
169
|
+
...normalizeToFactories(opts.middleware, defineServerMiddleware),
|
|
170
|
+
...normalizeToFactories(opts.api, defineServerRoutes),
|
|
171
|
+
...normalizeToFactories([...viewRoutes, ...pageRoutes], defineServerRoutes),
|
|
163
172
|
];
|
|
164
173
|
|
|
165
174
|
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
|
-
|
|
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
|
-
*
|
|
44
|
-
*
|
|
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
|
|
48
|
-
* @param {string} opts.scriptsDir
|
|
49
|
-
* @param {string} [opts.viewsDir]
|
|
50
|
-
* @param {string} [opts.partialsDir]
|
|
51
|
-
* @param {object[]} [opts.routes]
|
|
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({
|
|
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
|
|
63
|
-
for (const dir of
|
|
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
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
19
|
-
|
|
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.
|