Skip to content

Commit 2c45337

Browse files
committed
2 parents c078156 + 436182c commit 2c45337

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

packages/crd-scripts/src/conf/getPrerenderRoutes.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const DirectoryTree = require('./node-directory-tree')
22

3-
// eg: ['docs/quick_start.md', 'a']
4-
// output: ['/quick_start', '/a/b', '/a/b/c']
5-
const getPrerenderRoutes = (cmd) => {
3+
const getDirTree = (cmd) => {
64
const dir = cmd.markdownPaths
75
const dirs = Array.isArray(dir) ? dir : [dir]
86
const otherProps = {
@@ -14,7 +12,14 @@ const getPrerenderRoutes = (cmd) => {
1412
path,
1513
options: otherProps,
1614
}))
17-
const result = getPrerenderRoute(dirTree)
15+
return dirTree
16+
}
17+
18+
// eg: ['docs/quick_start.md', 'a']
19+
// output: ['/quick_start', '/a/b', '/a/b/c']
20+
const getPrerenderRoutes = (dirTree) => {
21+
const dpCloneDirTree = JSON.parse(JSON.stringify(dirTree))
22+
const result = getPrerenderRoute(dpCloneDirTree)
1823
result.push('/404')
1924
return result
2025
}
@@ -32,7 +37,6 @@ function recursive(data, routePath, arr) {
3237

3338
if (item.type === 'directory') {
3439
if (item.children && item.children.length > 0) {
35-
// eslint-disable-next-line no-unused-vars
3640
item.children = recursive(item.children, composeRouteName, arr)
3741
} else {
3842
item.children = []
@@ -47,4 +51,7 @@ function recursive(data, routePath, arr) {
4751
return arr
4852
}
4953

50-
module.exports = getPrerenderRoutes
54+
module.exports = {
55+
getDirTree,
56+
getPrerenderRoutes
57+
}

packages/crd-scripts/src/conf/node-directory-tree.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ function directoryTree({
126126
} else if (stats.isDirectory()) {
127127
const dirData = safeReadDirSync(path)
128128
if (dirData === null) return null
129-
130129
item.children = dirData
131130
.map(child =>
132131
directoryTree({

packages/crd-scripts/src/conf/webpack.config.prod.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ const CopyMarkdownImageWebpackPlugin = require('copy-markdown-image-webpack-plug
77
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
88
const PrerenderSPAPlugin = require('crd-prerender-spa-plugin')
99
const { generateSiteMap } = require('crd-generator-sitemap')
10-
// const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
11-
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
1210
const fs = require('fs-extra')
1311
const { defaultHTMLPath, docsBuildDist } = require('crd-utils')
1412
const { getDocsConfig } = require('crd-utils')
1513
const config = require('./webpack.config')
1614
const paths = require('./path')
17-
const getPrerenderRoutes = require('./getPrerenderRoutes')
15+
const { getPrerenderRoutes, getDirTree } = require('./getPrerenderRoutes')
1816

1917
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer
2018

2119
module.exports = function (cmd) {
2220
const docsConfig = getDocsConfig()
21+
const dirTree = getDirTree(cmd)
22+
const routes = getPrerenderRoutes(dirTree)
23+
2324
config.mode = 'production'
2425
config.entry = [paths.appIndexJs]
2526
// config.output.filename = 'js/[hash:8].js'
2627
config.output.chunkFilename = 'js/[name].[hash:8].js'
2728
config.output.publicPath = docsConfig.repo ? `/${docsConfig.repo}/` : '/'
2829
config.output.path = docsConfig.repo ? `${docsBuildDist}/${docsConfig.repo}` : docsBuildDist
29-
3030
config.module.rules = config.module.rules.map((item) => {
3131
if (item.oneOf) {
3232
const loaders = []
@@ -121,8 +121,6 @@ module.exports = function (cmd) {
121121
// ],
122122
}
123123

124-
const routes = getPrerenderRoutes(cmd)
125-
126124
config.plugins = config.plugins.concat([
127125
new webpack.DefinePlugin({
128126
env: JSON.stringify('prod'),
@@ -169,8 +167,11 @@ module.exports = function (cmd) {
169167
// not use fs.move here or it'll throw error in github action
170168
await fs.copy(`${docsBuildDist}/${docsConfig.repo}`, docsBuildDist)
171169
await fs.remove(`${docsBuildDist}/${docsConfig.repo}`)
170+
const defaultPath = (dirTree.find(data => data.name === 'README.md')
171+
&& dirTree.find(data => data.name === 'README.md').mdconf
172+
&& dirTree.find(data => data.name === 'README.md').mdconf.abbrlink) || 'README'
172173
// move README as root index.html
173-
await fs.copy(`${docsBuildDist}/README/index.html`, `${docsBuildDist}/index.html`)
174+
await fs.copy(`${docsBuildDist}/${defaultPath}/index.html`, `${docsBuildDist}/index.html`)
174175
console.log('✅ generate prerender file success!')
175176
if (docsConfig.seo) {
176177
if (docsConfig.seo.google) {

0 commit comments

Comments
 (0)