From 5588fd9a9d557938f034ce05ffb18f02015a198d Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 14 Apr 2023 11:44:14 +0800 Subject: [PATCH] core&ui: enhanced template hot reload --- packages/hydrooj/src/commands/addon.ts | 2 +- packages/hydrooj/src/entry/common.ts | 6 ++++-- packages/ui-default/backendlib/template.js | 15 +++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/hydrooj/src/commands/addon.ts b/packages/hydrooj/src/commands/addon.ts index a978fc1c..29699034 100644 --- a/packages/hydrooj/src/commands/addon.ts +++ b/packages/hydrooj/src/commands/addon.ts @@ -18,7 +18,7 @@ export function register(cli: CAC) { let addons = JSON.parse(fs.readFileSync(addonPath).toString()); if (operation === 'create') { const dir = `${addonDir}/${name || 'addon'}`; - fs.mkdirSync(dir); + fs.mkdirSync(dir, { recursive: true }); child.execSync('yarn init -y', { cwd: dir }); fs.mkdirSync(`${dir}/templates`); fs.mkdirSync(`${dir}/locales`); diff --git a/packages/hydrooj/src/entry/common.ts b/packages/hydrooj/src/entry/common.ts index 2733a1d8..bca11694 100644 --- a/packages/hydrooj/src/entry/common.ts +++ b/packages/hydrooj/src/entry/common.ts @@ -147,8 +147,10 @@ export async function template(pending: string[], fail: string[]) { try { const files = await getFiles(p); for (const file of files) { - if (file.endsWith('.tsx')) global.Hydro.ui.template[file] = require(path.resolve(p, file)); - global.Hydro.ui.template[file] = await fs.readFile(path.resolve(p, file), 'utf-8'); + const l = path.resolve(p, file); + if (file.endsWith('.tsx')) global.Hydro.ui.template[file] = require(l); + global.Hydro.ui.template[file] = await fs.readFile(l, 'utf-8'); + if (process.env.DEV) global.Hydro.ui.template[`${file}.source`] = l; } logger.info('Template init: %s', i); } catch (e) { diff --git a/packages/ui-default/backendlib/template.js b/packages/ui-default/backendlib/template.js index ddad8b80..38f870c6 100644 --- a/packages/ui-default/backendlib/template.js +++ b/packages/ui-default/backendlib/template.js @@ -17,10 +17,12 @@ else template &&= findFileSync(template); class Loader extends nunjucks.Loader { getSource(name) { - if (!template) { - if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`); + const src = global.Hydro.ui.template[name]; + const ref = global.Hydro.ui.template[`${name}.source`]; + if (!process.env.DEV) { + if (!src) throw new Error(`Cannot get template ${name}`); return { - src: global.Hydro.ui.template[name], + src, path: name, noCache: false, }; @@ -28,10 +30,11 @@ class Loader extends nunjucks.Loader { let fullpath = null; const p = path.resolve(template, name); if (fs.existsSync(p)) fullpath = p; + if (!fullpath && ref && fs.existsSync(ref)) fullpath = ref; if (!fullpath) { - if (global.Hydro.ui.template[name]) { + if (src) { return { - src: global.Hydro.ui.template[name], + src, path: name, noCache: true, }; @@ -39,7 +42,7 @@ class Loader extends nunjucks.Loader { throw new Error(`Cannot get template ${name}`); } return { - src: fs.readFileSync(fullpath, 'utf-8').toString(), + src: fs.readFileSync(fullpath, 'utf-8'), path: fullpath, noCache: true, };