metaowl 0.2.12 → 0.2.13
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/modules/templates-manager.js +37 -2
- package/package.json +1 -1
- package/vite/plugin.js +31 -14
|
@@ -2,17 +2,43 @@
|
|
|
2
2
|
* @module TemplatesManager
|
|
3
3
|
*
|
|
4
4
|
* Template loading and merging utilities for OWL applications.
|
|
5
|
+
* Supports both runtime loading (legacy) and build-time inlined templates.
|
|
5
6
|
*/
|
|
6
7
|
import { loadFile } from '@odoo/owl'
|
|
7
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Try to import inlined templates from build time.
|
|
11
|
+
* Returns null if not available (dev mode without inline plugin or legacy setup).
|
|
12
|
+
*/
|
|
13
|
+
async function getInlinedTemplates() {
|
|
14
|
+
try {
|
|
15
|
+
// In production (build), templates are inlined via Vite plugin
|
|
16
|
+
const { TEMPLATES } = await import('/src/templates.js')
|
|
17
|
+
return TEMPLATES
|
|
18
|
+
} catch (e) {
|
|
19
|
+
// In development or legacy setup, templates are loaded at runtime
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
/**
|
|
9
25
|
* Loads and concatenates a list of OWL XML template files into a single
|
|
10
26
|
* `<templates>` string ready to be passed to OWL's mount() options.
|
|
11
27
|
*
|
|
12
|
-
*
|
|
28
|
+
* If inlined templates are available (from build time), those are used directly.
|
|
29
|
+
* Otherwise, falls back to runtime loading of individual XML files.
|
|
30
|
+
*
|
|
31
|
+
* @param {string[]} [files] - Array of URL-style XML paths (ignored if inlined templates exist)
|
|
13
32
|
* @returns {Promise<string>}
|
|
14
33
|
*/
|
|
15
|
-
export async function mergeTemplates(files) {
|
|
34
|
+
export async function mergeTemplates(files = []) {
|
|
35
|
+
// Try to get inlined templates first (production build)
|
|
36
|
+
const inlined = await getInlinedTemplates()
|
|
37
|
+
if (inlined) {
|
|
38
|
+
return inlined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Fallback: load templates at runtime (development or legacy)
|
|
16
42
|
let templates = '<templates>'
|
|
17
43
|
for (const file of files) {
|
|
18
44
|
try {
|
|
@@ -23,3 +49,12 @@ export async function mergeTemplates(files) {
|
|
|
23
49
|
}
|
|
24
50
|
return templates + '</templates>'
|
|
25
51
|
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Check if inlined templates are available.
|
|
55
|
+
* @returns {Promise<boolean>}
|
|
56
|
+
*/
|
|
57
|
+
export async function hasInlinedTemplates() {
|
|
58
|
+
const templates = await getInlinedTemplates()
|
|
59
|
+
return templates !== null
|
|
60
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metaowl",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "Lightweight meta-framework for Odoo OWL — file-based routing, app mounting, Fetch helper, Cache, Meta tags, SSG generator, and a Vite plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
package/vite/plugin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url'
|
|
2
2
|
import { resolve, dirname } from 'node:path'
|
|
3
|
-
import { mkdirSync, copyFileSync, cpSync, existsSync } from 'node:fs'
|
|
3
|
+
import { mkdirSync, copyFileSync, cpSync, existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
4
4
|
import { createRequire } from 'node:module'
|
|
5
5
|
import { globSync } from 'glob'
|
|
6
6
|
import { config as dotenvConfig } from 'dotenv'
|
|
@@ -63,7 +63,7 @@ export async function metaowlPlugin(options = {}) {
|
|
|
63
63
|
const componentXml = collectXml(`${componentsDir}/**/*.xml`)
|
|
64
64
|
const pageXml = collectXml(`${pagesDir}/**/*.xml`)
|
|
65
65
|
const layoutXml = collectXml(`${layoutsDir}/**/*.xml`)
|
|
66
|
-
const allComponents = [...
|
|
66
|
+
const allComponents = [...layoutXml, ...pageXml, ...componentXml]
|
|
67
67
|
|
|
68
68
|
const defaultRestartGlobs = [
|
|
69
69
|
`${root}/**/*.[jt]s`,
|
|
@@ -219,20 +219,37 @@ export async function metaowlPlugin(options = {}) {
|
|
|
219
219
|
closeBundle() {
|
|
220
220
|
const projectRoot = process.cwd()
|
|
221
221
|
|
|
222
|
-
//
|
|
223
|
-
const xmlFiles =
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
copyFileSync(resolve(projectRoot, xmlFile), dest)
|
|
229
|
-
}
|
|
222
|
+
// Collect and inline all XML templates into a single JS file
|
|
223
|
+
const xmlFiles = [
|
|
224
|
+
...globSync(`${root}/${layoutsDir}/**/*.xml`),
|
|
225
|
+
...globSync(`${root}/${pagesDir}/**/*.xml`),
|
|
226
|
+
...globSync(`${root}/${componentsDir}/**/*.xml`)
|
|
227
|
+
]
|
|
230
228
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
229
|
+
let templates = '<templates>'
|
|
230
|
+
for (const file of xmlFiles) {
|
|
231
|
+
try {
|
|
232
|
+
templates += readFileSync(file, 'utf-8')
|
|
233
|
+
} catch (e) {
|
|
234
|
+
console.warn(`[metaowl] Failed to read template: ${file}`)
|
|
235
|
+
}
|
|
235
236
|
}
|
|
237
|
+
templates += '</templates>'
|
|
238
|
+
|
|
239
|
+
// Escape for JavaScript string
|
|
240
|
+
const escaped = templates
|
|
241
|
+
.replace(/\\/g, '\\\\')
|
|
242
|
+
.replace(/'/g, "\\'")
|
|
243
|
+
.replace(/\n/g, '\\n')
|
|
244
|
+
.replace(/\r/g, '')
|
|
245
|
+
|
|
246
|
+
// Write templates.js
|
|
247
|
+
const templatesJs = `export const TEMPLATES = '${escaped}';\n`
|
|
248
|
+
const templatesDir = resolve(_outDirResolved, root)
|
|
249
|
+
mkdirSync(templatesDir, { recursive: true })
|
|
250
|
+
writeFileSync(resolve(templatesDir, 'templates.js'), templatesJs)
|
|
251
|
+
|
|
252
|
+
console.log(`[metaowl] Inlined ${xmlFiles.length} XML templates into templates.js`)
|
|
236
253
|
}
|
|
237
254
|
}
|
|
238
255
|
]
|