nimiq-vitepress-theme
Version:
Nimiq UI theme for VitePress
66 lines (65 loc) • 1.76 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { GitChangelog } from "@nolebase/vitepress-plugin-git-changelog/vite";
import { groupIconVitePlugin } from "../code-groups/vite.mjs";
function getProjectVersion(rootDir = process.cwd()) {
try {
const packageJsonPath = path.resolve(rootDir, "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return packageJson.version;
}
} catch (error) {
console.warn("Failed to read version from package.json:", error);
}
return void 0;
}
export function NimiqVitepressVitePlugin({
repoURL,
contentPath = "",
gitChangelog
}) {
const { resolveId, configureServer, load, transform } = groupIconVitePlugin();
const externalPlugins = [];
if (gitChangelog !== false) {
const changelogConfig = gitChangelog || (repoURL ? { repoURL } : {});
if (Object.keys(changelogConfig).length > 0) {
externalPlugins.push(GitChangelog(changelogConfig));
}
}
const version = getProjectVersion();
const nimiqConfig = {
repoURL,
contentPath,
version
};
const plugins = [
{
name: "nimiq-vitepress-plugin",
enforce: "pre",
resolveId,
configureServer,
load,
transform,
config: () => ({
define: {
__NIMIQ_VITEPRESS_CONFIG__: JSON.stringify(nimiqConfig)
},
optimizeDeps: {
exclude: [
"nimiq-vitepress-theme",
"virtual:nolebase-git-changelog"
]
},
ssr: {
noExternal: [
"nimiq-vitepress-theme"
]
}
})
},
...externalPlugins
];
return plugins;
}