Skip to content

Commit

Permalink
🔧 ci: 调整 vite 脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime committed Oct 7, 2024
1 parent 7861952 commit 2ff10bf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 90 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"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",
"update-version": "node --no-warnings ./scripts/update_version.js",
"dev": "vite build --watch",
"dev-srcmap": "vite build --watch --sourcemap=inline",
"build": "vite build",
"make-install": "vite build && node --no-warnings ./scripts/make_install.js"
"dev": "cross-env NODE_ENV=development VITE_SOURCEMAP=inline vite build --watch",
"build": "cross-env NODE_ENV=production vite build",
"build-srcmap": "cross-env NODE_ENV=production VITE_SOURCEMAP=inline vite build",
"make-install": "cross-env NODE_ENV=production vite build && node --no-warnings ./scripts/make_install.js"
},
"devDependencies": {
"@types/node": "^20.3.0",
Expand All @@ -32,7 +32,8 @@
"vite": "^5.2.13",
"vite-plugin-solid": "^2.10.2",
"vite-plugin-static-copy": "^1.0.5",
"vite-plugin-zip-pack": "^1.2.2"
"vite-plugin-zip-pack": "^1.2.2",
"cross-env": "^7.0.3"
},
"dependencies": {
"solid-transition-group": "^0.2.3"
Expand Down
146 changes: 61 additions & 85 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { resolve } from "path"
import { defineConfig, loadEnv } from "vite"
import minimist from "minimist"
import { viteStaticCopy } from "vite-plugin-static-copy"
import livereload from "rollup-plugin-livereload"
import solidPlugin from 'vite-plugin-solid';
Expand All @@ -9,86 +8,65 @@ import fg from 'fast-glob';

import vitePluginYamlI18n from './yaml-plugin';

const args = minimist(process.argv.slice(2));
const isWatch = args.watch || args.w || false;
const isSrcmap = args.sourcemap || false;
const devDistDir = "dev";
const distDir = isWatch ? devDistDir : "dist";
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const isSrcmap = env.VITE_SOURCEMAP === 'inline';
const isDev = mode === 'development';
const distDir = isDev ? "dev" : "dist";

console.log("isWatch=>", isWatch);
console.log("isSrcmap=>", isSrcmap);
console.log("isDev=>", isDev);
console.log("isSrcmap=>", isSrcmap);

export default defineConfig({
resolve: {
alias: {
"@": resolve(__dirname, "src"),
}
},

plugins: [
solidPlugin({
babel: {
plugins: ['solid-styled-jsx/babel']
return {
resolve: {
alias: {
"@": resolve(__dirname, "src"),
}
}),

vitePluginYamlI18n({
inDir: 'public/i18n',
outDir: `${distDir}/i18n`
}),
},

viteStaticCopy({
targets: [
{
src: "./README*.md",
dest: "./",
},
{
src: "./plugin.json",
dest: "./",
},
{
src: "./preview.png",
dest: "./",
},
{
src: "./icon.png",
dest: "./",
plugins: [
solidPlugin({
babel: {
plugins: ['solid-styled-jsx/babel']
}
],
}),
],
}),

// 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: {
"process.env.DEV_MODE": `"${isWatch}"`,
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
},
vitePluginYamlI18n({
inDir: 'public/i18n',
outDir: `${distDir}/i18n`
}),

build: {
// 输出路径
outDir: distDir,
emptyOutDir: false,
viteStaticCopy({
targets: [
{ src: "./README*.md", dest: "./" },
{ src: "./plugin.json", dest: "./" },
{ src: "./preview.png", dest: "./" },
{ src: "./icon.png", dest: "./" }
],
}),
],

minify: !isWatch || (isWatch && (isSrcmap === 'inline')),

lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/index.ts"),
// the proper extensions will be added
fileName: "index",
formats: ["cjs"],
define: {
"process.env.DEV_MODE": JSON.stringify(isDev),
"process.env.NODE_ENV": JSON.stringify(env.NODE_ENV)
},
rollupOptions: {
plugins: [
...(
isWatch ? [
livereload(devDistDir),

build: {
outDir: distDir,
emptyOutDir: false,
minify: true,
sourcemap: isSrcmap ? 'inline' : false,

lib: {
entry: resolve(__dirname, "src/index.ts"),
fileName: "index",
formats: ["cjs"],
},
rollupOptions: {
plugins: [
...(isDev ? [
livereload(distDir),
{
//监听静态资源文件
name: 'watch-external',
async buildStart() {
const files = await fg([
Expand All @@ -107,23 +85,21 @@ export default defineConfig({
outDir: './',
outFileName: 'package.zip'
})
]
)
],
])
],

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

output: {
entryFileNames: "[name].js",
assetFileNames: (assetInfo) => {
if (assetInfo.name === "style.css") {
return "index.css"
}
return assetInfo.name
output: {
entryFileNames: "[name].js",
assetFileNames: (assetInfo) => {
if (assetInfo.name === "style.css") {
return "index.css"
}
return assetInfo.name
},
},
},
},
}
}
})

0 comments on commit 2ff10bf

Please sign in to comment.