Skip to content

Commit

Permalink
配置添加版本号,运行时将自动对比版本决定是否进行全量编译
Browse files Browse the repository at this point in the history
  • Loading branch information
2234839 committed Dec 20, 2023
1 parent 95511a2 commit 972442d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/package.json
@@ -1,7 +1,7 @@
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"version": "0.0.3",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
91 changes: 50 additions & 41 deletions apps/frontend/src/config/index.ts
@@ -1,17 +1,18 @@
import { computed, reactive, watch, watchEffect } from "vue";
import { notebook } from "../core/siyuan_type";
import { deepAssign } from "@/util/deep_assign";
import { storeDep } from "@/dependency";
import { computed, reactive, watch, watchEffect } from 'vue'
import { notebook } from '../core/siyuan_type'
import { deepAssign } from '@/util/deep_assign'
import { storeDep } from '@/dependency'
import { version } from 'jszip'

/** 不要在运行时修改这个对象,他只应该在代码中配置 */
const defaultConfig = {
name: "default",
name: 'default',
/** 需要编译的笔记本 */
notebook: {} as notebook,
/** 思源的鉴权key */
authorized: "",
authorized: '',
/** 思源的api服务地址 */
apiPrefix: "http://127.0.0.1:6806",
apiPrefix: 'http://127.0.0.1:6806',
/** 打包成 zip */
compressedZip: true,
/** 不将 publicZip 打包到 zip 包中 */
Expand All @@ -27,39 +28,39 @@ const defaultConfig = {
* 则会生成 "https://shenzilong.cn/record/思源笔记.html" 这样的绝对路径
* 参见 https://www.sitemaps.org/protocol.html#escaping
*/
sitePrefix: ".",
sitePrefix: '.',
},
/** 开启增量编译,当开启增量编译时,
* 在编译过程中会依据 __skipBuilds__ 的内容来跳过一些没有变化不需要重新输出的内容
*/
enableIncrementalCompilation: false,
/** 增量编译文档
* 当需要重新全量编译文档时,将此选项设置为false
/**
* 要全量编译文档时将此选项设置为false,当OceanPress版本和上次编译时不同时会忽略此属性全量编译文档
*/
enableIncrementalCompilation_doc: true,
/** 跳过编译的资源 */
__skipBuilds__: {} as {
[id: string]:
| {
hash?: string;
/** 此文档正向引用的其他文档的id */ refs?: string[];
/** 挂件快照的更新时间 */ updated?: string;
hash?: string
/** 此文档正向引用的其他文档的id */ refs?: string[]
/** 挂件快照的更新时间 */ updated?: string
}
| undefined;
| undefined
},

cdn: {
/** 思源 js、css等文件的前缀 */
siyuanPrefix:
"https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/notebook/",
'https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/notebook/',
/** 思源 js、css等文件zip包地址 */
publicZip:
"https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/public.zip",
'https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/public.zip',
},
/** html模板嵌入代码块,会将此处配置中的代码嵌入到生成的html所对应的位置 */
embedCode: {
head: "",
beforeBody: "",
head: '',
beforeBody: '',
afterBody: `<footer>
<p style="text-align:center;margin:15px 0;">
技术支持:
Expand All @@ -69,59 +70,67 @@ const defaultConfig = {
</p>
</footer>`,
},
};
OceanPress: {
/** 此配置文件编译时的版本 */
version: '',
},
}
export const configs = reactive({
/** 当前所使用的配置项的 key */
__current__: "default" as const,
__current__: 'default' as const,
/** 为true是表示是代码中设置的默认值,不会保存到本地,避免覆盖之前保存的数据,在加载本地配置后会自动修改为false */
__init__: true,
default: deepAssign<typeof defaultConfig>({}, defaultConfig),
});
})

export function addConfig(name: string, value?: typeof defaultConfig) {
configs[name as "default"] = deepAssign<typeof defaultConfig>({}, value ?? defaultConfig);
configs[name as 'default'] = deepAssign<typeof defaultConfig>(
{},
value ?? defaultConfig,
)
}
/** 加载配置文件 */
export const loadConfigFile = (c?: typeof configs) => {
if (c) {
deepAssign(configs, c);
deepAssign(configs, c)
} else {
const localConfig = storeDep.getItem("configs");
const localConfig = storeDep.getItem('configs')
if (localConfig) {
/** 从本地存储加载配置 */
deepAssign(configs, JSON.parse(localConfig));
deepAssign(configs, JSON.parse(localConfig))
}
}

Object.entries(configs)
.filter(([key]) => key.startsWith("__") === false)
.filter(([key]) => key.startsWith('__') === false)
.forEach(([_key, obj]) => {
/** 将新增配置项更新到旧配置上 */
deepAssign(obj, defaultConfig, { update: false, add: true });
});
};
export const currentConfig = computed(() => configs[configs.__current__]);
deepAssign(obj, defaultConfig, { update: false, add: true })
})
}
export const currentConfig = computed(() => configs[configs.__current__])

export const saveConfig = () => {
if (configs.__init__ === false) storeDep.setItem("configs", JSON.stringify(configs));
};
if (configs.__init__ === false)
storeDep.setItem('configs', JSON.stringify(configs))
}

let timer: NodeJS.Timeout | null = null;
let timer: NodeJS.Timeout | null = null
/** 防抖的保存配置 */
export const debounceSaveConfig = () => {
if (timer) {
clearTimeout(timer);
clearTimeout(timer)
}
timer = setTimeout(() => {
saveConfig();
timer = null;
}, 700);
};
watch(configs, debounceSaveConfig, { deep: true });
saveConfig()
timer = null
}, 700)
}
watch(configs, debounceSaveConfig, { deep: true })

configs.__init__ = false;
configs.__init__ = false

/** 浏览器环境下,直接尝试加载配置 */
if (globalThis.document) {
loadConfigFile();
loadConfigFile()
}
12 changes: 10 additions & 2 deletions apps/frontend/src/core/build.ts
Expand Up @@ -11,6 +11,7 @@ import {
get_node_by_id,
sy_refs_get,
} from './cache'
import packageJson from '@/../package.json'

export interface DocTree {
[/** "/计算机基础课/自述" */ docPath: string]: {
Expand Down Expand Up @@ -82,6 +83,7 @@ export async function* build(
yield `=== 查询文档级block完成 ===`
for (let i = 0; i < Doc_blocks.length; i++) {
const docBlock = Doc_blocks[i]
// TODO 增量编译时不应该全部获取
const sy = await get_doc_by_SyPath(DB_block_path(docBlock))
docTree[docBlock.hpath] = { sy, docBlock }
process(i / Doc_blocks.length)
Expand All @@ -90,11 +92,16 @@ export async function* build(

process = processPercentage(0.4)
const arr = Object.entries(docTree)
let enableIncrementalCompilation_doc = config.enableIncrementalCompilation_doc
if (packageJson.version !== config.OceanPress.version) {
yield '配置文件版本号与OceanPress版本不一致,将进行文档全量编译'
enableIncrementalCompilation_doc = false
}
for (let i = 0; i < arr.length; i++) {
const [path, { sy, docBlock }] = arr[i]
if (
config.enableIncrementalCompilation &&
config.enableIncrementalCompilation_doc &&
enableIncrementalCompilation_doc &&
/** 文档本身没有发生变化 */
config.__skipBuilds__[docBlock.id]?.hash === docBlock.hash &&
/** docBlock所引用的文档也没有更新 */
Expand Down Expand Up @@ -232,10 +239,11 @@ export async function* build(
publicZip: config.cdn.publicZip,
})
}
config.OceanPress.version = packageJson.version
/** 更新跳过编译的资源 */
skipBuilds.write()
emit.percentage(100)
yield 'ok'
yield '编译完毕'
}
/** 下载zip */
export async function downloadZIP(
Expand Down

0 comments on commit 972442d

Please sign in to comment.