@lifetechocms/react 0.0.0-stage → 0.1.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/vite/index.js ADDED
@@ -0,0 +1,365 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ //import { generateEntries} from './GenerateEntries.js';
5
+ import { generateEntries, discoverReactFiles,getRelativeName} from './GenerateEntries.js';
6
+ import { generateLayoutHtml} from './GenerateLayoutHtml.js';
7
+ import { discoverLayouts} from './DiscoverLayouts.js';
8
+ import { generateLayoutMapping} from './GenerateLayoutMapping.js';
9
+ import { generateThemeMetadata} from './GenerateThemeMetadata.js';
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
+
48
+ export default function lifetechReact(options = {}) {
49
+
50
+ // 1. Plugin options
51
+ const layoutsDirectory =
52
+ options.layoutsDirectory ?? 'src/Layouts';
53
+
54
+ const viewsDirectory =
55
+ options.viewsDirectory ?? 'src/Views';
56
+
57
+ const themeName = options.themeName;
58
+
59
+ if (
60
+ !themeName ||
61
+ !/^[A-Za-z][A-Za-z0-9_-]*$/.test(themeName)
62
+ ) {
63
+ throw new Error(
64
+ 'LifeTechOCMS: Provide a valid themeName in Vite configuration.'
65
+ );
66
+ }
67
+ // Activate LifeTech generation only during lt-export.
68
+ if (process.env.LIFETECH_BUILD !== 'true') {
69
+ return {
70
+ name: 'lifetechocms-react'
71
+ };
72
+ }
73
+ // 2. Virtual module identifiers
74
+ const viewsVirtualId = 'virtual:lifetech-views';
75
+ const resolvedViewsId = '\0' + viewsVirtualId;
76
+
77
+ // 3. Plugin variables
78
+ let projectRoot;
79
+ let buildOutputDirectory;
80
+ let discoveredLayouts = {};
81
+ let discoveredViews = {};
82
+ let virtualModules = {};
83
+
84
+ return {
85
+ name: 'lifetechocms-react',
86
+
87
+ enforce: 'pre',
88
+
89
+ // 4. Discover Layouts and generate Vite entries
90
+ config(config) {
91
+ projectRoot = path.resolve(
92
+ config.root || process.cwd()
93
+ );
94
+
95
+ // Reset virtual modules for this build.
96
+ virtualModules = {};
97
+
98
+ const layouts = discoverLayouts(
99
+ projectRoot,
100
+ layoutsDirectory
101
+ );
102
+
103
+ // Generate Default Layout if none exists.
104
+ if (Object.keys(layouts).length === 0) {
105
+ const defaultLayoutId =
106
+ 'virtual:lifetech-default-layout';
107
+
108
+ layouts.Default = defaultLayoutId;
109
+
110
+ virtualModules['\0' + defaultLayoutId] = `
111
+ import React from 'react';
112
+
113
+ export default function Default({ children }) {
114
+ return React.createElement(
115
+ React.Fragment,
116
+ null,
117
+ children
118
+ );
119
+ }
120
+ `;
121
+
122
+ console.log(
123
+ '[LifeTechOCMS] Using generated Default Layout.'
124
+ );
125
+ }
126
+
127
+ // Retain Layouts for HTML generation.
128
+ discoveredLayouts = layouts;
129
+
130
+ const viewsPath = path.resolve(
131
+ projectRoot,
132
+ viewsDirectory
133
+ );
134
+
135
+ const viewFiles = discoverReactFiles(viewsPath);
136
+
137
+ discoveredViews = Object.fromEntries(
138
+ viewFiles.map(file => [
139
+ getRelativeName(viewsPath, file),
140
+ file
141
+ ])
142
+ );
143
+
144
+ const pluginRoot = path.resolve(
145
+ import.meta.dirname,
146
+ '..'
147
+ );
148
+
149
+ const generated = generateEntries(
150
+ layouts,
151
+ pluginRoot,
152
+ projectRoot,
153
+ viewsDirectory
154
+ );
155
+
156
+ virtualModules = {
157
+ ...virtualModules,
158
+ ...generated.modules
159
+ };
160
+
161
+ return {
162
+ build: {
163
+ manifest: true,
164
+ assetsDir: 'Assets',
165
+
166
+ rolldownOptions: {
167
+ input: generated.entries,
168
+
169
+ output: {
170
+ entryFileNames: 'Assets/js/[name].js',
171
+
172
+ chunkFileNames:
173
+ 'Assets/js/LtChunk-[name].js',
174
+
175
+ assetFileNames: assetInfo => {
176
+ const name =
177
+ assetInfo.names?.[0] ?? '';
178
+
179
+ if (name.endsWith('.css')) {
180
+ return 'Assets/css/[name][extname]';
181
+ }
182
+
183
+ return 'Assets/[name][extname]';
184
+ }
185
+ }
186
+ }
187
+ }
188
+ };
189
+ },
190
+
191
+ // 5. Resolve the final build directory.
192
+
193
+ configResolved(config) {
194
+ buildOutputDirectory = path.resolve(
195
+ config.root,
196
+ config.build.outDir
197
+ );
198
+
199
+ console.log(
200
+ '[LifeTechOCMS] Output directory:',
201
+ buildOutputDirectory
202
+ );
203
+
204
+ console.log(
205
+ '[LifeTechOCMS] Manifest enabled:',
206
+ config.build.manifest
207
+ );
208
+ },
209
+
210
+ // 6. Validate React Layouts and Views.
211
+ buildStart() {
212
+ console.log(
213
+ '[LifeTechOCMS] Validating React Layouts and Views...'
214
+ );
215
+
216
+ validateReactComponents(
217
+ discoveredLayouts,
218
+ 'Layout'
219
+ );
220
+
221
+ validateReactComponents(
222
+ discoveredViews,
223
+ 'View'
224
+ );
225
+
226
+ console.log(
227
+ '[LifeTechOCMS] React component validation passed.'
228
+ );
229
+ },
230
+
231
+ // 6. Resolve virtual modules.
232
+ resolveId(id) {
233
+ if (id === viewsVirtualId) {
234
+ return resolvedViewsId;
235
+ }
236
+
237
+ if (Object.hasOwn(virtualModules, id)) {
238
+ return id;
239
+ }
240
+
241
+ const resolvedId = '\0' + id;
242
+
243
+ if (Object.hasOwn(virtualModules, resolvedId)) {
244
+ return resolvedId;
245
+ }
246
+ },
247
+
248
+ // 7. Load virtual modules.
249
+ load(id) {
250
+ if (id === resolvedViewsId) {
251
+ // Discover nested Views in the configured directory.
252
+ // The glob is evaluated by Vite.
253
+ const directory = viewsDirectory
254
+ .replace(/\\/g, '/')
255
+ .replace(/^\/+|\/+$/g, '');
256
+
257
+ const pattern =
258
+ `/${directory}/**/*.{js,jsx,ts,tsx}`;
259
+
260
+ return `
261
+ const modules = import.meta.glob(
262
+ ${JSON.stringify(pattern)},
263
+ { eager: true }
264
+ );
265
+
266
+ const views = {};
267
+
268
+ for (
269
+ const [file, module]
270
+ of Object.entries(modules)
271
+ ) {
272
+ const relativePath = file
273
+ .replace(
274
+ ${JSON.stringify(`/${directory}/`)},
275
+ ''
276
+ )
277
+ .replace(/\\.(js|jsx|ts|tsx)$/, '');
278
+
279
+ views[relativePath] = module.default;
280
+ }
281
+
282
+ export default views;
283
+ `;
284
+ }
285
+
286
+ return virtualModules[id];
287
+ },
288
+
289
+ // 8. Generate LifeTech HTML and metadata.
290
+ /*
291
+ // 8. Generate LifeTech HTML and metadata.
292
+ async closeBundle() {
293
+ const manifestPath = path.join(
294
+ buildOutputDirectory,
295
+ '.vite',
296
+ 'manifest.json'
297
+ );
298
+
299
+ if (!fs.existsSync(manifestPath)) {
300
+ throw new Error(
301
+ 'LifeTechOCMS: Vite manifest not found. ' +
302
+ 'Theme generation cannot continue.'
303
+ );
304
+ }
305
+
306
+ generateLayoutHtml({
307
+ outputDirectory: buildOutputDirectory,
308
+ projectRoot,
309
+ viewsDirectory,
310
+ layouts: discoveredLayouts,
311
+ themeName
312
+ });
313
+
314
+ await generateLayoutMapping({
315
+ projectRoot,
316
+ outputDirectory: buildOutputDirectory,
317
+ themeName
318
+ });
319
+
320
+ generateThemeMetadata({
321
+ outputDirectory: buildOutputDirectory,
322
+ themeName
323
+ });
324
+ }*/
325
+
326
+
327
+ async writeBundle() {
328
+ const manifestPath = path.join(
329
+ buildOutputDirectory,
330
+ '.vite',
331
+ 'manifest.json'
332
+ );
333
+
334
+ if (!fs.existsSync(manifestPath)) {
335
+ throw new Error(
336
+ 'LifeTechOCMS: Vite manifest not found. ' +
337
+ 'Theme generation cannot continue.'
338
+ );
339
+ }
340
+
341
+ generateLayoutHtml({
342
+ outputDirectory: buildOutputDirectory,
343
+ projectRoot,
344
+ viewsDirectory,
345
+ layouts: discoveredLayouts,
346
+ themeName
347
+ });
348
+
349
+ await generateLayoutMapping({
350
+ projectRoot,
351
+ outputDirectory: buildOutputDirectory,
352
+ themeName
353
+ });
354
+
355
+ generateThemeMetadata({
356
+ outputDirectory: buildOutputDirectory,
357
+ themeName
358
+ });
359
+ },
360
+
361
+
362
+
363
+
364
+ };
365
+ }