clovie 0.1.49 → 0.2.1

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.
@@ -25,7 +25,6 @@ export const createClovie = async (config = {}) => {
25
25
 
26
26
  if (opts.scripts) {
27
27
  clovie.install(EsBuild.configure({
28
- entryPoints: [`${opts.scripts}/**/*.js`],
29
28
  outdir: opts.outputDir,
30
29
  outbase: opts.scripts,
31
30
  bundle: true,
@@ -162,20 +162,20 @@ export class Compile extends ServiceProvider {
162
162
 
163
163
  for (let i = 0; i < filePaths.length; i++) {
164
164
  const filePath = filePaths[i];
165
- console.log('filePath', filePath);
166
165
  promises.push(
167
- this.#view(filePath, opts).then(() => {
166
+ this.#view(filePath, opts).then((content) => {
168
167
  this.#progressTracker.updateProgress(progressId, i + 1, {
169
168
  current: path.basename(filePath)
170
169
  });
170
+ return content;
171
171
  })
172
172
  );
173
173
  }
174
174
 
175
- await Promise.all(promises);
175
+ const results = await Promise.all(promises);
176
176
  this.#progressTracker.completeProgress(progressId, `✅ Compiled ${filePaths.length} views`);
177
177
 
178
- return promises;
178
+ return results.filter(Boolean);
179
179
  },
180
180
 
181
181
  routes: async (opts) => {
@@ -195,34 +195,36 @@ export class Compile extends ServiceProvider {
195
195
  const entries = Array.isArray(data) ? data.entries() : Object.entries(data);
196
196
  for (const [key, value] of entries) {
197
197
  promises.push(
198
- this.#route(route, opts, value, key).then(() => {
198
+ this.#route(route, opts, value, key).then((content) => {
199
199
  this.#progressTracker.updateProgress(progressId, i + 1, {
200
200
  current: route.path || 'route'
201
201
  });
202
+ return content;
202
203
  })
203
204
  );
204
205
  }
205
206
  continue;
206
207
  }
207
208
  promises.push(
208
- this.#route(route, opts, null, null).then(() => {
209
+ this.#route(route, opts, null, null).then((content) => {
209
210
  this.#progressTracker.updateProgress(progressId, i + 1, {
210
211
  current: route.path || 'route'
211
212
  });
213
+ return content;
212
214
  })
213
215
  );
214
216
  }
215
217
 
216
- await Promise.all(promises);
218
+ const results = await Promise.all(promises);
217
219
  this.#progressTracker.completeProgress(progressId, `✅ Compiled ${opts.routes.length} routes`);
218
220
 
219
- return promises;
221
+ return results.filter(Boolean);
220
222
  },
221
223
 
222
224
  asset: (path, opts) => this.#asset(path, opts),
223
225
  style: (path, opts) => this.#style(path, opts),
224
226
  partial: (path, opts) => this.#loadPartial(path, opts),
225
- view: (path, opts) => this.#view(path, opts),
227
+ view: (filePath, opts) => this.#view(filePath, opts),
226
228
  remove: (path, opts) => this.#remove(path, opts),
227
229
  route: (route, opts, item, key) => this.#route(route, opts, item, key),
228
230
  };
@@ -310,13 +312,13 @@ export class Compile extends ServiceProvider {
310
312
  const fileName = path.basename(filePath);
311
313
  const viewsDir = opts.views;
312
314
 
313
- // Ensure both paths are absolute for proper relative calculation
314
315
  const absoluteViewsDir = this.#toAbsolutePath(viewsDir);
315
316
  const absoluteFilePath = this.#toAbsolutePath(filePath);
316
317
  const relativePath = path.relative(absoluteViewsDir, absoluteFilePath);
317
318
  const renderedContent = await this.#template(filePath, opts.data, opts);
318
319
  this.#registerOutputPath(relativePath, relativePath, opts.outputDir);
319
- return this.#write(relativePath, renderedContent, opts.outputDir);
320
+ this.#write(relativePath, renderedContent, opts.outputDir);
321
+ return renderedContent;
320
322
  }
321
323
  }
322
324
 
@@ -329,7 +331,8 @@ export class Compile extends ServiceProvider {
329
331
  if (file.exists(route.template) && opts.renderEngine) {
330
332
  const renderedContent = await this.#template(route.template, data, opts);
331
333
  this.#registerOutputPath(relativePath, outputPath, opts.outputDir);
332
- return this.#write(outputPath, renderedContent, opts.outputDir);
334
+ this.#write(outputPath, renderedContent, opts.outputDir);
335
+ return renderedContent;
333
336
  } else {
334
337
  log.warn(`No template compiler configured for route: ${route.path}`);
335
338
  }
@@ -4,6 +4,7 @@ import { viewsToRoutes, pagesToRoutes } 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';
7
+ import { extractScriptSources } from '../utils/extractScripts.js';
7
8
 
8
9
  export class Run extends ServiceProvider {
9
10
  static manifest = {
@@ -11,6 +12,8 @@ export class Run extends ServiceProvider {
11
12
  namespace: 'run',
12
13
  };
13
14
 
15
+ #currentEntryPoints = [];
16
+
14
17
  actions(useContext) {
15
18
  const [configurator, log] = useContext('configurator', 'log');
16
19
 
@@ -94,25 +97,55 @@ export class Run extends ServiceProvider {
94
97
  log.info('🚀 Starting compilation...');
95
98
  const startTime = Date.now();
96
99
 
97
- const promises = [
100
+ // Phase 1: partials must load before templates can render
101
+ await compile.partials(opts);
102
+
103
+ // Phase 2: render views/routes (to extract script refs) + assets/styles in parallel
104
+ const phase2 = [
98
105
  compile.assets(opts),
99
- compile.scripts(opts),
100
106
  compile.styles(opts),
101
- compile.partials(opts),
102
107
  compile.views(opts),
103
108
  ];
104
109
 
105
110
  if (opts.type === 'static') {
106
- promises.push(compile.routes(opts));
111
+ phase2.push(compile.routes(opts));
107
112
  }
108
113
 
109
- const results = await Promise.all(promises);
114
+ const [, , viewHtml, routeHtml] = await Promise.all(phase2);
115
+
116
+ // Phase 3: discover script entry points from rendered HTML, then build
117
+ const allHtml = [...(viewHtml || []), ...(routeHtml || [])];
118
+ await this.#buildScripts(opts, allHtml, compile, log);
119
+
110
120
  const endTime = Date.now();
111
121
  const duration = ((endTime - startTime) / 1000).toFixed(2);
112
122
 
113
123
  log.info(`✅ Build completed in ${duration}s`);
114
-
115
- return results;
124
+ }
125
+
126
+ async #buildScripts(opts, htmlStrings, compile, log) {
127
+ if (!opts.scripts) return;
128
+
129
+ const esBuild = this.useContext('esBuild');
130
+ if (!esBuild) return;
131
+
132
+ const entryPoints = extractScriptSources({
133
+ htmlStrings,
134
+ scriptsDir: opts.scripts,
135
+ viewsDir: opts.views,
136
+ partialsDir: opts.partials,
137
+ routes: opts.routes,
138
+ });
139
+ this.#currentEntryPoints = entryPoints;
140
+
141
+ if (entryPoints.length === 0) {
142
+ log.debug('No script references found in templates, skipping script build');
143
+ return;
144
+ }
145
+
146
+ log.debug(`Discovered ${entryPoints.length} script entry point(s) from templates`);
147
+ esBuild.configure({ entryPoints });
148
+ await compile.scripts(opts);
116
149
  }
117
150
 
118
151
  async #serve (opts) {
@@ -140,13 +173,40 @@ export class Run extends ServiceProvider {
140
173
 
141
174
  const closeWatchers = [];
142
175
 
176
+ const rebuildScriptsIfNeeded = async (htmlStrings) => {
177
+ if (!opts.scripts) return;
178
+
179
+ const esBuild = this.useContext('esBuild');
180
+ if (!esBuild) return;
181
+
182
+ const newEntryPoints = extractScriptSources({
183
+ htmlStrings,
184
+ scriptsDir: opts.scripts,
185
+ viewsDir: opts.views,
186
+ partialsDir: opts.partials,
187
+ routes: opts.routes,
188
+ });
189
+ const changed = newEntryPoints.length !== this.#currentEntryPoints.length ||
190
+ newEntryPoints.some((ep, i) => ep !== this.#currentEntryPoints[i]);
191
+
192
+ if (changed) {
193
+ this.#currentEntryPoints = newEntryPoints;
194
+ if (newEntryPoints.length > 0) {
195
+ log.debug(`Script entry points changed (${newEntryPoints.length}), rebuilding...`);
196
+ esBuild.configure({ entryPoints: newEntryPoints });
197
+ await esBuild.rebuild();
198
+ }
199
+ }
200
+ };
201
+
143
202
  if (opts.partials) {
144
203
  closeWatchers.push(file.watch(opts.partials, async (partialPath) => {
145
- await Promise.all([
146
- compile.partial(partialPath, opts),
204
+ await compile.partial(partialPath, opts);
205
+ const [viewHtml, routeHtml] = await Promise.all([
147
206
  compile.views(opts),
148
- (opts.type === 'static' && compile.routes(opts))
207
+ (opts.type === 'static' ? compile.routes(opts) : Promise.resolve([]))
149
208
  ]);
209
+ await rebuildScriptsIfNeeded([...viewHtml, ...routeHtml]);
150
210
  }));
151
211
  }
152
212
 
@@ -187,9 +247,11 @@ export class Run extends ServiceProvider {
187
247
  log.debug('View changed, recompiling...');
188
248
  switch (type) {
189
249
  case 'change':
190
- case 'add':
191
- await compile.view(viewPath, opts);
250
+ case 'add': {
251
+ const content = await compile.view(viewPath, opts);
252
+ await rebuildScriptsIfNeeded([content].filter(Boolean));
192
253
  break;
254
+ }
193
255
  case 'unlink':
194
256
  compile.remove(viewPath)
195
257
  break;
@@ -0,0 +1,81 @@
1
+ import path from 'path';
2
+ import { existsSync, readFileSync, readdirSync } from 'fs';
3
+
4
+ const SCRIPT_SRC_RE = /<script[^>]+src\s*=\s*["']([^"']+)["'][^>]*>/gi;
5
+ const EXTERNAL_RE = /^(https?:\/\/|\/\/|data:)/i;
6
+
7
+ function addSource(src, scriptsDir, seen, entryPoints) {
8
+ if (EXTERNAL_RE.test(src)) return;
9
+ const sourcePath = path.resolve(scriptsDir, src);
10
+ if (seen.has(sourcePath)) return;
11
+ seen.add(sourcePath);
12
+ if (existsSync(sourcePath)) {
13
+ entryPoints.push(sourcePath);
14
+ }
15
+ }
16
+
17
+ function extractFromString(html, scriptsDir, seen, entryPoints) {
18
+ if (!html) return;
19
+ let match;
20
+ SCRIPT_SRC_RE.lastIndex = 0;
21
+ while ((match = SCRIPT_SRC_RE.exec(html)) !== null) {
22
+ addSource(match[1].trim(), scriptsDir, seen, entryPoints);
23
+ }
24
+ }
25
+
26
+ function readFilesRecursively(dir) {
27
+ const results = [];
28
+ if (!dir || !existsSync(dir)) return results;
29
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
30
+ const full = path.join(dir, entry.name);
31
+ if (entry.isDirectory()) {
32
+ results.push(...readFilesRecursively(full));
33
+ } else {
34
+ results.push(full);
35
+ }
36
+ }
37
+ return results;
38
+ }
39
+
40
+ /**
41
+ * Extract local script source paths from rendered HTML and raw source files.
42
+ * Scans rendered output, plus raw views/partials/route templates for a
43
+ * shotgun approach that catches every possible script reference.
44
+ *
45
+ * @param {object} opts
46
+ * @param {string[]} opts.htmlStrings - Rendered HTML content from views/routes
47
+ * @param {string} opts.scriptsDir - Absolute path to scripts source directory
48
+ * @param {string} [opts.viewsDir] - Absolute path to views directory
49
+ * @param {string} [opts.partialsDir] - Absolute path to partials directory
50
+ * @param {object[]} [opts.routes] - Route config objects with .template paths
51
+ * @returns {string[]} Deduped absolute paths to source script files
52
+ */
53
+ export function extractScriptSources({ htmlStrings = [], scriptsDir, viewsDir, partialsDir, routes = [] }) {
54
+ const seen = new Set();
55
+ const entryPoints = [];
56
+
57
+ for (const html of htmlStrings) {
58
+ extractFromString(html, scriptsDir, seen, entryPoints);
59
+ }
60
+
61
+ const sourceDirs = [viewsDir, partialsDir].filter(Boolean);
62
+ for (const dir of sourceDirs) {
63
+ for (const filePath of readFilesRecursively(dir)) {
64
+ try {
65
+ const content = readFileSync(filePath, 'utf-8');
66
+ extractFromString(content, scriptsDir, seen, entryPoints);
67
+ } catch { /* skip unreadable files */ }
68
+ }
69
+ }
70
+
71
+ for (const route of routes) {
72
+ if (route?.template && existsSync(route.template)) {
73
+ try {
74
+ const content = readFileSync(route.template, 'utf-8');
75
+ extractFromString(content, scriptsDir, seen, entryPoints);
76
+ } catch { /* skip unreadable files */ }
77
+ }
78
+ }
79
+
80
+ return entryPoints;
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clovie",
3
- "version": "0.1.49",
3
+ "version": "0.2.1",
4
4
  "description": "Vintage web dev tooling with modern quality of life",
5
5
  "keywords": [
6
6
  "static-site-generator",
@@ -57,6 +57,7 @@
57
57
  ],
58
58
  "scripts": {
59
59
  "dev": "node ./bin/cli.js watch --config ./__dev__/clovie.config.js",
60
+ "build:dev": "node ./bin/cli.js build --config ./__dev__/clovie.config.js",
60
61
  "test": "vitest run",
61
62
  "test:watch": "vitest",
62
63
  "test:coverage": "vitest --coverage",
@@ -85,7 +86,7 @@
85
86
  },
86
87
  "dependencies": {
87
88
  "@jucie.io/engine": "^1.1.68",
88
- "@jucie.io/engine-esbuild": "^1.0.8",
89
+ "@jucie.io/engine-esbuild": "^1.0.10",
89
90
  "@jucie.io/engine-server": "^1.0.26",
90
91
  "@jucie.io/reactive": "^1.0.30",
91
92
  "chalk": "^5.6.2",