embersparks_obsidian-github.../esbuild.config.mjs
EmberSparks af98fdfe7d chore: 移除个人路径信息,提供通用 example 部署文件
- 从 git 跟踪移除含个人路径的部署脚本(deploy.bat, deploy-wsl.sh 等)
- 新增 deploy.example.bat 和 deploy-wsl.example.sh 供用户自定义
- .env.example 改为通用占位符路径
- CLAUDE.md 部署命令改用环境变量引用
- esbuild.config.mjs 移除 Node 22 不兼容的 setEncoding 调用和中文路径 hack
- .gitignore 补充 deploy-wsl.sh

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-06-12 14:45:21 +08:00

156 lines
5.2 KiB
JavaScript

import esbuild from "esbuild";
import process from "process";
import fs from "fs"; // Added for file system operations
import path from "path"; // Added for path manipulation
import { builtinModules } from "node:module";
function loadLocalEnvFile(envPath = ".env") {
if (!fs.existsSync(envPath)) return;
const envContent = fs.readFileSync(envPath, "utf8");
for (const line of envContent.split(/\r?\n/)) {
const trimmedLine = line.trim();
if (!trimmedLine || trimmedLine.startsWith("#")) continue;
const separatorIndex = trimmedLine.indexOf("=");
if (separatorIndex === -1) continue;
const key = trimmedLine.slice(0, separatorIndex).trim();
let value = trimmedLine.slice(separatorIndex + 1).trim();
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1);
}
if (key && process.env[key] === undefined) {
process.env[key] = value;
}
}
}
loadLocalEnvFile();
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === "production");
const nodeBuiltins = Array.from(new Set(builtinModules.flatMap(moduleName => {
return moduleName.startsWith("node:") ? [moduleName, moduleName.slice(5)] : [moduleName, `node:${moduleName}`];
})));
// esbuild plugin to handle copying files after build with backup
const copyPlugin = {
name: 'copy-to-obsidian',
setup: (build) => {
build.onEnd(result => {
if (result.errors.length > 0) {
console.error("Build failed with errors, skipping copy.");
return;
}
const obsidianPluginDir = process.env.OBSIDIAN_PLUGIN_DIR;
if (!obsidianPluginDir) {
console.warn("OBSIDIAN_PLUGIN_DIR environment variable not set. Plugin will not be automatically copied to Obsidian vault.");
return;
}
const pluginId = "github-stars-manager"; // Must match the 'id' in manifest.json
const targetDir = path.join(obsidianPluginDir, pluginId);
const backupDir = path.join(targetDir, "backup");
// Ensure target directory exists
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
// Ensure backup directory exists
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
// Function to backup and copy file
const backupAndCopy = (sourceFile, targetFile, fileName) => {
try {
// Create backup if target file exists
if (fs.existsSync(targetFile)) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupFile = path.join(backupDir, `${fileName}.${timestamp}.backup`);
fs.copyFileSync(targetFile, backupFile);
console.log(`Backed up ${fileName} to ${backupFile}`);
}
// Copy new file
fs.copyFileSync(sourceFile, targetFile);
console.log(`Copied ${fileName} to ${targetDir}`);
} catch (err) {
console.error(`Error copying ${fileName}:`, err);
}
};
// Copy main.js with backup
backupAndCopy("main.js", path.join(targetDir, "main.js"), "main.js");
// Copy styles.css with backup if it exists
if (fs.existsSync("styles.css")) {
backupAndCopy("styles.css", path.join(targetDir, "styles.css"), "styles.css");
}
// Copy themes.css with backup if it exists
if (fs.existsSync("themes.css")) {
backupAndCopy("themes.css", path.join(targetDir, "themes.css"), "themes.css");
}
// Copy manifest.json with backup
backupAndCopy("manifest.json", path.join(targetDir, "manifest.json"), "manifest.json");
console.log(`\n✅ Plugin files deployed to: ${targetDir}`);
console.log(`📦 Backups stored in: ${backupDir}`);
});
},
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/main.ts"], // Corrected entry point
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...nodeBuiltins],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
minify: prod,
plugins: [copyPlugin]
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}