From 38b19fdb888f5f26004b747739f2403b2087c120 Mon Sep 17 00:00:00 2001 From: frostime Date: Thu, 24 Oct 2024 14:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20ci:=20update=20vite=20script,=20?= =?UTF-8?q?auto=20remove=20trivial=20files=20under=20`dist/`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/i18n/README.md | 12 ++++++++ vite.config.ts | 64 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 public/i18n/README.md diff --git a/public/i18n/README.md b/public/i18n/README.md new file mode 100644 index 0000000..af8de98 --- /dev/null +++ b/public/i18n/README.md @@ -0,0 +1,12 @@ +思源支持的 i18n 文件范围,可以在控制台 `siyuan.config.langs` 中查看。以下是目前(2024-10-24)支持的语言方案: + +The range of i18n files supported by SiYuan can be viewed in the console under `siyuan.config.langs`. Below are the language schemes currently supported as of now (October 24, 2024) : + +```js +>>> siyuan.config.langs.map( lang => lang.name) +['de_DE', 'en_US', 'es_ES', 'fr_FR', 'he_IL', 'it_IT', 'ja_JP', 'pl_PL', 'ru_RU', 'zh_CHT', 'zh_CN'] +``` + +在插件开发中,默认使用 JSON 格式作为国际化(i18n)的载体文件。如果您更喜欢使用 YAML 语法,可以将 JSON 文件替换为 YAML 文件(例如 `en_US.yaml`),并在其中编写 i18n 文本。本模板提供了相关的 Vite 插件,可以在编译时自动将 YAML 文件转换为 JSON 文件(请参见 `/yaml-plugin.js`)。本 MD 文件 和 YAML 文件会在 `npm run build` 时自动从 `dist` 目录下删除,仅保留必要的 JSON 文件共插件系统使用。 + +In plugin development, JSON format is used by default as the carrier file for internationalization (i18n). If you prefer to use YAML syntax, you can replace the JSON file with a YAML file (e.g., `en_US.yaml`) and write the i18n text within it. This template provides a related Vite plugin that can automatically convert YAML files to JSON files during the compilation process (see `/yaml-plugin.js`). This markdown file and YAML files will be automatically removed from the `dist` directory during `npm run build`, leaving only the necessary JSON files for plugin system to use. diff --git a/vite.config.ts b/vite.config.ts index 7deacce..cb7511b 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -41,6 +41,7 @@ export default defineConfig({ { src: "./icon.png", dest: "./" } ], }), + ], define: { @@ -77,6 +78,11 @@ export default defineConfig({ } } ] : [ + // Clean up unnecessary files under dist dir + cleanupDistFiles({ + patterns: ['i18n/*.yaml', 'i18n/*.md'], + distDir: outputDir + }), zipPack({ inDir: './dist', outDir: './', @@ -98,4 +104,60 @@ export default defineConfig({ }, }, } -}) +}); + + +/** + * Clean up some dist files after compiled + * @author frostime + * @param options: + * @returns + */ +function cleanupDistFiles(options: { patterns: string[], distDir: string }) { + const { + patterns, + distDir + } = options; + + return { + name: 'rollup-plugin-cleanup', + enforce: 'post', + writeBundle: { + sequential: true, + order: 'post' as 'post', + async handler() { + const fg = await import('fast-glob'); + const fs = await import('fs'); + // const path = await import('path'); + + // 使用 glob 语法,确保能匹配到文件 + const distPatterns = patterns.map(pat => `${distDir}/${pat}`); + console.debug('Cleanup searching patterns:', distPatterns); + + const files = await fg.default(distPatterns, { + dot: true, + absolute: true, + onlyFiles: false + }); + + // console.info('Files to be cleaned up:', files); + + for (const file of files) { + try { + if (fs.default.existsSync(file)) { + const stat = fs.default.statSync(file); + if (stat.isDirectory()) { + fs.default.rmSync(file, { recursive: true }); + } else { + fs.default.unlinkSync(file); + } + console.log(`Cleaned up: ${file}`); + } + } catch (error) { + console.error(`Failed to clean up ${file}:`, error); + } + } + } + } + }; +}