|
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" |
6 | 7 | import {
|
7 | 8 | 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" |
12 | 13 |
|
13 |
| -// 更改 siyuanDir 为你的思源工作空间的所在目录 |
14 |
| -// 例如:/Users/example/Siyuan |
| 14 | +const pluginInfo = require("./plugin.json") |
15 | 15 |
|
16 |
| -// Change siyuanDir to your Siyuan workspace directory |
17 |
| -// For example: /Users/example/Siyuan |
| 16 | +export default defineConfig(({ |
| 17 | + mode, |
| 18 | +}) => { |
18 | 19 |
|
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) |
22 | 27 |
|
23 |
| -const args = minimist(process.argv.slice(2)); |
24 |
| -const isWatch = args.watch || args.w || false; |
25 |
| -const distDir = isWatch ? devDistDir : "./dist"; |
26 | 28 |
|
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}`) |
29 | 42 |
|
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 | + }, |
34 | 56 | },
|
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), |
94 | 93 | },
|
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 | + }, |
112 | 134 | },
|
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 | + }, |
135 | 157 | },
|
136 | 158 | },
|
137 | 159 | },
|
138 |
| - }, |
139 |
| -}); |
| 160 | + } |
| 161 | +}) |
0 commit comments