mermaid/packages/mermaid/src/docs/vite.config.ts

100 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-02-24 18:33:51 +01:00
import { defineConfig, searchForWorkspaceRoot } from 'vite';
import type { PluginOption, Plugin } from 'vite';
2022-09-24 04:33:24 +02:00
import path from 'path';
2022-09-25 04:24:48 +02:00
import { SearchPlugin } from 'vitepress-plugin-search';
2023-02-24 18:33:51 +01:00
import fs from 'fs'
import Components from 'unplugin-vue-components/vite'
import Unocss from 'unocss/vite'
import { presetAttributify, presetIcons, presetUno } from 'unocss'
import { resolve } from 'pathe'
2022-09-24 04:33:24 +02:00
const virtualModuleId = 'virtual:mermaid-config';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
2022-09-22 00:40:49 +02:00
export default defineConfig({
2023-02-24 18:33:51 +01:00
optimizeDeps: {
// vitepress is aliased with replacement `join(DIST_CLIENT_PATH, '/index')`
// This needs to be excluded from optimization
exclude: ['vitepress'],
},
2022-09-24 04:33:24 +02:00
plugins: [
2023-02-24 18:33:51 +01:00
Components({
include: [/\.vue/, /\.md/],
dirs: '.vitepress/components',
dts: '.vitepress/components.d.ts',
}) as Plugin,
Unocss({
shortcuts: [
['btn', 'px-4 py-1 rounded inline-flex justify-center gap-2 text-white leading-30px children:mya !no-underline cursor-pointer disabled:cursor-default disabled:bg-gray-600 disabled:opacity-50'],
],
presets: [
presetUno({
dark: 'media',
}),
presetAttributify(),
presetIcons({
scale: 1.2,
}),
],
}) as unknown as Plugin,
IncludesPlugin(),
2023-02-09 06:58:27 +01:00
SearchPlugin() as PluginOption,
2022-09-24 04:33:24 +02:00
{
2022-09-25 04:24:48 +02:00
// TODO: will be fixed in the next vitepress release.
2022-09-24 04:33:24 +02:00
name: 'fix-virtual',
2023-02-18 18:05:14 +01:00
async resolveId(id: string) {
2022-09-24 04:33:24 +02:00
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
2023-02-18 18:05:14 +01:00
async load(this, id: string) {
2022-09-24 04:33:24 +02:00
if (id === resolvedVirtualModuleId) {
return `export default ${JSON.stringify({
securityLevel: 'loose',
startOnLoad: false,
})};`;
}
},
2023-02-18 18:05:14 +01:00
} as PluginOption,
2022-09-24 04:33:24 +02:00
],
2022-09-22 00:40:49 +02:00
resolve: {
alias: {
2022-10-18 00:32:47 +02:00
mermaid: path.join(__dirname, '../../dist/mermaid.esm.min.mjs'), // Use this one to build
'@mermaid-js/mermaid-example-diagram': path.join(
2022-11-14 17:36:26 +01:00
__dirname,
'../../../mermaid-example-diagram/dist/mermaid-example-diagram.esm.min.mjs'
2022-11-14 17:36:26 +01:00
), // Use this one to build
2022-09-22 00:40:49 +02:00
},
},
2022-10-15 05:50:11 +02:00
server: {
fs: {
allow: [
// search up for workspace root
searchForWorkspaceRoot(process.cwd()),
// Allow serving files from one level up to the project root
path.join(__dirname, '..'),
],
},
},
2022-09-22 00:40:49 +02:00
});
2023-02-24 18:33:51 +01:00
function IncludesPlugin(): Plugin {
return {
name: 'include-plugin',
enforce: 'pre',
transform(code: string, id: string): string | undefined {
let changed = false
code = code.replace(/\[@@include]\((.*?)\)/, (_: string, url: any): string => {
changed = true
const full = resolve(id, url)
return fs.readFileSync(full, 'utf-8')
})
if (changed) {
return code
}
},
} as Plugin
}