bajo-template 2.5.0 → 2.5.1

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/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import resolveResource, { filecheck } from './lib/resolve-resource.js'
1
+ import resolveResource, { checking } from './lib/resolve-resource.js'
2
2
  import crypto from 'crypto'
3
3
  import path from 'path'
4
4
 
@@ -287,37 +287,10 @@ async function factory (pkgName) {
287
287
  }
288
288
 
289
289
  resolveLayout = (item = '', opts = {}) => {
290
- const { find } = this.app.lib._
291
- const fallbackHandler = ({ file, exts, ns, subSubNs, type, theme }) => {
292
- const dir = ''
293
- const base = 'default'
290
+ const fallbackHandler = ({ type, ns, subSubNs, dir, exts, theme, req }) => {
294
291
  if (!this.config.layout.fallback) return false
295
- // check main: theme specific
296
- if (theme && !file) {
297
- const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${type}/_${theme}`
298
- file = filecheck.call(this, { dir, base, exts, check })
299
- }
300
- // check main: common
301
- if (!file) {
302
- const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${type}`
303
- file = filecheck.call(this, { dir, base, exts, check })
304
- }
305
- if (theme && !file) {
306
- const otheme = find(this.app.waibuMpa.themes, { name: theme })
307
- const check = `${otheme.plugin.dir.pkg}/extend/${this.ns}/extend/${this.ns}/${type}`
308
- file = filecheck.call(this, { dir, base, exts, check })
309
- }
310
- // check fallback: common
311
- if (!file) {
312
- const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${subSubNs ? (subSubNs + '/') : ''}${type}`
313
- file = filecheck.call(this, { dir, base, exts, check })
314
- }
315
- // check general fallback
316
- if (!file) {
317
- const check = `${this.dir.pkg}/extend/${this.ns}/${subSubNs ? (subSubNs + '/') : ''}${type}`
318
- file = filecheck.call(this, { dir, base, exts, check })
319
- }
320
- return file
292
+ const base = 'default'
293
+ return checking.call(this, { type, ns, subSubNs, dir, base, exts, theme, req })
321
294
  }
322
295
 
323
296
  return resolveResource.call(this, 'layout', item, opts, fallbackHandler)
@@ -15,28 +15,12 @@ export function filecheck ({ check, dir, base, exts }) {
15
15
  return file
16
16
  }
17
17
 
18
- function resolveResource (type, item = '', opts = {}, fallbackHandler) {
19
- const { trim, uniq, find } = this.app.lib._
20
- const { fs } = this.app.lib
21
- const env = this.app.bajo.config.env
22
- if (env !== 'dev' && cache[item]) return cache[item]
23
-
24
- let { ns, subSubNs, path } = this.getResource(item)
25
- const ext = _path.extname(path)
26
- path = trim(path, '/')
27
- const dir = _path.dirname(path)
28
- const base = _path.basename(path, ext)
29
- const fallbackLang = this.app.bajo.config.intl.fallback
30
- const exts = uniq([`.${fallbackLang}${ext}`, ext])
31
- if (opts.lang) exts.unshift(`.${opts.lang}${ext}`)
32
- let theme
33
- if (opts.theme && this.app.waibuMpa && opts.req) theme = find(this.app.waibuMpa.themes, { name: opts.theme })
34
-
18
+ export function checking ({ type, ns, subSubNs, dir, base, exts, theme, req } = {}) {
35
19
  const extension = subSubNs ? `${subSubNs}/` : ''
36
20
  let file
37
21
  // check main override: domain specific
38
- if (this.app.waibu && opts.req) {
39
- const hostname = this.app.waibu.getHostname(opts.req)
22
+ if (this.app.waibu && req) {
23
+ const hostname = this.app.waibu.getHostname(req)
40
24
  const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${hostname}/${ns}/${extension}${type}`
41
25
  file = filecheck.call(this, { dir, base, exts, check })
42
26
  }
@@ -70,12 +54,35 @@ function resolveResource (type, item = '', opts = {}, fallbackHandler) {
70
54
  const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${extension}${type}`
71
55
  file = filecheck.call(this, { dir, base, exts, check })
72
56
  }
73
- if (fallbackHandler) file = fallbackHandler.call(this, { file, dir, base, exts, ns, subSubNs, type, theme: opts.theme })
74
- if (opts.default) {
75
- const fname = this.getResource(opts.default).fullPath
76
- if (fs.existsSync(fname)) file = fname
57
+ return file
58
+ }
59
+
60
+ function resolveResource (type, item = '', opts = {}, fallbackHandler) {
61
+ const { trim, find, uniq } = this.app.lib._
62
+ const { fs } = this.app.lib
63
+ const env = this.app.bajo.config.env
64
+ if (env !== 'dev' && cache[item]) return cache[item]
65
+
66
+ let { ns, subSubNs, path } = this.getResource(item)
67
+ const ext = _path.extname(path)
68
+ path = trim(path, '/')
69
+ const dir = _path.dirname(path)
70
+ const base = _path.basename(path, ext)
71
+ const fallbackLang = this.app.bajo.config.intl.fallback
72
+ const exts = uniq([`.${fallbackLang}${ext}`, ext])
73
+ if (opts.lang) exts.unshift(`.${opts.lang}${ext}`)
74
+ let theme
75
+ if (opts.theme && this.app.waibuMpa && opts.req) theme = find(this.app.waibuMpa.themes, { name: opts.theme })
76
+
77
+ let file = checking.call(this, { type, ns, subSubNs, dir, base, exts, theme, req: opts.req })
78
+ if (!file) {
79
+ if (fallbackHandler) file = fallbackHandler.call(this, { dir, base, exts, ns, subSubNs, type, theme: opts.theme, req: opts.req })
80
+ if (opts.default) {
81
+ const fname = this.getResource(opts.default).fullPath
82
+ if (fs.existsSync(fname)) file = fname
83
+ }
84
+ if (!file) throw this.error('cantFind%s%s', type, item)
77
85
  }
78
- if (!file) throw this.error('cantFind%s%s', type, item)
79
86
  const result = { file, ns, layout: opts.layout }
80
87
  if (env !== 'dev') cache[item] = result
81
88
  return result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-02-21
4
+
5
+ - [2.5.1] Bug fix on layout fallback
6
+ - [2.5.1] Refactoring ```resolveResource()``` to be more modular
7
+
3
8
  ## 2026-02-17
4
9
 
5
10
  - [2.5.0] Add ability to extend template by domain