metaowl 0.2.12 → 0.2.14
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 +46 -2
- package/package.json +1 -1
- package/vite/plugin.js +30 -10
|
@@ -2,17 +2,52 @@
|
|
|
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
|
+
// Skip in test environment (Vitest doesn't have the same import.meta behavior)
|
|
15
|
+
// Also skip if we're not in a browser environment
|
|
16
|
+
if (typeof window === 'undefined' && typeof globalThis.importMeta === 'undefined') {
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
// In production (build), templates are inlined via Vite plugin
|
|
22
|
+
// Use dynamic import with a path that Vite can resolve at runtime
|
|
23
|
+
// The templates.js is generated in the output directory
|
|
24
|
+
// Using eval to prevent Vite's static analysis from failing
|
|
25
|
+
const templatesModule = await new Function('return import("/templates.js")')()
|
|
26
|
+
return templatesModule.TEMPLATES
|
|
27
|
+
} catch (e) {
|
|
28
|
+
// In development or legacy setup, templates are loaded at runtime
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
8
33
|
/**
|
|
9
34
|
* Loads and concatenates a list of OWL XML template files into a single
|
|
10
35
|
* `<templates>` string ready to be passed to OWL's mount() options.
|
|
11
36
|
*
|
|
12
|
-
*
|
|
37
|
+
* If inlined templates are available (from build time), those are used directly.
|
|
38
|
+
* Otherwise, falls back to runtime loading of individual XML files.
|
|
39
|
+
*
|
|
40
|
+
* @param {string[]} [files] - Array of URL-style XML paths (ignored if inlined templates exist)
|
|
13
41
|
* @returns {Promise<string>}
|
|
14
42
|
*/
|
|
15
|
-
export async function mergeTemplates(files) {
|
|
43
|
+
export async function mergeTemplates(files = []) {
|
|
44
|
+
// Try to get inlined templates first (production build)
|
|
45
|
+
const inlined = await getInlinedTemplates()
|
|
46
|
+
if (inlined) {
|
|
47
|
+
return inlined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Fallback: load templates at runtime (development or legacy)
|
|
16
51
|
let templates = '<templates>'
|
|
17
52
|
for (const file of files) {
|
|
18
53
|
try {
|
|
@@ -23,3 +58,12 @@ export async function mergeTemplates(files) {
|
|
|
23
58
|
}
|
|
24
59
|
return templates + '</templates>'
|
|
25
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check if inlined templates are available.
|
|
64
|
+
* @returns {Promise<boolean>}
|
|
65
|
+
*/
|
|
66
|
+
export async function hasInlinedTemplates() {
|
|
67
|
+
const templates = await getInlinedTemplates()
|
|
68
|
+
return templates !== null
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metaowl",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
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,16 +219,36 @@ export async function metaowlPlugin(options = {}) {
|
|
|
219
219
|
closeBundle() {
|
|
220
220
|
const projectRoot = process.cwd()
|
|
221
221
|
|
|
222
|
-
//
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
222
|
+
// Collect and inline all XML templates into a single JS file
|
|
223
|
+
// Use already collected XML arrays to avoid duplicate glob operations
|
|
224
|
+
const xmlFiles = [
|
|
225
|
+
...layoutXml,
|
|
226
|
+
...pageXml,
|
|
227
|
+
...componentXml
|
|
228
|
+
]
|
|
229
|
+
|
|
230
|
+
let templates = '<templates>'
|
|
231
|
+
for (const file of xmlFiles) {
|
|
232
|
+
try {
|
|
233
|
+
templates += readFileSync(file, 'utf-8')
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.warn(`[metaowl] Failed to read template: ${file}`)
|
|
236
|
+
}
|
|
229
237
|
}
|
|
238
|
+
templates += '</templates>'
|
|
239
|
+
|
|
240
|
+
// Escape for JavaScript string using JSON encoding for safety
|
|
241
|
+
const escaped = JSON.stringify(templates).slice(1, -1)
|
|
242
|
+
|
|
243
|
+
// Write templates.js to the output root (accessible as /templates.js)
|
|
244
|
+
const templatesJs = `export const TEMPLATES = '${escaped}';\n`
|
|
245
|
+
const templatesDir = _outDirResolved
|
|
246
|
+
mkdirSync(templatesDir, { recursive: true })
|
|
247
|
+
writeFileSync(resolve(templatesDir, 'templates.js'), templatesJs)
|
|
248
|
+
|
|
249
|
+
console.log(`[metaowl] Inlined ${xmlFiles.length} XML templates into templates.js`)
|
|
230
250
|
|
|
231
|
-
// Copy assets/images (referenced via absolute URLs in XML
|
|
251
|
+
// Restore: Copy assets/images (referenced via absolute URLs in XML)
|
|
232
252
|
const srcImages = resolve(projectRoot, root, 'assets', 'images')
|
|
233
253
|
if (existsSync(srcImages)) {
|
|
234
254
|
cpSync(srcImages, resolve(_outDirResolved, 'assets', 'images'), { recursive: true })
|