Skip to content

Commit 11a5e8f

Browse files
committed
🐛 fix: 长文档无法正常加载
1 parent 0768e69 commit 11a5e8f

File tree

4 files changed

+136
-85
lines changed

4 files changed

+136
-85
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88
"author": "",
99
"license": "MIT",
1010
"scripts": {
11-
"make-link": "node --no-warnings ./scripts/make_dev_link.js",
12-
"dev": "vite build --watch",
13-
"build": "vite build"
11+
"dev": "cross-env NODE_ENV=development VITE_SOURCEMAP=inline vite build --watch",
12+
"build": "cross-env NODE_ENV=production vite build",
13+
"make-install": "vite build && npx make-install"
1414
},
1515
"devDependencies": {
1616
"@sveltejs/vite-plugin-svelte": "^2.5.3",
1717
"@tsconfig/svelte": "^5.0.4",
1818
"@types/node": "^20.2.0",
19+
"cross-env": "^7.0.3",
1920
"eslint": "^8.42.0",
2021
"fast-glob": "^3.2.12",
2122
"glob": "^10.4.5",
2223
"js-yaml": "^4.1.0",
2324
"minimist": "^1.2.8",
2425
"rollup-plugin-livereload": "^2.0.5",
2526
"sass": "^1.62.1",
26-
"siyuan": "^1.0.3",
27+
"siyuan": "^1.0.8",
2728
"svelte": "^4.2.19",
2829
"ts-node": "^10.9.1",
2930
"typescript": "^5.0.4",

src/components/docs-flow/protyle.svelte

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
Author : Yp Z
44
Date : 2023-07-28 21:14:31
55
FilePath : /src/components/docs-flow/protyle.svelte
6-
LastEditTime : 2024-06-15 21:34:59
6+
LastEditTime : 2025-01-18 16:46:30
77
Description :
88
-->
99
<script lang="ts">
1010
import { onDestroy, onMount, afterUpdate } from "svelte";
11-
import { Protyle, type TProtyleAction, openTab } from "siyuan";
11+
import { Protyle, openTab } from "siyuan";
12+
// import { type TProtyleAction } from "siyuan";
1213
import { getBlockByID } from "../../api";
1314
import { notebooks } from "../../utils";
1415
@@ -34,7 +35,7 @@
3435
// scrollingChanged: false,
3536
};
3637
37-
let scroll: boolean = config.scroll;
38+
// let scroll: boolean = config.scroll;
3839
3940
let hpath: string = "";
4041
let divProtyle: HTMLDivElement;
@@ -47,7 +48,7 @@
4748
4849
let styleProtyleMaxHeight: string = "";
4950
const updateProtyleMaxHeight = () => {
50-
let maxHeight: number = scroll ? setting.getMaxHeight() : null;
51+
let maxHeight: number = config.scroll ? setting.getMaxHeight() : null;
5152
if (maxHeight) {
5253
maxHeight = maxHeight - (displayCollapseBar ? heightBreadcrumb : 0);
5354
}
@@ -112,28 +113,33 @@
112113
}
113114
});
114115
115-
function whichAction(): TProtyleAction[] {
116-
if (thisBlock.type == "d") {
117-
return ["cb-get-context"];
118-
} else {
119-
return ["cb-get-all"];
120-
}
121-
}
116+
// function whichAction(): TProtyleAction[] {
117+
// if (thisBlock.type == "d") {
118+
// return ["cb-get-context"];
119+
// } else {
120+
// return ["cb-get-all"];
121+
// }
122+
// }
122123
123124
function load() {
124125
if (!divProtyle) {
125126
return;
126127
}
127128
updateProtyleMaxHeight();
129+
console.log("load protyle", {
130+
title: config.protyleTitle,
131+
mode: config.readonly ? "preview" : "wysiwyg",
132+
scroll: config.scroll,
133+
});
128134
protyle = new Protyle(app, divProtyle, {
129135
mode: config.readonly ? "preview" : "wysiwyg",
130-
action: whichAction(),
136+
action: ["cb-get-all"],
131137
blockId: blockId,
132138
render: {
133139
background: false,
134140
title: config.protyleTitle,
135141
gutter: true,
136-
scroll: scroll,
142+
scroll: config.scroll,
137143
breadcrumb: true,
138144
breadcrumbDocName: false,
139145
},

svelte.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"
22

3+
const NoWarns = new Set([
4+
"a11y-click-events-have-key-events",
5+
"a11y-no-static-element-interactions",
6+
"a11y-no-noninteractive-element-interactions"
7+
]);
8+
39
export default {
410
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
511
// for more information about preprocessors
612
preprocess: vitePreprocess(),
13+
onwarn: (warning, handler) => {
14+
// suppress warnings on `vite dev` and `vite build`; but even without this, things still work
15+
if (NoWarns.has(warning.code)) return;
16+
handler(warning);
17+
}
718
}

vite.config.ts

Lines changed: 101 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { resolve } from "path"
22
import { defineConfig, loadEnv } from "vite"
3-
import minimist from "minimist"
43
import { viteStaticCopy } from "vite-plugin-static-copy"
54
import livereload from "rollup-plugin-livereload"
65
import { svelte } from "@sveltejs/vite-plugin-svelte"
@@ -9,13 +8,15 @@ import fg from 'fast-glob';
98

109
import vitePluginYamlI18n from './yaml-plugin';
1110

12-
const args = minimist(process.argv.slice(2))
13-
const isWatch = args.watch || args.w || false
14-
const devDistDir = "./dev"
15-
const distDir = isWatch ? devDistDir : "./dist"
11+
const env = process.env;
12+
const isSrcmap = env.VITE_SOURCEMAP === 'inline';
13+
const isDev = env.NODE_ENV === 'development';
1614

17-
console.log("isWatch=>", isWatch)
18-
console.log("distDir=>", distDir)
15+
const outputDir = isDev ? "dev" : "dist";
16+
17+
console.log("isDev=>", isDev);
18+
console.log("isSrcmap=>", isSrcmap);
19+
console.log("outputDir=>", outputDir);
1920

2021
export default defineConfig({
2122
resolve: {
@@ -29,95 +30,71 @@ export default defineConfig({
2930

3031
vitePluginYamlI18n({
3132
inDir: 'src/i18n',
32-
outDir: isWatch ? 'dev/i18n' : 'dist/i18n',
33+
outDir: `${outputDir}/i18n`
3334
}),
3435

3536
viteStaticCopy({
3637
targets: [
37-
{
38-
src: "./README*.md",
39-
dest: "./",
40-
},
41-
{
42-
src: "./icon.png",
43-
dest: "./",
44-
},
45-
{
46-
src: "./preview.png",
47-
dest: "./",
48-
},
49-
{
50-
src: "./plugin.json",
51-
dest: "./",
52-
},
38+
{ src: "./README*.md", dest: "./" },
39+
{ src: "./plugin.json", dest: "./" },
40+
{ src: "./preview.png", dest: "./" },
41+
{ src: "./icon.png", dest: "./" },
5342
{
5443
src: "./src/i18n/*.md",
5544
dest: "./i18n/",
5645
},
5746
],
5847
}),
48+
5949
],
6050

61-
// https://github.com/vitejs/vite/issues/1930
62-
// https://vitejs.dev/guide/env-and-mode.html#env-files
63-
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
64-
// 在这里自定义变量
6551
define: {
66-
"process.env.DEV_MODE": `"${isWatch}"`,
52+
"process.env.DEV_MODE": JSON.stringify(isDev),
53+
"process.env.NODE_ENV": JSON.stringify(env.NODE_ENV)
6754
},
6855

6956
build: {
70-
// 输出路径
71-
outDir: distDir,
57+
outDir: outputDir,
7258
emptyOutDir: false,
73-
74-
// 构建后是否生成 source map 文件
75-
sourcemap: isWatch ? 'inline' : false,
76-
77-
// 设置为 false 可以禁用最小化混淆
78-
// 或是用来指定是应用哪种混淆器
79-
// boolean | 'terser' | 'esbuild'
80-
// 不压缩,用于调试
81-
minify: !isWatch,
59+
minify: true,
60+
sourcemap: isSrcmap ? 'inline' : false,
8261

8362
lib: {
84-
// Could also be a dictionary or array of multiple entry points
8563
entry: resolve(__dirname, "src/index.ts"),
86-
// the proper extensions will be added
8764
fileName: "index",
8865
formats: ["cjs"],
8966
},
9067
rollupOptions: {
9168
plugins: [
92-
...(
93-
isWatch ? [
94-
livereload(devDistDir),
95-
{
96-
//监听静态资源文件
97-
name: 'watch-external',
98-
async buildStart() {
99-
const files = await fg([
100-
'src/i18n/*.yaml',
101-
'./README*.md',
102-
'./plugin.json'
103-
]);
104-
for (let file of files) {
105-
this.addWatchFile(file);
106-
}
69+
...(isDev ? [
70+
livereload(outputDir),
71+
{
72+
name: 'watch-external',
73+
async buildStart() {
74+
const files = await fg([
75+
'src/i18n/*.yaml',
76+
'./README*.md',
77+
'./plugin.json'
78+
]);
79+
for (let file of files) {
80+
this.addWatchFile(file);
10781
}
10882
}
109-
] : [
110-
zipPack({
111-
inDir: './dist',
112-
outDir: './',
113-
outFileName: 'package.zip'
114-
})
115-
]
116-
)
83+
}
84+
] : [
85+
// Clean up unnecessary files under dist dir
86+
cleanupDistFiles({
87+
patterns: ['i18n/*.yaml', 'i18n/*.md'],
88+
distDir: outputDir
89+
}),
90+
zipPack({
91+
inDir: './dist',
92+
outDir: './',
93+
outFileName: 'package.zip'
94+
})
95+
])
11796
],
11897

119-
// make sure to externalize deps that shouldn't be bundled
120-
// into your library
12198
external: ["siyuan", "process"],
12299

123100
output: {
@@ -131,4 +108,60 @@ export default defineConfig({
131108
},
132109
},
133110
}
134-
})
111+
});
112+
113+
114+
/**
115+
* Clean up some dist files after compiled
116+
* @author frostime
117+
* @param options:
118+
* @returns
119+
*/
120+
function cleanupDistFiles(options: { patterns: string[], distDir: string }) {
121+
const {
122+
patterns,
123+
distDir
124+
} = options;
125+
126+
return {
127+
name: 'rollup-plugin-cleanup',
128+
enforce: 'post',
129+
writeBundle: {
130+
sequential: true,
131+
order: 'post' as 'post',
132+
async handler() {
133+
const fg = await import('fast-glob');
134+
const fs = await import('fs');
135+
// const path = await import('path');
136+
137+
// 使用 glob 语法,确保能匹配到文件
138+
const distPatterns = patterns.map(pat => `${distDir}/${pat}`);
139+
console.debug('Cleanup searching patterns:', distPatterns);
140+
141+
const files = await fg.default(distPatterns, {
142+
dot: true,
143+
absolute: true,
144+
onlyFiles: false
145+
});
146+
147+
// console.info('Files to be cleaned up:', files);
148+
149+
for (const file of files) {
150+
try {
151+
if (fs.default.existsSync(file)) {
152+
const stat = fs.default.statSync(file);
153+
if (stat.isDirectory()) {
154+
fs.default.rmSync(file, { recursive: true });
155+
} else {
156+
fs.default.unlinkSync(file);
157+
}
158+
console.log(`Cleaned up: ${file}`);
159+
}
160+
} catch (error) {
161+
console.error(`Failed to clean up ${file}:`, error);
162+
}
163+
}
164+
}
165+
}
166+
};
167+
}

0 commit comments

Comments
 (0)