bajo-template 1.2.0 → 1.2.2

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.
Files changed (2) hide show
  1. package/index.js +22 -10
  2. package/package.json +4 -1
package/index.js CHANGED
@@ -88,12 +88,16 @@ async function factory (pkgName) {
88
88
  const { fs } = this.lib
89
89
  const { breakNsPath } = this.app.bajo
90
90
 
91
- const { subNs } = breakNsPath(tpl)
92
91
  let resp
93
- if (subNs === 'template') {
94
- resp = this.resolveTemplate(tpl, opts)
95
- } else if (subNs === 'partial') {
96
- resp = this.resolvePartial(tpl, opts)
92
+ let subNs
93
+ if (path.isAbsolute(tpl)) resp = { file: tpl }
94
+ else {
95
+ subNs = breakNsPath(tpl).subNs
96
+ if (subNs === 'template') {
97
+ resp = this.resolveTemplate(tpl, opts)
98
+ } else if (subNs === 'partial') {
99
+ resp = this.resolvePartial(tpl, opts)
100
+ }
97
101
  }
98
102
  if (!resp) throw this.error('resourceNotFound%s', tpl)
99
103
  const { file } = resp
@@ -247,21 +251,24 @@ async function factory (pkgName) {
247
251
  const { upperFirst } = this.lib._
248
252
  const cache = this.app.bajoCache
249
253
  const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
250
- const { subNs } = breakNsPath(tpl)
251
- const canCache = subNs === 'template' && this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
254
+ let subNs
255
+ const isAbsolute = path.isAbsolute(tpl)
256
+ if (!isAbsolute) subNs = breakNsPath(tpl).subNs
257
+ const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
252
258
  if (canCache) {
253
259
  const item = await cache.get({ key })
254
260
  if (item) return item
255
261
  }
256
- await runHook(`${this.name}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
262
+ if (subNs) await runHook(`${this.name}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
257
263
  let text = await this._render(tpl, locals, opts)
258
264
  if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
259
- await runHook(`${this.name}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
265
+ if (subNs) await runHook(`${this.name}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
260
266
  if (canCache) await cache.set({ key, value: text, ttl: opts.cacheMaxAge ?? this.config.cache.maxAgeDur })
261
267
  return text
262
268
  }
263
269
 
264
270
  resolveLayout = (item = '', opts = {}) => {
271
+ const { find } = this.lib._
265
272
  const fallbackHandler = ({ file, exts, ns, subSubNs, type, theme }) => {
266
273
  const dir = ''
267
274
  const base = 'default'
@@ -271,11 +278,16 @@ async function factory (pkgName) {
271
278
  const check = `${this.app.main.dir.pkg}/${this.name}/${type}/_${theme}`
272
279
  file = filecheck.call(this, { dir, base, exts, check })
273
280
  }
274
- // check mail: common
281
+ // check main: common
275
282
  if (!file) {
276
283
  const check = `${this.app.main.dir.pkg}/${this.name}/${type}`
277
284
  file = filecheck.call(this, { dir, base, exts, check })
278
285
  }
286
+ if (theme && !file) {
287
+ const otheme = find(this.app.waibuMpa.themes, { name: theme })
288
+ const check = `${otheme.plugin.dir.pkg}/${this.name}/extend/${this.name}/${type}`
289
+ file = filecheck.call(this, { dir, base, exts, check })
290
+ }
279
291
  // check fallback: common
280
292
  if (!file) {
281
293
  const check = `${this.app[ns].dir.pkg}/${this.name}/${subSubNs ? (subSubNs + '/') : ''}${type}`
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "bajo-template",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Template Renderer for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
9
9
  "type": "module",
10
+ "bajo": {
11
+ "type": "plugin"
12
+ },
10
13
  "repository": {
11
14
  "type": "git",
12
15
  "url": "git+https://github.com/ardhi/bajo-template.git"