@lifetechocms/react 0.1.4 → 0.1.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifetechocms/react",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "React and Vite integration for building and exporting LifeTechOCMS themes.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -163,6 +163,16 @@ export function generateEntries(
163
163
  entries[entryName] = resolvedId;
164
164
 
165
165
  modules[resolvedId] = `
166
+ // LifeTechOCMS View: ${relativeName}
167
+ // Source: ${normalize(file)}
168
+ //
169
+ // This View must provide a default export.
170
+ //
171
+ // Example:
172
+ // export default function Home() {
173
+ // return <h1>Welcome</h1>;
174
+ // }
175
+
166
176
  import View from ${JSON.stringify(
167
177
  normalize(file)
168
178
  )};
@@ -180,6 +190,7 @@ export function generateEntries(
180
190
  mountLifeTechView(element, View);
181
191
  });
182
192
  `;
193
+
183
194
  }
184
195
 
185
196
  return {
package/vite/index.js CHANGED
@@ -1,12 +1,50 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
 
4
- import { generateEntries} from './GenerateEntries.js';
4
+ //import { generateEntries} from './GenerateEntries.js';
5
+ import { generateEntries, discoverReactFiles,getRelativeName} from './GenerateEntries.js';
5
6
  import { generateLayoutHtml} from './GenerateLayoutHtml.js';
6
7
  import { discoverLayouts} from './DiscoverLayouts.js';
7
8
  import { generateLayoutMapping} from './GenerateLayoutMapping.js';
8
9
  import { generateThemeMetadata} from './GenerateThemeMetadata.js';
9
10
 
11
+
12
+
13
+
14
+ function validateReactComponents(files, type) {
15
+ for (const [name, filePath] of Object.entries(files)) {
16
+ // Skip virtual modules generated by LifeTechOCMS.
17
+ if (filePath.startsWith('virtual:')) {
18
+ continue;
19
+ }
20
+
21
+ const source = fs.readFileSync(filePath, 'utf8');
22
+
23
+ // Check common default-export syntaxes.
24
+ const hasDefaultExport =
25
+ /\bexport\s+default\b/.test(source) ||
26
+ /\bexport\s*\{[^}]*\bas\s+default\b[^}]*\}/s.test(source);
27
+
28
+ if (!hasDefaultExport) {
29
+ throw new Error(
30
+ '\n[LifeTechOCMS] Invalid React ' + type + '\n' +
31
+ 'Component: ' + name + '\n' +
32
+ 'File: ' + filePath + '\n\n' +
33
+ 'Reason: No default export was found.\n\n' +
34
+ 'Every LifeTechOCMS React ' + type +
35
+ ' must export a default component.\n\n' +
36
+ 'Example:\n' +
37
+ 'export default function ' +
38
+ (type === 'View' ? 'Home' : 'Default') +
39
+ '() {\n' +
40
+ ' return <h1>Welcome</h1>;\n' +
41
+ '}\n'
42
+ );
43
+ }
44
+ }
45
+ }
46
+
47
+
10
48
  export default function lifetechReact(options = {}) {
11
49
 
12
50
  // 1. Plugin options
@@ -26,12 +64,40 @@ export default function lifetechReact(options = {}) {
26
64
  'LifeTechOCMS: Provide a valid themeName in Vite configuration.'
27
65
  );
28
66
  }
67
+ /*
29
68
  // Activate LifeTech generation only during lt-export.
30
69
  if (process.env.LIFETECH_BUILD !== 'true') {
31
70
  return {
32
71
  name: 'lifetechocms-react'
33
72
  };
34
73
  }
74
+ */
75
+
76
+ // Directories generated by the LifeTechOCMS exporter.
77
+ // Exclude them from the Vite development file watcher.
78
+ const ignoredExportDirectories = [
79
+ '**/lt-exports/**',
80
+ '**/dist-lifetech/**'
81
+ ];
82
+
83
+ // Activate LifeTech generation only during lt-export.
84
+ if (process.env.LIFETECH_BUILD !== 'true') {
85
+ return {
86
+ name: 'lifetechocms-react',
87
+
88
+ config() {
89
+ return {
90
+ server: {
91
+ watch: {
92
+ ignored: ignoredExportDirectories
93
+ }
94
+ }
95
+ };
96
+ }
97
+ };
98
+ }
99
+
100
+
35
101
  // 2. Virtual module identifiers
36
102
  const viewsVirtualId = 'virtual:lifetech-views';
37
103
  const resolvedViewsId = '\0' + viewsVirtualId;
@@ -40,6 +106,7 @@ export default function lifetechReact(options = {}) {
40
106
  let projectRoot;
41
107
  let buildOutputDirectory;
42
108
  let discoveredLayouts = {};
109
+ let discoveredViews = {};
43
110
  let virtualModules = {};
44
111
 
45
112
  return {
@@ -88,6 +155,20 @@ export default function lifetechReact(options = {}) {
88
155
  // Retain Layouts for HTML generation.
89
156
  discoveredLayouts = layouts;
90
157
 
158
+ const viewsPath = path.resolve(
159
+ projectRoot,
160
+ viewsDirectory
161
+ );
162
+
163
+ const viewFiles = discoverReactFiles(viewsPath);
164
+
165
+ discoveredViews = Object.fromEntries(
166
+ viewFiles.map(file => [
167
+ getRelativeName(viewsPath, file),
168
+ file
169
+ ])
170
+ );
171
+
91
172
  const pluginRoot = path.resolve(
92
173
  import.meta.dirname,
93
174
  '..'
@@ -154,6 +235,27 @@ export default function lifetechReact(options = {}) {
154
235
  );
155
236
  },
156
237
 
238
+ // 6. Validate React Layouts and Views.
239
+ buildStart() {
240
+ console.log(
241
+ '[LifeTechOCMS] Validating React Layouts and Views...'
242
+ );
243
+
244
+ validateReactComponents(
245
+ discoveredLayouts,
246
+ 'Layout'
247
+ );
248
+
249
+ validateReactComponents(
250
+ discoveredViews,
251
+ 'View'
252
+ );
253
+
254
+ console.log(
255
+ '[LifeTechOCMS] React component validation passed.'
256
+ );
257
+ },
258
+
157
259
  // 6. Resolve virtual modules.
158
260
  resolveId(id) {
159
261
  if (id === viewsVirtualId) {
@@ -214,7 +316,21 @@ export default function lifetechReact(options = {}) {
214
316
 
215
317
  // 8. Generate LifeTech HTML and metadata.
216
318
  /*
319
+ // 8. Generate LifeTech HTML and metadata.
217
320
  async closeBundle() {
321
+ const manifestPath = path.join(
322
+ buildOutputDirectory,
323
+ '.vite',
324
+ 'manifest.json'
325
+ );
326
+
327
+ if (!fs.existsSync(manifestPath)) {
328
+ throw new Error(
329
+ 'LifeTechOCMS: Vite manifest not found. ' +
330
+ 'Theme generation cannot continue.'
331
+ );
332
+ }
333
+
218
334
  generateLayoutHtml({
219
335
  outputDirectory: buildOutputDirectory,
220
336
  projectRoot,
@@ -233,12 +349,10 @@ export default function lifetechReact(options = {}) {
233
349
  outputDirectory: buildOutputDirectory,
234
350
  themeName
235
351
  });
236
- }
237
- */
238
- // 8. Generate LifeTech HTML and metadata.
239
-
240
- // 8. Generate LifeTech HTML and metadata.
241
- async closeBundle() {
352
+ }*/
353
+
354
+
355
+ async writeBundle() {
242
356
  const manifestPath = path.join(
243
357
  buildOutputDirectory,
244
358
  '.vite',
@@ -270,7 +384,8 @@ export default function lifetechReact(options = {}) {
270
384
  outputDirectory: buildOutputDirectory,
271
385
  themeName
272
386
  });
273
- }
387
+ },
388
+
274
389
 
275
390
 
276
391