chore: improve ui build scripts

This commit is contained in:
Fu Diwei
2026-07-30 14:16:49 +08:00
parent 7c36e6f98f
commit bd6d1266b5
8 changed files with 78 additions and 70 deletions
@@ -0,0 +1,45 @@
const targetAttrs = ["href", "src"];
const getStaticPrefix = (node) => {
if (!node) {
return;
}
if (node.type === "Literal") {
return typeof node.value === "string" ? node.value : void 0;
}
if (node.type === "TemplateLiteral") {
return node.quasis[0]?.value.raw;
}
if (node.type === "BinaryExpression" && node.operator === "+") {
return getStaticPrefix(node.left);
}
};
export default {
meta: {
type: "problem",
schema: [],
},
create(context) {
return {
JSXAttribute(node) {
const attribute = node.name.type === "JSXIdentifier" ? node.name.name : "";
if (!targetAttrs.includes(attribute)) {
return;
}
const value = node.value?.type === "JSXExpressionContainer" ? getStaticPrefix(node.value.expression) : getStaticPrefix(node.value);
if (!!value && value.startsWith("/") && !value.startsWith("//")) {
context.report({
node: node.value ?? node,
message: "Root-relative {{attribute}} bypasses the application base path. Wrap the URL with `withBasePath()` utility function.",
data: { attribute },
});
}
},
};
},
};
@@ -0,0 +1,40 @@
import path from "node:path";
import fs from "fs-extra";
import { type Plugin } from "vite";
interface Options {
files: string[];
}
export default function (options: Options): Plugin {
const tmpdir = path.resolve("./", "node_modules/.vite/certimate-temp");
return {
name: "preserve-files",
apply: "build",
buildStart() {
// 在构建开始时将要保留的文件或目录移动到临时位置
options.files.forEach((file) => {
const src = path.resolve("./", file);
const dist = path.resolve(tmpdir, file);
if (fs.existsSync(src)) {
fs.moveSync(src, dist, { overwrite: true });
}
});
},
closeBundle() {
// 在构建完成后将临时位置的文件或目录移回原来的位置
options.files.forEach((file) => {
const src = path.resolve("./", file);
const dist = path.resolve(tmpdir, file);
if (fs.existsSync(dist)) {
fs.moveSync(dist, src, { overwrite: true });
}
});
// 清理临时目录
fs.emptyDirSync(tmpdir);
},
};
}