Skip to content

Commit fd547ea

Browse files
meteorlxyulivz
authored andcommitted
refactor: make the prepare process clearer and modify the resolve alias (#277)
1 parent 5231809 commit fd547ea

File tree

3 files changed

+108
-92
lines changed

3 files changed

+108
-92
lines changed

Diff for: lib/app/app.js

-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Content from './Content'
44
import ClientOnly from './ClientOnly'
55
import dataMixin from './dataMixin'
66
import store from './store'
7-
import NotFound from '@notFound'
87
import { routes } from '@temp/routes'
98
import { siteData } from '@temp/siteData'
109
import enhanceApp from '@temp/enhanceApp'
@@ -42,12 +41,6 @@ Vue.prototype.$withBase = function (path) {
4241
}
4342
}
4443

45-
// add 404 route
46-
routes.push({
47-
path: '*',
48-
component: NotFound
49-
})
50-
5144
export function createApp () {
5245
const router = new Router({
5346
base: siteData.base,

Diff for: lib/prepare.js

+104-83
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ async function writeTemp (file, content) {
1919
}
2020
}
2121

22+
async function writeEnhanceTemp (destName, srcPath) {
23+
await writeTemp(
24+
destName,
25+
fs.existsSync(srcPath)
26+
? `export { default } from ${JSON.stringify(srcPath)}`
27+
: `export default function () {}`
28+
)
29+
}
30+
2231
module.exports = async function prepare (sourceDir) {
2332
// 1. load options
2433
const options = await resolveOptions(sourceDir)
@@ -50,29 +59,12 @@ if (!Object.assign) Object.assign = require('object-assign')`
5059
const hasUserOverride = options.useDefaultTheme && fs.existsSync(overridePath)
5160
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
5261

53-
async function writeEnhanceTemp (destName, srcPath, isEnhanceExist) {
54-
await writeTemp(
55-
destName,
56-
isEnhanceExist
57-
? `export { default } from ${JSON.stringify(srcPath)}`
58-
: `export default function () {}`
59-
)
60-
}
61-
6262
// 6. handle enhanceApp.js
6363
const enhanceAppPath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
64-
writeEnhanceTemp(
65-
'enhanceApp.js',
66-
enhanceAppPath,
67-
fs.existsSync(enhanceAppPath)
68-
)
64+
await writeEnhanceTemp('enhanceApp.js', enhanceAppPath)
6965

7066
// 7. handle the theme enhanceApp.js
71-
writeEnhanceTemp(
72-
'themeEnhanceApp.js',
73-
options.themeEnhanceAppPath,
74-
fs.existsSync(options.themeEnhanceAppPath)
75-
)
67+
await writeEnhanceTemp('themeEnhanceApp.js', options.themeEnhanceAppPath)
7668

7769
return options
7870
}
@@ -84,6 +76,8 @@ async function resolveOptions (sourceDir) {
8476
const configTomlPath = path.resolve(vuepressDir, 'config.toml')
8577

8678
delete require.cache[configPath]
79+
80+
// resolve siteConfig
8781
let siteConfig = {}
8882
if (fs.existsSync(configYmlPath)) {
8983
siteConfig = await parseConfig(configYmlPath)
@@ -111,77 +105,79 @@ async function resolveOptions (sourceDir) {
111105
})
112106
}
113107

108+
// resolve outDir
109+
const outDir = siteConfig.dest
110+
? path.resolve(siteConfig.dest)
111+
: path.resolve(sourceDir, '.vuepress/dist')
112+
114113
// resolve theme
115114
const useDefaultTheme = (
116115
!siteConfig.theme &&
117116
!fs.existsSync(path.resolve(vuepressDir, 'theme'))
118117
)
119-
120-
// resolve algolia
121-
const themeConfig = siteConfig.themeConfig || {}
122-
const isAlgoliaSearch = (
123-
themeConfig.algolia ||
124-
Object.keys(siteConfig.locales && themeConfig.locales || {})
125-
.some(base => themeConfig.locales[base].algolia)
126-
)
127-
128-
const options = {
129-
siteConfig,
130-
sourceDir,
131-
outDir: siteConfig.dest
132-
? path.resolve(siteConfig.dest)
133-
: path.resolve(sourceDir, '.vuepress/dist'),
134-
publicPath: base,
135-
pageFiles: sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir })),
136-
pagesData: null,
137-
themePath: null,
138-
notFoundPath: null,
139-
useDefaultTheme,
140-
isAlgoliaSearch,
141-
markdown: createMarkdown(siteConfig)
142-
}
118+
const defaultThemePath = path.resolve(__dirname, 'default-theme')
119+
let themePath = null
120+
let themeLayoutPath = null
121+
let themeNotFoundPath = null
122+
let themeEnhanceAppPath = null
143123

144124
if (useDefaultTheme) {
145125
// use default theme
146-
options.themePath = path.resolve(__dirname, 'default-theme/Layout.vue')
147-
options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
126+
themePath = defaultThemePath
127+
themeLayoutPath = path.resolve(defaultThemePath, 'Layout.vue')
128+
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue')
148129
} else {
149-
let themeDir
150-
let themePath
151-
// resolve custom theme
130+
// resolve theme Layout
152131
if (siteConfig.theme) {
132+
// use external theme
153133
try {
154-
themePath = require.resolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`)
155-
themeDir = path.dirname(themePath)
134+
themeLayoutPath = require.resolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`)
135+
themePath = path.dirname(themeLayoutPath)
156136
} catch (e) {
157137
throw new Error(`[vuepress] Failed to load custom theme "${
158138
siteConfig.theme
159139
}". File vuepress-theme-${siteConfig.theme}/Layout.vue does not exist.`)
160140
}
161141
} else {
162-
themeDir = path.resolve(vuepressDir, 'theme')
163-
themePath = path.resolve(themeDir, 'Layout.vue')
164-
if (!fs.existsSync(themePath)) {
142+
// use custom theme
143+
themePath = path.resolve(vuepressDir, 'theme')
144+
themeLayoutPath = path.resolve(themePath, 'Layout.vue')
145+
if (!fs.existsSync(themeLayoutPath)) {
165146
throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`)
166147
}
167148
}
168-
options.themePath = themePath
169149

170-
const notFoundPath = path.resolve(themeDir, 'NotFound.vue')
171-
if (fs.existsSync(notFoundPath)) {
172-
options.notFoundPath = notFoundPath
173-
} else {
174-
options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
150+
// resolve theme NotFound
151+
themeNotFoundPath = path.resolve(themePath, 'NotFound.vue')
152+
if (!fs.existsSync(themeNotFoundPath)) {
153+
themeNotFoundPath = path.resolve(defaultThemePath, 'NotFound.vue')
175154
}
176155

177-
const themeEnhanceAppPath = path.resolve(themeDir, 'enhanceApp.js')
178-
if (fs.existsSync(themeEnhanceAppPath)) {
179-
options.themeEnhanceAppPath = themeEnhanceAppPath
156+
// resolve theme enhanceApp
157+
themeEnhanceAppPath = path.resolve(themePath, 'enhanceApp.js')
158+
if (!fs.existsSync(themeEnhanceAppPath)) {
159+
themeEnhanceAppPath = null
180160
}
181161
}
182162

183-
// resolve pages
184-
const pagesData = await Promise.all(options.pageFiles.map(async (file) => {
163+
// resolve theme config
164+
const themeConfig = siteConfig.themeConfig || {}
165+
166+
// resolve algolia
167+
const isAlgoliaSearch = (
168+
themeConfig.algolia ||
169+
Object.keys(siteConfig.locales && themeConfig.locales || {})
170+
.some(base => themeConfig.locales[base].algolia)
171+
)
172+
173+
// resolve markdown
174+
const markdown = createMarkdown(siteConfig)
175+
176+
// resolve pageFiles
177+
const pageFiles = sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir }))
178+
179+
// resolve pagesData
180+
const pagesData = await Promise.all(pageFiles.map(async (file) => {
185181
const data = {
186182
path: fileToPath(file)
187183
}
@@ -197,7 +193,7 @@ async function resolveOptions (sourceDir) {
197193
const headers = extractHeaders(
198194
frontmatter.content,
199195
['h2', 'h3'],
200-
options.markdown
196+
markdown
201197
)
202198
if (headers.length) {
203199
data.headers = headers
@@ -212,15 +208,32 @@ async function resolveOptions (sourceDir) {
212208
}))
213209

214210
// resolve site data
215-
options.siteData = {
211+
const siteData = {
216212
title: siteConfig.title || '',
217213
description: siteConfig.description || '',
218-
base: siteConfig.base || '/',
214+
base,
219215
pages: pagesData,
220216
themeConfig,
221217
locales: siteConfig.locales
222218
}
223219

220+
const options = {
221+
siteConfig,
222+
siteData,
223+
sourceDir,
224+
outDir,
225+
publicPath: base,
226+
pageFiles,
227+
pagesData,
228+
themePath,
229+
themeLayoutPath,
230+
themeNotFoundPath,
231+
themeEnhanceAppPath,
232+
useDefaultTheme,
233+
isAlgoliaSearch,
234+
markdown
235+
}
236+
224237
return options
225238
}
226239

@@ -279,30 +292,38 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
279292
const file = pageFiles[index]
280293
const filePath = path.resolve(sourceDir, file)
281294
let code = `
282-
{
283-
path: ${JSON.stringify(pagePath)},
284-
component: Theme,
285-
beforeEnter: (to, from, next) => {
286-
import(${JSON.stringify(filePath)}).then(comp => {
287-
Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
288-
next()
289-
})
290-
}
291-
}`
295+
{
296+
path: ${JSON.stringify(pagePath)},
297+
component: ThemeLayout,
298+
beforeEnter: (to, from, next) => {
299+
import(${JSON.stringify(filePath)}).then(comp => {
300+
Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
301+
next()
302+
})
303+
}
304+
}`
292305

293306
if (/\/$/.test(pagePath)) {
294-
code += `,{
295-
path: ${JSON.stringify(pagePath + 'index.html')},
296-
redirect: ${JSON.stringify(pagePath)}
297-
}`
307+
code += `,
308+
{
309+
path: ${JSON.stringify(pagePath + 'index.html')},
310+
redirect: ${JSON.stringify(pagePath)}
311+
}`
298312
}
299313

300314
return code
301315
}
302316

317+
const notFoundRoute = `,
318+
{
319+
path: '*',
320+
component: ThemeNotFound
321+
}`
322+
303323
return (
304-
`import Theme from '@theme'\n` +
305-
`export const routes = [${pages.map(genRoute).join(',')}\n]`
324+
`import ThemeLayout from '@themeLayout'\n` +
325+
`import ThemeNotFound from '@themeNotFound'\n` +
326+
`export const routes = [${pages.map(genRoute).join(',')}${notFoundRoute}\n]`
306327
)
307328
}
308329

Diff for: lib/webpack/createBaseConfig.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = function createBaseConfig ({
66
outDir,
77
publicPath,
88
themePath,
9-
notFoundPath,
9+
themeLayoutPath,
10+
themeNotFoundPath,
1011
isAlgoliaSearch,
1112
markdown
1213
}, { debug } = {}, isServer) {
@@ -36,7 +37,8 @@ module.exports = function createBaseConfig ({
3637
.set('symlinks', true)
3738
.alias
3839
.set('@theme', themePath)
39-
.set('@notFound', notFoundPath)
40+
.set('@themeLayout', themeLayoutPath)
41+
.set('@themeNotFound', themeNotFoundPath)
4042
.set('@source', sourceDir)
4143
.set('@app', path.resolve(__dirname, '../app'))
4244
.set('@temp', path.resolve(__dirname, '../app/.temp'))

0 commit comments

Comments
 (0)