Skip to content

Commit 972442d

Browse files
committedDec 20, 2023
配置添加版本号,运行时将自动对比版本决定是否进行全量编译
·
v1.0.6v0.0.0.7
1 parent 95511a2 commit 972442d

File tree

3 files changed

+61
-44
lines changed

3 files changed

+61
-44
lines changed
 

‎apps/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "frontend",
33
"private": true,
4-
"version": "0.0.0",
4+
"version": "0.0.3",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

‎apps/frontend/src/config/index.ts

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { computed, reactive, watch, watchEffect } from "vue";
2-
import { notebook } from "../core/siyuan_type";
3-
import { deepAssign } from "@/util/deep_assign";
4-
import { storeDep } from "@/dependency";
1+
import { computed, reactive, watch, watchEffect } from 'vue'
2+
import { notebook } from '../core/siyuan_type'
3+
import { deepAssign } from '@/util/deep_assign'
4+
import { storeDep } from '@/dependency'
5+
import { version } from 'jszip'
56

67
/** 不要在运行时修改这个对象,他只应该在代码中配置 */
78
const defaultConfig = {
8-
name: "default",
9+
name: 'default',
910
/** 需要编译的笔记本 */
1011
notebook: {} as notebook,
1112
/** 思源的鉴权key */
12-
authorized: "",
13+
authorized: '',
1314
/** 思源的api服务地址 */
14-
apiPrefix: "http://127.0.0.1:6806",
15+
apiPrefix: 'http://127.0.0.1:6806',
1516
/** 打包成 zip */
1617
compressedZip: true,
1718
/** 不将 publicZip 打包到 zip 包中 */
@@ -27,39 +28,39 @@ const defaultConfig = {
2728
* 则会生成 "https://shenzilong.cn/record/思源笔记.html" 这样的绝对路径
2829
* 参见 https://www.sitemaps.org/protocol.html#escaping
2930
*/
30-
sitePrefix: ".",
31+
sitePrefix: '.',
3132
},
3233
/** 开启增量编译,当开启增量编译时,
3334
* 在编译过程中会依据 __skipBuilds__ 的内容来跳过一些没有变化不需要重新输出的内容
3435
*/
3536
enableIncrementalCompilation: false,
36-
/** 增量编译文档
37-
* 当需要重新全量编译文档时,将此选项设置为false
37+
/**
38+
* 要全量编译文档时将此选项设置为false,当OceanPress版本和上次编译时不同时会忽略此属性全量编译文档
3839
*/
3940
enableIncrementalCompilation_doc: true,
4041
/** 跳过编译的资源 */
4142
__skipBuilds__: {} as {
4243
[id: string]:
4344
| {
44-
hash?: string;
45-
/** 此文档正向引用的其他文档的id */ refs?: string[];
46-
/** 挂件快照的更新时间 */ updated?: string;
45+
hash?: string
46+
/** 此文档正向引用的其他文档的id */ refs?: string[]
47+
/** 挂件快照的更新时间 */ updated?: string
4748
}
48-
| undefined;
49+
| undefined
4950
},
5051

5152
cdn: {
5253
/** 思源 js、css等文件的前缀 */
5354
siyuanPrefix:
54-
"https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/notebook/",
55+
'https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/notebook/',
5556
/** 思源 js、css等文件zip包地址 */
5657
publicZip:
57-
"https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/public.zip",
58+
'https://fastly.jsdelivr.net/gh/siyuan-note/oceanpress@main/apps/frontend/public/public.zip',
5859
},
5960
/** html模板嵌入代码块,会将此处配置中的代码嵌入到生成的html所对应的位置 */
6061
embedCode: {
61-
head: "",
62-
beforeBody: "",
62+
head: '',
63+
beforeBody: '',
6364
afterBody: `<footer>
6465
<p style="text-align:center;margin:15px 0;">
6566
技术支持:
@@ -69,59 +70,67 @@ const defaultConfig = {
6970
</p>
7071
</footer>`,
7172
},
72-
};
73+
OceanPress: {
74+
/** 此配置文件编译时的版本 */
75+
version: '',
76+
},
77+
}
7378
export const configs = reactive({
7479
/** 当前所使用的配置项的 key */
75-
__current__: "default" as const,
80+
__current__: 'default' as const,
7681
/** 为true是表示是代码中设置的默认值,不会保存到本地,避免覆盖之前保存的数据,在加载本地配置后会自动修改为false */
7782
__init__: true,
7883
default: deepAssign<typeof defaultConfig>({}, defaultConfig),
79-
});
84+
})
8085

8186
export function addConfig(name: string, value?: typeof defaultConfig) {
82-
configs[name as "default"] = deepAssign<typeof defaultConfig>({}, value ?? defaultConfig);
87+
configs[name as 'default'] = deepAssign<typeof defaultConfig>(
88+
{},
89+
value ?? defaultConfig,
90+
)
8391
}
8492
/** 加载配置文件 */
8593
export const loadConfigFile = (c?: typeof configs) => {
8694
if (c) {
87-
deepAssign(configs, c);
95+
deepAssign(configs, c)
8896
} else {
89-
const localConfig = storeDep.getItem("configs");
97+
const localConfig = storeDep.getItem('configs')
9098
if (localConfig) {
9199
/** 从本地存储加载配置 */
92-
deepAssign(configs, JSON.parse(localConfig));
100+
deepAssign(configs, JSON.parse(localConfig))
93101
}
94102
}
95103

96104
Object.entries(configs)
97-
.filter(([key]) => key.startsWith("__") === false)
105+
.filter(([key]) => key.startsWith('__') === false)
98106
.forEach(([_key, obj]) => {
99107
/** 将新增配置项更新到旧配置上 */
100-
deepAssign(obj, defaultConfig, { update: false, add: true });
101-
});
102-
};
103-
export const currentConfig = computed(() => configs[configs.__current__]);
108+
deepAssign(obj, defaultConfig, { update: false, add: true })
109+
})
110+
}
111+
export const currentConfig = computed(() => configs[configs.__current__])
104112

105113
export const saveConfig = () => {
106-
if (configs.__init__ === false) storeDep.setItem("configs", JSON.stringify(configs));
107-
};
114+
if (configs.__init__ === false)
115+
storeDep.setItem('configs', JSON.stringify(configs))
116+
}
108117

109-
let timer: NodeJS.Timeout | null = null;
118+
let timer: NodeJS.Timeout | null = null
110119
/** 防抖的保存配置 */
111120
export const debounceSaveConfig = () => {
112121
if (timer) {
113-
clearTimeout(timer);
122+
clearTimeout(timer)
114123
}
115124
timer = setTimeout(() => {
116-
saveConfig();
117-
timer = null;
118-
}, 700);
119-
};
120-
watch(configs, debounceSaveConfig, { deep: true });
125+
saveConfig()
126+
timer = null
127+
}, 700)
128+
}
129+
watch(configs, debounceSaveConfig, { deep: true })
121130

122-
configs.__init__ = false;
131+
configs.__init__ = false
123132

124133
/** 浏览器环境下,直接尝试加载配置 */
125134
if (globalThis.document) {
126-
loadConfigFile();
135+
loadConfigFile()
127136
}

‎apps/frontend/src/core/build.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
get_node_by_id,
1212
sy_refs_get,
1313
} from './cache'
14+
import packageJson from '@/../package.json'
1415

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

9193
process = processPercentage(0.4)
9294
const arr = Object.entries(docTree)
95+
let enableIncrementalCompilation_doc = config.enableIncrementalCompilation_doc
96+
if (packageJson.version !== config.OceanPress.version) {
97+
yield '配置文件版本号与OceanPress版本不一致,将进行文档全量编译'
98+
enableIncrementalCompilation_doc = false
99+
}
93100
for (let i = 0; i < arr.length; i++) {
94101
const [path, { sy, docBlock }] = arr[i]
95102
if (
96103
config.enableIncrementalCompilation &&
97-
config.enableIncrementalCompilation_doc &&
104+
enableIncrementalCompilation_doc &&
98105
/** 文档本身没有发生变化 */
99106
config.__skipBuilds__[docBlock.id]?.hash === docBlock.hash &&
100107
/** docBlock所引用的文档也没有更新 */
@@ -232,10 +239,11 @@ export async function* build(
232239
publicZip: config.cdn.publicZip,
233240
})
234241
}
242+
config.OceanPress.version = packageJson.version
235243
/** 更新跳过编译的资源 */
236244
skipBuilds.write()
237245
emit.percentage(100)
238-
yield 'ok'
246+
yield '编译完毕'
239247
}
240248
/** 下载zip */
241249
export async function downloadZIP(

0 commit comments

Comments
 (0)
Please sign in to comment.