fix(valaxy): do not need manual external addon

This commit is contained in:
YunYouJun 2023-08-21 01:07:10 +08:00
parent 4127610094
commit ff19ebe2bf
10 changed files with 16 additions and 72 deletions

View File

@ -327,30 +327,6 @@ Valaxy 决定通过插件中心化地提供各类封装好的评论组件和辅
## FAQ
如果您的主题适配了多个 Addon`valaxy-addon-waline`/`valaxy-addon-twikoo`),但用户并非都需要安装。
您需要将其添加至 `vite.build.rollupOptions.external` 中以避免引起编译问题
当用户没有主动安装对应 addon 时(即 addon 不存在的情况),则会默认重定向至一个空函数
譬如 `valaxy-theme-yun/valaxy.config.ts`:
```ts
import { defineValaxyTheme } from 'valaxy'
import type { ThemeConfig } from './types'
import { defaultThemeConfig, generateSafelist } from './node'
export default defineValaxyTheme<ThemeConfig>((options) => {
return {
themeConfig: defaultThemeConfig,
vite: {
build: {
rollupOptions: {
// add on demand external
external: ['valaxy-addon-twikoo', 'valaxy-addon-waline'],
},
},
plugins: [ThemeVitePlugin(options)],
},
unocss: {
safelist: generateSafelist(options),
},
}
})
```
因此,如果某个插件不是必须的,请在主题文档中提醒想要使用该功能的用户安装对应插件。

View File

@ -57,27 +57,3 @@ useTwikooWithOptions()
// custom twikoo style
</style>
```
#### External for On Demand
主题不依赖于 `valaxy-addon-twikoo` 的同时,对用户自行安装的 `valaxy-addon-twikoo` 进行适配,需要在 `valaxy.config.ts` 中配置 `external`
```ts
// valaxy.config.ts
export default defineValaxyTheme<ThemeConfig>((options) => {
return {
vite: {
build: {
rollupOptions: {
// add on demand external
external: [
// adapt for twikoo
'valaxy-addon-twikoo',
'valaxy-addon-twikoo/client/styles/index.scss',
],
},
},
},
}
})
```

View File

@ -32,11 +32,6 @@ export default defineTheme<ThemeConfig>((options) => {
return {
themeConfig: defaultThemeConfig,
vite: {
build: {
rollupOptions: {
external: ['valaxy-addon-algolia'],
},
},
plugins: [ThemeVitePlugin(options)],
},
}

View File

@ -5,21 +5,21 @@ import { defineAsyncComponent } from 'vue'
const runtimeConfig = useRuntimeConfig()
// not import from 'valaxy-addon-waline' to judge
const YunWaline = runtimeConfig.value.addons['valaxy-addon-waline']
? defineAsyncComponent(() => import('./YunWaline.vue'))
const AsyncYunWaline = runtimeConfig.value.addons['valaxy-addon-waline']
? defineAsyncComponent(() => import('./third/YunWaline.vue'))
: () => null
// todo: refactor
const YunTwikoo = runtimeConfig.value.addons['valaxy-addon-twikoo']
? defineAsyncComponent(() => import('./YunTwikoo.vue'))
const AsyncYunTwikoo = runtimeConfig.value.addons['valaxy-addon-twikoo']
? defineAsyncComponent(() => import('./third/YunTwikoo.vue'))
: () => null
</script>
<template>
<YunCard w="full" p="4" class="comment sm:p-6 lg:px-12 xl:px-16">
<ClientOnly>
<YunWaline />
<YunTwikoo />
<AsyncYunWaline />
<AsyncYunTwikoo />
</ClientOnly>
</YunCard>
</template>

View File

@ -31,17 +31,6 @@ export default defineValaxyTheme<ThemeConfig>((options) => {
return {
themeConfig: defaultThemeConfig,
vite: {
build: {
rollupOptions: {
// add on demand external
external: [
'valaxy-addon-algolia',
// adapt for twikoo
'valaxy-addon-twikoo',
'valaxy-addon-waline',
],
},
},
plugins: [ThemeVitePlugin(options)],
},
unocss: {

View File

@ -0,0 +1,2 @@
// compatibility for not exist addons
export default function () {}

View File

@ -172,6 +172,12 @@ export function getAlias(options: ResolvedValaxyOptions): AliasOptions {
})
})
// adapt for not exist addon
alias.push({
find: /^valaxy-addon-(.*)$/,
replacement: toAtFS(resolve(options.clientRoot, './addons/index.ts')),
})
// alias.push(...[
// { find: /^valaxy-addon-(.*)$/, replacement: toAtFS(resolve(options.themeRoot, '../valaxy-addon-$1/client/index.ts')) },
// ])