🔧 ci: 更新 vite 打包脚本

This commit is contained in:
frostime 2024-10-16 14:43:56 +08:00
parent 4aee720da5
commit 591cf2e95e
10 changed files with 146 additions and 135 deletions

View file

@ -59,4 +59,4 @@ jobs:
artifactErrorsFailBuild: true artifactErrorsFailBuild: true
artifacts: "package.zip" artifacts: "package.zip"
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
prerelease: true prerelease: false

View file

@ -1,6 +1,6 @@
{ {
"name": "plugin-sample-vite-svelte", "name": "plugin-sample-vite-svelte",
"version": "0.3.5", "version": "0.3.6",
"type": "module", "type": "module",
"description": "This is a sample plugin based on vite and svelte for Siyuan (https://b3log.org/siyuan)", "description": "This is a sample plugin based on vite and svelte for Siyuan (https://b3log.org/siyuan)",
"repository": "", "repository": "",
@ -8,28 +8,29 @@
"author": "frostime", "author": "frostime",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "cross-env NODE_ENV=development VITE_SOURCEMAP=inline vite build --watch",
"build": "cross-env NODE_ENV=production vite build",
"make-link": "node --no-warnings ./scripts/make_dev_link.js", "make-link": "node --no-warnings ./scripts/make_dev_link.js",
"make-link-win": "powershell.exe -NoProfile -ExecutionPolicy Bypass -File ./scripts/elevate.ps1 -scriptPath ./scripts/make_dev_link.js", "make-link-win": "powershell.exe -NoProfile -ExecutionPolicy Bypass -File ./scripts/elevate.ps1 -scriptPath ./scripts/make_dev_link.js",
"dev": "vite build --watch",
"update-version": "node --no-warnings ./scripts/update_version.js", "update-version": "node --no-warnings ./scripts/update_version.js",
"build": "vite build",
"make-install": "vite build && node --no-warnings ./scripts/make_install.js" "make-install": "vite build && node --no-warnings ./scripts/make_install.js"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.0", "@sveltejs/vite-plugin-svelte": "^3.1.0",
"@tsconfig/svelte": "^4.0.1", "@tsconfig/svelte": "^4.0.1",
"@types/node": "^20.3.0", "@types/node": "^20.3.0",
"cross-env": "^7.0.3",
"fast-glob": "^3.2.12", "fast-glob": "^3.2.12",
"glob": "^7.2.3", "glob": "^10.0.0",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"minimist": "^1.2.8", "minimist": "^1.2.8",
"rollup-plugin-livereload": "^2.0.5", "rollup-plugin-livereload": "^2.0.5",
"sass": "^1.63.3", "sass": "^1.63.3",
"siyuan": "0.9.9", "siyuan": "1.0.4",
"svelte": "^4.2.0", "svelte": "^4.2.19",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^5.1.3", "typescript": "^5.1.3",
"vite": "^5.0.0", "vite": "^5.2.9",
"vite-plugin-static-copy": "^1.0.2", "vite-plugin-static-copy": "^1.0.2",
"vite-plugin-zip-pack": "^1.0.5" "vite-plugin-zip-pack": "^1.0.5"
} }

View file

@ -2,7 +2,7 @@
"name": "plugin-sample-vite-svelte", "name": "plugin-sample-vite-svelte",
"author": "frostime", "author": "frostime",
"url": "https://github.com/siyuan-note/plugin-sample-vite-svelte", "url": "https://github.com/siyuan-note/plugin-sample-vite-svelte",
"version": "0.3.5", "version": "0.3.6",
"minAppVersion": "3.0.12", "minAppVersion": "3.0.12",
"backends": [ "backends": [
"windows", "windows",
@ -37,6 +37,8 @@
] ]
}, },
"keywords": [ "keywords": [
"plugin", "sample", "插件样例" "plugin",
"sample",
"插件样例"
] ]
} }

20
public/i18n/zh_CN.json Normal file
View file

@ -0,0 +1,20 @@
{
"addTopBarIcon": "使用插件添加一个顶栏按钮",
"cancel": "取消",
"save": "保存",
"byeMenu": "再见,菜单!",
"helloPlugin": "你好,插件!",
"byePlugin": "再见,插件!",
"showDialog": "弹出一个对话框",
"removedData": "数据已删除",
"confirmRemove": "确认删除 ${name} 中的数据?",
"insertEmoji": "插入表情",
"removeSpace": "移除空格",
"getTab": "在日志中打印出已打开的所有自定义页签",
"name": "思源",
"hello": {
"makesure": "使用这个模板之前,请阅读<a href=\"https://github.com/siyuan-note/plugin-sample\">官方教程</a>, 确保自己已经理解了插件的基本开发流程。"
},
"hintTitle": "关于",
"hintDesc": "<a href='https://github.com/siyuan-note/plugin-sample-vite-svelte'>🔗 plugin-sample-vite-svelte</a><br>💻 @frostime<br>💻 @88250<br>💻 @zxkmm"
}

View file

@ -1,21 +0,0 @@
---
addTopBarIcon: 使用插件添加一个顶栏按钮
cancel: 取消
save: 保存
byeMenu: 再见,菜单!
helloPlugin: 你好,插件!
byePlugin: 再见,插件!
showDialog: 弹出一个对话框
removedData: 数据已删除
confirmRemove: 确认删除 ${name} 中的数据?
insertEmoji: 插入表情
removeSpace: 移除空格
getTab: 在日志中打印出已打开的所有自定义页签
name: 思源
hello:
makesure: 使用这个模板之前,请阅读<a href="https://github.com/siyuan-note/plugin-sample">官方教程</a>,
确保自己已经理解了插件的基本开发流程。
hintTitle: 关于
hintDesc: "<a href='https://github.com/siyuan-note/plugin-sample-vite-svelte'>\U0001F517
plugin-sample-vite-svelte</a><br>\U0001F4BB @frostime<br>\U0001F4BB @88250<br>\U0001F4BB
@zxkmm"

View file

@ -1,3 +1,11 @@
<!--
Copyright (c) 2024 by frostime. All Rights Reserved.
Author : frostime
Date : 2023-11-19 12:30:45
FilePath : /src/hello.svelte
LastEditTime : 2024-10-16 14:37:50
Description :
-->
<script lang="ts"> <script lang="ts">
import { onDestroy, onMount } from "svelte"; import { onDestroy, onMount } from "svelte";
import { version, sql as query } from "@/api"; import { version, sql as query } from "@/api";

View file

@ -1,26 +0,0 @@
#helloPanel {
border: 1px rgb(189, 119, 119) dashed;
}
.plugin-sample {
&__custom-tab {
background-color: var(--b3-theme-background);
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
&__custom-dock {
display: flex;
justify-content: center;
align-items: center;
}
&__time {
background: var(--b3-card-info-background);
border-radius: 4px;
padding: 2px 8px;
}
}

View file

@ -3,7 +3,7 @@
* @Author : frostime * @Author : frostime
* @Date : 2024-03-23 21:37:33 * @Date : 2024-03-23 21:37:33
* @FilePath : /src/libs/dialog.ts * @FilePath : /src/libs/dialog.ts
* @LastEditTime : 2024-07-19 15:34:39 * @LastEditTime : 2024-10-16 14:31:04
* @Description : Kits about dialogs * @Description : Kits about dialogs
*/ */
import { Dialog } from "siyuan"; import { Dialog } from "siyuan";
@ -135,7 +135,10 @@ export const simpleDialog = (args: {
destroyCallback: args.callback destroyCallback: args.callback
}); });
dialog.element.querySelector(".dialog-content").appendChild(args.ele); dialog.element.querySelector(".dialog-content").appendChild(args.ele);
return dialog; return {
dialog,
close: dialog.destroy.bind(dialog)
};
} }
@ -147,9 +150,15 @@ export const svelteDialog = (args: {
let container = document.createElement('div') let container = document.createElement('div')
container.style.display = 'contents'; container.style.display = 'contents';
let component = args.constructor(container); let component = args.constructor(container);
simpleDialog({...args, ele: container, callback: () => { const { dialog, close } = simpleDialog({
...args, ele: container, callback: () => {
component.$destroy(); component.$destroy();
if (args.callback) args.callback();; if (args.callback) args.callback();
}}); }
return component; });
return {
component,
dialog,
close
}
} }

48
src/libs/promise-pool.ts Normal file
View file

@ -0,0 +1,48 @@
export default class PromiseLimitPool<T> {
private maxConcurrent: number;
private currentRunning = 0;
private queue: (() => void)[] = [];
private promises: Promise<T>[] = [];
constructor(maxConcurrent: number) {
this.maxConcurrent = maxConcurrent;
}
add(fn: () => Promise<T>): void {
const promise = new Promise<T>((resolve, reject) => {
const run = async () => {
try {
this.currentRunning++;
const result = await fn();
resolve(result);
} catch (error) {
reject(error);
} finally {
this.currentRunning--;
this.next();
}
};
if (this.currentRunning < this.maxConcurrent) {
run();
} else {
this.queue.push(run);
}
});
this.promises.push(promise);
}
async awaitAll(): Promise<T[]> {
return Promise.all(this.promises);
}
/**
* Handles the next task in the queue.
*/
private next(): void {
if (this.queue.length > 0 && this.currentRunning < this.maxConcurrent) {
const nextRun = this.queue.shift()!;
nextRun();
}
}
}

View file

@ -1,6 +1,5 @@
import { resolve } from "path" import { resolve } from "path"
import { defineConfig, loadEnv } from "vite" import { defineConfig, loadEnv } from "vite"
import minimist from "minimist"
import { viteStaticCopy } from "vite-plugin-static-copy" import { viteStaticCopy } from "vite-plugin-static-copy"
import livereload from "rollup-plugin-livereload" import livereload from "rollup-plugin-livereload"
import { svelte } from "@sveltejs/vite-plugin-svelte" import { svelte } from "@sveltejs/vite-plugin-svelte"
@ -9,13 +8,15 @@ import fg from 'fast-glob';
import vitePluginYamlI18n from './yaml-plugin'; import vitePluginYamlI18n from './yaml-plugin';
const args = minimist(process.argv.slice(2)) const env = process.env;
const isWatch = args.watch || args.w || false const isSrcmap = env.VITE_SOURCEMAP === 'inline';
const devDistDir = "dev" const isDev = env.NODE_ENV === 'development';
const distDir = isWatch ? devDistDir : "dist"
console.log("isWatch=>", isWatch) const outputDir = isDev ? "dev" : "dist";
console.log("distDir=>", distDir)
console.log("isDev=>", isDev);
console.log("isSrcmap=>", isSrcmap);
console.log("outputDir=>", outputDir);
export default defineConfig({ export default defineConfig({
resolve: { resolve: {
@ -29,68 +30,40 @@ export default defineConfig({
vitePluginYamlI18n({ vitePluginYamlI18n({
inDir: 'public/i18n', inDir: 'public/i18n',
outDir: `${distDir}/i18n` outDir: `${outputDir}/i18n`
}), }),
viteStaticCopy({ viteStaticCopy({
targets: [ targets: [
{ { src: "./README*.md", dest: "./" },
src: "./README*.md", { src: "./plugin.json", dest: "./" },
dest: "./", { src: "./preview.png", dest: "./" },
}, { src: "./icon.png", dest: "./" }
{
src: "./plugin.json",
dest: "./",
},
{
src: "./preview.png",
dest: "./",
},
{
src: "./icon.png",
dest: "./",
}
], ],
}), }),
], ],
// https://github.com/vitejs/vite/issues/1930
// https://vitejs.dev/guide/env-and-mode.html#env-files
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
// 在这里自定义变量
define: { define: {
"process.env.DEV_MODE": `"${isWatch}"`, "process.env.DEV_MODE": JSON.stringify(isDev),
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV) "process.env.NODE_ENV": JSON.stringify(env.NODE_ENV)
}, },
build: { build: {
// 输出路径 outDir: outputDir,
outDir: distDir,
emptyOutDir: false, emptyOutDir: false,
minify: true,
// 构建后是否生成 source map 文件 sourcemap: isSrcmap ? 'inline' : false,
sourcemap: isWatch ? 'inline' : false,
// 设置为 false 可以禁用最小化混淆
// 或是用来指定是应用哪种混淆器
// boolean | 'terser' | 'esbuild'
// 不压缩,用于调试
minify: !isWatch,
lib: { lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/index.ts"), entry: resolve(__dirname, "src/index.ts"),
// the proper extensions will be added
fileName: "index", fileName: "index",
formats: ["cjs"], formats: ["cjs"],
}, },
rollupOptions: { rollupOptions: {
plugins: [ plugins: [
...( ...(isDev ? [
isWatch ? [ livereload(outputDir),
livereload(devDistDir),
{ {
//监听静态资源文件
name: 'watch-external', name: 'watch-external',
async buildStart() { async buildStart() {
const files = await fg([ const files = await fg([
@ -109,12 +82,9 @@ export default defineConfig({
outDir: './', outDir: './',
outFileName: 'package.zip' outFileName: 'package.zip'
}) })
] ])
)
], ],
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ["siyuan", "process"], external: ["siyuan", "process"],
output: { output: {