Skip to content

Commit

Permalink
feat: support config alias (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
em2046 committed Sep 3, 2020
1 parent f52b1d5 commit 63a3691
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/node/build/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion src/node/resolver.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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,
Expand Down

0 comments on commit 63a3691

Please sign in to comment.