@lifetechocms/react 0.0.0-stage → 0.1.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.
package/vite/index.js ADDED
@@ -0,0 +1,278 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ import { generateEntries} from './GenerateEntries.js';
5
+ import { generateLayoutHtml} from './GenerateLayoutHtml.js';
6
+ import { discoverLayouts} from './DiscoverLayouts.js';
7
+ import { generateLayoutMapping} from './GenerateLayoutMapping.js';
8
+ import { generateThemeMetadata} from './GenerateThemeMetadata.js';
9
+
10
+ export default function lifetechReact(options = {}) {
11
+
12
+ // 1. Plugin options
13
+ const layoutsDirectory =
14
+ options.layoutsDirectory ?? 'src/Layouts';
15
+
16
+ const viewsDirectory =
17
+ options.viewsDirectory ?? 'src/Views';
18
+
19
+ const themeName = options.themeName;
20
+
21
+ if (
22
+ !themeName ||
23
+ !/^[A-Za-z][A-Za-z0-9_-]*$/.test(themeName)
24
+ ) {
25
+ throw new Error(
26
+ 'LifeTechOCMS: Provide a valid themeName in Vite configuration.'
27
+ );
28
+ }
29
+ // Activate LifeTech generation only during lt-export.
30
+ if (process.env.LIFETECH_BUILD !== 'true') {
31
+ return {
32
+ name: 'lifetechocms-react'
33
+ };
34
+ }
35
+ // 2. Virtual module identifiers
36
+ const viewsVirtualId = 'virtual:lifetech-views';
37
+ const resolvedViewsId = '\0' + viewsVirtualId;
38
+
39
+ // 3. Plugin variables
40
+ let projectRoot;
41
+ let buildOutputDirectory;
42
+ let discoveredLayouts = {};
43
+ let virtualModules = {};
44
+
45
+ return {
46
+ name: 'lifetechocms-react',
47
+
48
+ enforce: 'pre',
49
+
50
+ // 4. Discover Layouts and generate Vite entries
51
+ config(config) {
52
+ projectRoot = path.resolve(
53
+ config.root || process.cwd()
54
+ );
55
+
56
+ // Reset virtual modules for this build.
57
+ virtualModules = {};
58
+
59
+ const layouts = discoverLayouts(
60
+ projectRoot,
61
+ layoutsDirectory
62
+ );
63
+
64
+ // Generate Default Layout if none exists.
65
+ if (Object.keys(layouts).length === 0) {
66
+ const defaultLayoutId =
67
+ 'virtual:lifetech-default-layout';
68
+
69
+ layouts.Default = defaultLayoutId;
70
+
71
+ virtualModules['\0' + defaultLayoutId] = `
72
+ import React from 'react';
73
+
74
+ export default function Default({ children }) {
75
+ return React.createElement(
76
+ React.Fragment,
77
+ null,
78
+ children
79
+ );
80
+ }
81
+ `;
82
+
83
+ console.log(
84
+ '[LifeTechOCMS] Using generated Default Layout.'
85
+ );
86
+ }
87
+
88
+ // Retain Layouts for HTML generation.
89
+ discoveredLayouts = layouts;
90
+
91
+ const pluginRoot = path.resolve(
92
+ import.meta.dirname,
93
+ '..'
94
+ );
95
+
96
+ const generated = generateEntries(
97
+ layouts,
98
+ pluginRoot,
99
+ projectRoot,
100
+ viewsDirectory
101
+ );
102
+
103
+ virtualModules = {
104
+ ...virtualModules,
105
+ ...generated.modules
106
+ };
107
+
108
+ return {
109
+ build: {
110
+ manifest: true,
111
+ assetsDir: 'Assets',
112
+
113
+ rolldownOptions: {
114
+ input: generated.entries,
115
+
116
+ output: {
117
+ entryFileNames: 'Assets/js/[name].js',
118
+
119
+ chunkFileNames:
120
+ 'Assets/js/LtChunk-[name].js',
121
+
122
+ assetFileNames: assetInfo => {
123
+ const name =
124
+ assetInfo.names?.[0] ?? '';
125
+
126
+ if (name.endsWith('.css')) {
127
+ return 'Assets/css/[name][extname]';
128
+ }
129
+
130
+ return 'Assets/[name][extname]';
131
+ }
132
+ }
133
+ }
134
+ }
135
+ };
136
+ },
137
+
138
+ // 5. Resolve the final build directory.
139
+
140
+ configResolved(config) {
141
+ buildOutputDirectory = path.resolve(
142
+ config.root,
143
+ config.build.outDir
144
+ );
145
+
146
+ console.log(
147
+ '[LifeTechOCMS] Output directory:',
148
+ buildOutputDirectory
149
+ );
150
+
151
+ console.log(
152
+ '[LifeTechOCMS] Manifest enabled:',
153
+ config.build.manifest
154
+ );
155
+ },
156
+
157
+ // 6. Resolve virtual modules.
158
+ resolveId(id) {
159
+ if (id === viewsVirtualId) {
160
+ return resolvedViewsId;
161
+ }
162
+
163
+ if (Object.hasOwn(virtualModules, id)) {
164
+ return id;
165
+ }
166
+
167
+ const resolvedId = '\0' + id;
168
+
169
+ if (Object.hasOwn(virtualModules, resolvedId)) {
170
+ return resolvedId;
171
+ }
172
+ },
173
+
174
+ // 7. Load virtual modules.
175
+ load(id) {
176
+ if (id === resolvedViewsId) {
177
+ // Discover nested Views in the configured directory.
178
+ // The glob is evaluated by Vite.
179
+ const directory = viewsDirectory
180
+ .replace(/\\/g, '/')
181
+ .replace(/^\/+|\/+$/g, '');
182
+
183
+ const pattern =
184
+ `/${directory}/**/*.{js,jsx,ts,tsx}`;
185
+
186
+ return `
187
+ const modules = import.meta.glob(
188
+ ${JSON.stringify(pattern)},
189
+ { eager: true }
190
+ );
191
+
192
+ const views = {};
193
+
194
+ for (
195
+ const [file, module]
196
+ of Object.entries(modules)
197
+ ) {
198
+ const relativePath = file
199
+ .replace(
200
+ ${JSON.stringify(`/${directory}/`)},
201
+ ''
202
+ )
203
+ .replace(/\\.(js|jsx|ts|tsx)$/, '');
204
+
205
+ views[relativePath] = module.default;
206
+ }
207
+
208
+ export default views;
209
+ `;
210
+ }
211
+
212
+ return virtualModules[id];
213
+ },
214
+
215
+ // 8. Generate LifeTech HTML and metadata.
216
+ /*
217
+ async closeBundle() {
218
+ generateLayoutHtml({
219
+ outputDirectory: buildOutputDirectory,
220
+ projectRoot,
221
+ viewsDirectory,
222
+ layouts: discoveredLayouts,
223
+ themeName
224
+ });
225
+
226
+ await generateLayoutMapping({
227
+ projectRoot,
228
+ outputDirectory: buildOutputDirectory,
229
+ themeName
230
+ });
231
+
232
+ generateThemeMetadata({
233
+ outputDirectory: buildOutputDirectory,
234
+ themeName
235
+ });
236
+ }
237
+ */
238
+ // 8. Generate LifeTech HTML and metadata.
239
+
240
+ // 8. Generate LifeTech HTML and metadata.
241
+ async closeBundle() {
242
+ const manifestPath = path.join(
243
+ buildOutputDirectory,
244
+ '.vite',
245
+ 'manifest.json'
246
+ );
247
+
248
+ if (!fs.existsSync(manifestPath)) {
249
+ throw new Error(
250
+ 'LifeTechOCMS: Vite manifest not found. ' +
251
+ 'Theme generation cannot continue.'
252
+ );
253
+ }
254
+
255
+ generateLayoutHtml({
256
+ outputDirectory: buildOutputDirectory,
257
+ projectRoot,
258
+ viewsDirectory,
259
+ layouts: discoveredLayouts,
260
+ themeName
261
+ });
262
+
263
+ await generateLayoutMapping({
264
+ projectRoot,
265
+ outputDirectory: buildOutputDirectory,
266
+ themeName
267
+ });
268
+
269
+ generateThemeMetadata({
270
+ outputDirectory: buildOutputDirectory,
271
+ themeName
272
+ });
273
+ }
274
+
275
+
276
+
277
+ };
278
+ }