Skip to content

Commit

Permalink
feat: addons enhancements & code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WRXinYue committed Sep 12, 2024
1 parent 7f14798 commit 3ca15cc
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// Enable the ESlint flat config support
"eslint.experimental.useFlatConfig": true,
"eslint.useFlatConfig": true,
// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,
Expand Down
74 changes: 41 additions & 33 deletions packages/valaxy/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
resolveValaxyConfigFromRoot,
} from './config'
import type { ValaxyAddonResolver, ValaxyNodeConfig } from './types'
import { parseAddons } from './utils/addons'
import { parseAddons, sortAddons } from './utils/addons'
import { getThemeRoot } from './utils/theme'
import { resolveSiteConfig } from './config/site'
import { countPerformanceTime } from './utils/performance'
Expand Down Expand Up @@ -61,7 +61,7 @@ export interface ResolvedValaxyOptions<ThemeConfig = DefaultTheme.Config> {
*/
themeRoot: string
/**
* Addon root path
* Addon root pathuserValaxyConfig
*/
addonRoots: string[]
/**
Expand Down Expand Up @@ -132,15 +132,30 @@ async function processSiteConfig(options: ResolvedValaxyOptions) {
/**
* Post process valaxyOptions
* @param valaxyOptions
* @param valaxyConfig
* @param userValaxyConfig
*/
export async function processValaxyOptions(valaxyOptions: ResolvedValaxyOptions, valaxyConfig: ValaxyNodeConfig) {
const { clientRoot, themeRoot, userRoot } = valaxyOptions
export async function processThemeValaxyOptions(valaxyOptions: ResolvedValaxyOptions, userValaxyConfig?: ValaxyNodeConfig) {
const { userRoot, clientRoot, themeRoot } = valaxyOptions

// resolve addon valaxyConfig
const addons = await parseAddons(valaxyConfig.addons || [], valaxyOptions.userRoot)
const addonsValaxyConfig = await resolveAddonsConfig(addons, valaxyOptions)
valaxyConfig = mergeValaxyConfig(valaxyConfig, addonsValaxyConfig)
if (!userValaxyConfig)
userValaxyConfig = valaxyOptions.config

const userAddons = await parseAddons(userValaxyConfig?.addons || [], userRoot)
const userAddonRoots = userAddons.map(({ root }) => root)
const roots = uniq([clientRoot, themeRoot, ...userAddonRoots, userRoot])

valaxyOptions.addons = userAddons
valaxyOptions.addonRoots = userAddonRoots
valaxyOptions.roots = roots

const themeValaxyConfig = await resolveThemeValaxyConfig(valaxyOptions)
let valaxyConfig = mergeValaxyConfig(userValaxyConfig, themeValaxyConfig)
const userAddonsValaxyConfig = await resolveAddonsConfig(userAddons, valaxyOptions)

// resolve theme valaxy.config.ts and merge theme
const themeAddons = await parseAddons(themeValaxyConfig.addons || [], themeRoot)
const themeAddonsValaxyConfig = await resolveAddonsConfig(themeAddons, valaxyOptions)
valaxyConfig = mergeValaxyConfig(valaxyConfig, userAddonsValaxyConfig, themeAddonsValaxyConfig)

const config = defu(valaxyConfig, defaultValaxyConfig)
valaxyOptions.config = {
Expand All @@ -153,6 +168,9 @@ export async function processValaxyOptions(valaxyOptions: ResolvedValaxyOptions,
},
},
}

const [preAddons, normalAddons, postAddons] = sortAddons([...userAddons, ...themeAddons])
const addons = uniq([...preAddons, ...normalAddons, ...postAddons])
valaxyOptions.addons = addons

addons.forEach((addon) => {
Expand All @@ -163,8 +181,7 @@ export async function processValaxyOptions(valaxyOptions: ResolvedValaxyOptions,
const addonNames = addons.map(({ name }) => name)
valaxyOptions.addonRoots = addonRoots
// ensure order
valaxyOptions.roots = uniq([clientRoot, themeRoot, ...addonRoots, userRoot])

valaxyOptions.roots = uniq([...roots, ...addonRoots])
// when addon be used, remove it from external
const external = valaxyOptions.config.vite?.build?.rollupOptions?.external as string[] || []
valaxyOptions.config.vite!.build!.rollupOptions!.external = external.filter(name => !addonNames.includes(name))
Expand All @@ -184,30 +201,23 @@ export async function resolveOptions(
const userRoot = resolve(options.userRoot || process.cwd())

consola.start(`Resolve ${magenta('valaxy')} config...`)

const [resolvedValaxy, resolvedSite, resolvedTheme, pages] = await Promise.all([
resolveValaxyConfig(options),
resolveSiteConfig(options.userRoot),
// resolveThemeConfig(options),
resolveThemeConfigFromRoot(options.userRoot),

fg(['**.md'], {
cwd: resolve(userRoot, 'pages'),
ignore: ['**/node_modules'],
}),
fg(['**.md'], { cwd: resolve(userRoot, 'pages'), ignore: ['**/node_modules'] }),
])

let { config: userValaxyConfig, configFile, theme } = resolvedValaxy
const { config: userValaxyConfig, theme } = resolvedValaxy
const { siteConfig: userSiteConfig } = resolvedSite
const { config: userThemeConfig } = resolvedTheme

const redirects = collectRedirects(userSiteConfig.redirects?.rules)
const themeRoot = getThemeRoot(theme, options.userRoot)

const { siteConfig, siteConfigFile } = resolvedSite

const { config: themeConfig, configFile: themeConfigFile } = resolvedTheme

const redirects = collectRedirects(siteConfig.redirects?.rules)

// merge with valaxy
userValaxyConfig = defu<ValaxyNodeConfig, any>({ siteConfig }, { themeConfig }, userValaxyConfig)
const userValaxyOptionsConfig = defu<ValaxyNodeConfig, any>({ siteConfig: userSiteConfig }, { themeConfig: userThemeConfig }, userValaxyConfig)

// pages
// Important: fast-glob doesn't guarantee order of the returned files.
Expand All @@ -228,7 +238,7 @@ export async function resolveOptions(
roots: [],
theme,
config: {
...userValaxyConfig,
...userValaxyOptionsConfig,
runtimeConfig: {
addons: {},
redirects: {
Expand All @@ -237,9 +247,9 @@ export async function resolveOptions(
},
},
},
configFile: configFile || '',
siteConfigFile: siteConfigFile || '',
themeConfigFile: themeConfigFile || '',
configFile: resolvedValaxy.configFile || '',
siteConfigFile: resolvedSite.siteConfigFile || '',
themeConfigFile: resolvedTheme.configFile || '',
pages: pages.sort(),
addons: [],
redirects,
Expand All @@ -248,13 +258,11 @@ export async function resolveOptions(
links: [],
},
}

debug(valaxyOptions)

// resolve theme valaxy.config.ts and merge theme
const themeValaxyConfig = await resolveThemeValaxyConfig(valaxyOptions)
const valaxyConfig = mergeValaxyConfig(userValaxyConfig, themeValaxyConfig)

valaxyOptions = await processValaxyOptions(valaxyOptions, valaxyConfig)
valaxyOptions = await processThemeValaxyOptions(valaxyOptions, userValaxyConfig)

// ensure .valaxy folder to store temp files, like d.ts
fs.ensureDirSync(valaxyOptions.tempDir)
Expand Down
8 changes: 3 additions & 5 deletions packages/valaxy/node/plugins/valaxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import type { DefaultTheme, Pkg, SiteConfig } from 'valaxy/types'
import { dim, yellow } from 'picocolors'
import type { RouteRecordRaw } from 'vue-router'
import consola from 'consola'
import { defaultSiteConfig, mergeValaxyConfig, resolveSiteConfig, resolveUserThemeConfig } from '../../config'
import { defaultSiteConfig, resolveSiteConfig, resolveUserThemeConfig } from '../../config'
import type { ResolvedValaxyOptions, ValaxyServerOptions } from '../../options'
import { processValaxyOptions, resolveOptions, resolveThemeValaxyConfig } from '../../options'
import { processThemeValaxyOptions, resolveOptions } from '../../options'
import { resolveImportPath, toAtFS } from '../../utils'
import type { ValaxyNodeConfig } from '../../types'
import { vLogger } from '../../logger'
Expand Down Expand Up @@ -294,9 +294,7 @@ export async function createValaxyLoader(options: ResolvedValaxyOptions, serverO
}

if (file === resolve(options.themeRoot, 'valaxy.config.ts')) {
const themeValaxyConfig = await resolveThemeValaxyConfig(options)
const valaxyConfig = mergeValaxyConfig(options.config, themeValaxyConfig)
const { config } = await processValaxyOptions(options, valaxyConfig)
const { config } = await processThemeValaxyOptions(options)
return reloadConfigAndEntries(config)
}

Expand Down
1 change: 1 addition & 0 deletions packages/valaxy/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface ValaxyAddonResolver {
options: Record<string, any>
configFile?: string
pkg: Record<string, any>
enforce?: 'pre' | 'post'

setup?: (node: ValaxyNode) => void
}
18 changes: 18 additions & 0 deletions packages/valaxy/node/utils/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ export async function parseAddons(addons: ValaxyAddons, userRoot = process.cwd()
return resolvedAddons
}

export function sortAddons(addons: (ValaxyAddonResolver | ValaxyAddonResolver[])[] | undefined) {
const preAddons: ValaxyAddonResolver[] = []
const postAddons: ValaxyAddonResolver[] = []
const normalAddons: ValaxyAddonResolver[] = []

if (addons) {
addons.flat().forEach((p) => {
if (p.enforce === 'pre')
preAddons.push(p)
else if (p.enforce === 'post')
postAddons.push(p)
else normalAddons.push(p)
})
}

return [preAddons, normalAddons, postAddons]
}

/**
* read module from addon name
* @internal
Expand Down
1 change: 1 addition & 0 deletions packages/valaxy/types/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface ValaxyAddon<AddonOptions = Record<string, any>> {
global?: boolean
props?: Record<string, any>
options?: AddonOptions
enforce?: 'pre' | 'post'
}

0 comments on commit 3ca15cc

Please sign in to comment.