feat: support config alias (#59)

pull/77/head
em2046 4 years ago committed by GitHub
parent f52b1d576b
commit 63a3691960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,7 @@ import slash from 'slash'
import fs from 'fs-extra'
import { APP_PATH, createResolver, SITE_DATA_REQUEST_PATH } from '../resolver'
import { BuildOptions } from './build'
import { SiteConfig } from '../config'
import { resolveUserConfig, SiteConfig } from '../config'
import { Plugin, OutputAsset, OutputChunk } from 'rollup'
import { createMarkdownToVueRenderFn } from '../markdownToVue'
import {
@ -34,7 +34,8 @@ export async function bundle(
options: BuildOptions
): Promise<[BuildResult, BuildResult, Record<string, string>]> {
const root = config.root
const resolver = createResolver(config.themeDir)
const userConfig = await resolveUserConfig(root)
const resolver = createResolver(config.themeDir, userConfig)
const markdownToVue = createMarkdownToVueRenderFn(root)
let isClientBuild = true

@ -17,6 +17,7 @@ export interface UserConfig<ThemeConfig = any> {
head?: HeadConfig[]
themeConfig?: ThemeConfig
locales?: Record<string, LocaleConfig>
alias?: Record<string, string>
// TODO locales support etc.
}
@ -37,6 +38,7 @@ const resolve = (root: string, file: string) =>
export async function resolveConfig(
root: string = process.cwd()
): Promise<SiteConfig> {
const userConfig = await resolveUserConfig(root)
const site = await resolveSiteData(root)
// resolve theme path
@ -53,13 +55,13 @@ export async function resolveConfig(
configPath: resolve(root, 'config.js'),
outDir: resolve(root, 'dist'),
tempDir: path.resolve(APP_PATH, 'temp'),
resolver: createResolver(themeDir)
resolver: createResolver(themeDir, userConfig)
}
return config
}
export async function resolveSiteData(root: string): Promise<SiteData> {
export async function resolveUserConfig(root: string) {
// load user config
const configPath = resolve(root, 'config.js')
const hasUserConfig = await fs.pathExists(configPath)
@ -72,6 +74,12 @@ export async function resolveSiteData(root: string): Promise<SiteData> {
debug(`no config file found.`)
}
return userConfig
}
export async function resolveSiteData(root: string): Promise<SiteData> {
const userConfig = await resolveUserConfig(root)
return {
lang: userConfig.lang || 'en-US',
title: userConfig.title || 'VitePress',

@ -1,5 +1,6 @@
import path from 'path'
import { Resolver } from 'vite'
import { UserConfig } from './config'
export const APP_PATH = path.join(__dirname, '../client/app')
export const SHARED_PATH = path.join(__dirname, '../client/shared')
@ -15,9 +16,13 @@ export const SITE_DATA_REQUEST_PATH = '/' + SITE_DATA_ID
// so that we can resolve custom requests that start with /@app or /@theme
// we also need to map file paths back to their public served paths so that
// vite HMR can send the correct update notifications to the client.
export function createResolver(themeDir: string): Resolver {
export function createResolver(
themeDir: string,
userConfig: UserConfig
): Resolver {
return {
alias: {
...userConfig.alias,
'/@app/': APP_PATH,
'/@theme/': themeDir,
'/@shared/': SHARED_PATH,

Loading…
Cancel
Save