Skip to content

Commit cbf25a0

Browse files
committed
imp: added env to set siyuan workspace
1 parent 3a6753e commit cbf25a0

File tree

5 files changed

+162
-127
lines changed

5 files changed

+162
-127
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Siyuan workspace path
2+
# The workspace contains `data` folder
3+
VITE_SIYUAN_WORKSPACE_PATH=/path/to/siyuan/workspace
4+
5+
# Manually set the dist dir to work with Symbolic Links
6+
VITE_DEV_DIST_DIR=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ dev
88
dist
99
build
1010
tmp
11+
.env

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
2. Use `git clone` to clone the copied repo to your computer.
2525
3. Use `pnpm i` to install the dependencies.
2626

27-
4. Go to the [vite.config.ts](./vite.config.ts) and change the `siyuanDir` to your workspace.
27+
4. Copy the `.env.example` file as `.env`, set the `VITE_SIYUAN_WORKSPACE_PATH` to your SiYuan workspace.
28+
2829

2930
> [!TIP]
3031
>
3132
> If you don't like build the project into your workspace, you can use `Symbolic Link` to link the folder.
33+
>
34+
> Set `VITE_DEV_DIST_DIR` to any folder, and use `Symbolic Link` to link `siyuan_workspace/data/plugins/your_plugin_name` to `VITE_DEV_DIST_DIR`.
3235
3336

3437
5. Use `pnpm dev` to run the project.

README_zh_CN.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
2. 使用 `git clone` 克隆创建好的仓库。
2525
3. 使用 `pnpm i` 安装项目所需的依赖。
2626

27-
4. 打开 [vite.config.ts](./vite.config.ts) 文件,并将里面的 `siyuanDir` 更改为你自己的思源工作空间。
27+
4. 复制 `.env.example` 文件并取名为 `.env`,修改其中的 `VITE_SIYUAN_WORKSPACE_PATH` 为你的思源工作空间。
28+
2829

2930
> [!TIP]
3031
>
3132
> 如果你不喜欢将项目打包至工作空间中,可以使用 `软链接` 的方式。
33+
>
34+
> 设置 `VITE_DEV_DIST_DIR` 为任意文件夹,然后使用 `软链接` 链接 `siyuan_workspace/data/plugins/your_plugin_name` 目录到 `VITE_DEV_DIST_DIR`
3235
3336

3437
5. 使用 `pnpm dev` 启动项目。

vite.config.ts

Lines changed: 147 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,161 @@
1-
import { resolve } from "path";
2-
import vue from "@vitejs/plugin-vue";
3-
import fg from "fast-glob";
4-
import minimist from "minimist";
5-
import livereload from "rollup-plugin-livereload";
1+
/* eslint-disable node/prefer-global/process */
2+
import { resolve } from "node:path"
3+
import vue from "@vitejs/plugin-vue"
4+
import fg from "fast-glob"
5+
import minimist from "minimist"
6+
import livereload from "rollup-plugin-livereload"
67
import {
78
defineConfig,
8-
} from "vite";
9-
import { viteStaticCopy } from "vite-plugin-static-copy";
10-
import zipPack from "vite-plugin-zip-pack";
11-
const pluginInfo = require("./plugin.json");
9+
loadEnv,
10+
} from "vite"
11+
import { viteStaticCopy } from "vite-plugin-static-copy"
12+
import zipPack from "vite-plugin-zip-pack"
1213

13-
// 更改 siyuanDir 为你的思源工作空间的所在目录
14-
// 例如:/Users/example/Siyuan
14+
const pluginInfo = require("./plugin.json")
1515

16-
// Change siyuanDir to your Siyuan workspace directory
17-
// For example: /Users/example/Siyuan
16+
export default defineConfig(({
17+
mode,
18+
}) => {
1819

19-
// const siyuanDir = "/Users/example/Siyuan";
20-
const siyuanDir = "";
21-
const devDistDir = siyuanDir ? `${siyuanDir}/data/plugins/${pluginInfo.name}` : './dev';
20+
console.log('mode=>', mode)
21+
const env = loadEnv(mode, process.cwd())
22+
const {
23+
VITE_SIYUAN_WORKSPACE_PATH,
24+
VITE_DEV_DIST_DIR,
25+
} = env
26+
console.log('env=>', env)
2227

23-
const args = minimist(process.argv.slice(2));
24-
const isWatch = args.watch || args.w || false;
25-
const distDir = isWatch ? devDistDir : "./dist";
2628

27-
console.log("isWatch=>", isWatch);
28-
console.log("distDir=>", distDir);
29+
const siyuanWorkspacePath = VITE_SIYUAN_WORKSPACE_PATH
30+
let devDistDir = './dev'
31+
if (!siyuanWorkspacePath) {
32+
console.log("\nSiyuan workspace path is not set.")
33+
} else {
34+
console.log(`\nSiyuan workspace path is set:\n${siyuanWorkspacePath}`)
35+
devDistDir = `${siyuanWorkspacePath}/data/plugins/${pluginInfo.name}`
36+
}
37+
if (VITE_DEV_DIST_DIR) {
38+
console.log(`\nDev dist dir is set:\n${VITE_DEV_DIST_DIR}`)
39+
devDistDir = VITE_DEV_DIST_DIR
40+
}
41+
console.log(`\nPlugin will build to:\n${devDistDir}`)
2942

30-
export default defineConfig({
31-
resolve: {
32-
alias: {
33-
"@": resolve(__dirname, "src"),
43+
const args = minimist(process.argv.slice(2))
44+
const isWatch = args.watch || args.w || false
45+
const distDir = isWatch ? devDistDir : "./dist"
46+
47+
console.log()
48+
console.log("isWatch=>", isWatch)
49+
console.log("distDir=>", distDir)
50+
51+
return {
52+
resolve: {
53+
alias: {
54+
"@": resolve(__dirname, "src"),
55+
},
3456
},
35-
},
36-
37-
plugins: [
38-
vue(),
39-
viteStaticCopy({
40-
targets: [
41-
{
42-
src: "./README*.md",
43-
dest: "./",
44-
},
45-
{
46-
src: "./icon.png",
47-
dest: "./",
48-
},
49-
{
50-
src: "./preview.png",
51-
dest: "./",
52-
},
53-
{
54-
src: "./plugin.json",
55-
dest: "./",
56-
},
57-
{
58-
src: "./src/i18n/**",
59-
dest: "./i18n/",
60-
},
61-
],
62-
}),
63-
],
64-
65-
// https://github.com/vitejs/vite/issues/1930
66-
// https://vitejs.dev/guide/env-and-mode.html#env-files
67-
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
68-
// 在这里自定义变量
69-
define: {
70-
"process.env.DEV_MODE": `"${isWatch}"`,
71-
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
72-
},
73-
74-
build: {
75-
// 输出路径
76-
outDir: distDir,
77-
emptyOutDir: true,
78-
79-
// 构建后是否生成 source map 文件
80-
sourcemap: false,
81-
82-
// 设置为 false 可以禁用最小化混淆
83-
// 或是用来指定是应用哪种混淆器
84-
// boolean | 'terser' | 'esbuild'
85-
// 不压缩,用于调试
86-
minify: !isWatch,
87-
88-
lib: {
89-
// Could also be a dictionary or array of multiple entry points
90-
entry: resolve(__dirname, "src/index.ts"),
91-
// the proper extensions will be added
92-
fileName: "index",
93-
formats: ["cjs"],
57+
58+
plugins: [
59+
vue(),
60+
viteStaticCopy({
61+
targets: [
62+
{
63+
src: "./README*.md",
64+
dest: "./",
65+
},
66+
{
67+
src: "./icon.png",
68+
dest: "./",
69+
},
70+
{
71+
src: "./preview.png",
72+
dest: "./",
73+
},
74+
{
75+
src: "./plugin.json",
76+
dest: "./",
77+
},
78+
{
79+
src: "./src/i18n/**",
80+
dest: "./i18n/",
81+
},
82+
],
83+
}),
84+
],
85+
86+
// https://github.com/vitejs/vite/issues/1930
87+
// https://vitejs.dev/guide/env-and-mode.html#env-files
88+
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
89+
// 在这里自定义变量
90+
define: {
91+
"process.env.DEV_MODE": `"${isWatch}"`,
92+
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
9493
},
95-
rollupOptions: {
96-
plugins: [
97-
...(isWatch
98-
? [
99-
livereload(devDistDir),
100-
{
101-
//监听静态资源文件
102-
name: "watch-external",
103-
async buildStart() {
104-
const files = await fg([
105-
"src/i18n/*.json",
106-
"./README*.md",
107-
"./plugin.json",
108-
]);
109-
for (let file of files) {
110-
this.addWatchFile(file);
111-
}
94+
95+
build: {
96+
// 输出路径
97+
outDir: distDir,
98+
emptyOutDir: true,
99+
100+
// 构建后是否生成 source map 文件
101+
sourcemap: false,
102+
103+
// 设置为 false 可以禁用最小化混淆
104+
// 或是用来指定是应用哪种混淆器
105+
// boolean | 'terser' | 'esbuild'
106+
// 不压缩,用于调试
107+
minify: !isWatch,
108+
109+
lib: {
110+
// Could also be a dictionary or array of multiple entry points
111+
entry: resolve(__dirname, "src/index.ts"),
112+
// the proper extensions will be added
113+
fileName: "index",
114+
formats: ["cjs"],
115+
},
116+
rollupOptions: {
117+
plugins: [
118+
...(isWatch
119+
? [
120+
livereload(devDistDir),
121+
{
122+
// 监听静态资源文件
123+
name: "watch-external",
124+
async buildStart() {
125+
const files = await fg([
126+
"src/i18n/*.json",
127+
"./README*.md",
128+
"./plugin.json",
129+
])
130+
for (const file of files) {
131+
this.addWatchFile(file)
132+
}
133+
},
112134
},
113-
},
114-
]
115-
: [
116-
zipPack({
117-
inDir: "./dist",
118-
outDir: "./",
119-
outFileName: "package.zip",
120-
}),
121-
]),
122-
],
123-
124-
// make sure to externalize deps that shouldn't be bundled
125-
// into your library
126-
external: ["siyuan", "process"],
127-
128-
output: {
129-
entryFileNames: "[name].js",
130-
assetFileNames: (assetInfo) => {
131-
if (assetInfo.name === "style.css") {
132-
return "index.css";
133-
}
134-
return assetInfo.name;
135+
]
136+
: [
137+
zipPack({
138+
inDir: "./dist",
139+
outDir: "./",
140+
outFileName: "package.zip",
141+
}),
142+
]),
143+
],
144+
145+
// make sure to externalize deps that shouldn't be bundled
146+
// into your library
147+
external: ["siyuan", "process"],
148+
149+
output: {
150+
entryFileNames: "[name].js",
151+
assetFileNames: (assetInfo) => {
152+
if (assetInfo.name === "style.css") {
153+
return "index.css"
154+
}
155+
return assetInfo.name
156+
},
135157
},
136158
},
137159
},
138-
},
139-
});
160+
}
161+
})

0 commit comments

Comments
 (0)