Skip to content

Commit 983fdbd

Browse files
Merge branch 'main' into dev
2 parents 912e841 + f94bd91 commit 983fdbd

File tree

9 files changed

+55
-35
lines changed

9 files changed

+55
-35
lines changed

.vitepress/rewrite-title/index.js

+22-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path')
2-
const fs = require('fs')
2+
const fsp = require('fs').promises
33
const matterService = require('../utils/frontmatter-service')
44
const workspacePath = path.resolve(__dirname, '..', '..')
55

@@ -17,33 +17,28 @@ const rewriteMarkdownTitle = (filePath) => {
1717
matter.set('title', title).save()
1818
}
1919

20-
const ergodicDirectory = (dirPath) => {
21-
fs.readdir(dirPath, (err, files) => {
22-
if (err) {
23-
console.warn('Directory reading failed !')
24-
return
25-
}
26-
27-
files.forEach((file) => {
28-
const filePath = path.join(dirPath, file)
29-
fs.stat(filePath, (err, stats) => {
30-
if (err) {
31-
console.warn('File status reading failed !')
32-
return
20+
const ergodicDirectory = async (dirPath) => {
21+
try {
22+
const files = await fsp.readdir(dirPath)
23+
for (let i = 0; i < files.length; i++) {
24+
const file = files[i],
25+
filePath = path.join(dirPath, file)
26+
const stats = await fsp.stat(filePath)
27+
if (stats.isFile()) {
28+
if (filePath.split('.').pop().toLowerCase() === 'md') {
29+
rewriteMarkdownTitle(filePath)
3330
}
34-
35-
if (stats.isFile()) {
36-
if (filePath.split('.').pop().toLowerCase() === 'md') {
37-
rewriteMarkdownTitle(filePath)
38-
}
39-
} else if (stats.isDirectory()) {
40-
if (articleDirs.includes(filePath.split('/').pop())) {
41-
ergodicDirectory(filePath)
42-
}
31+
} else if (stats.isDirectory()) {
32+
if (articleDirs.includes(filePath.split('/').pop())) {
33+
await ergodicDirectory(filePath)
4334
}
44-
})
45-
})
46-
})
35+
}
36+
}
37+
} catch (err) {
38+
console.warn(
39+
`vite-docs-cn: failed to rewrite frontmatter for titles.\n ${err}!`
40+
)
41+
}
4742
}
4843

49-
ergodicDirectory(workspacePath)
44+
module.exports = () => ergodicDirectory(workspacePath)

.vitepress/theme/fetchReleaseTag.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ export default function fetchReleaseTag() {
1313
const releaseTagName = json.tag_name
1414
docsReleaseTag.innerText = releaseTagName
1515

16-
17-
mainTitle.appendChild(docsReleaseTag)
16+
if (releaseTagName !== undefined) {
17+
mainTitle.appendChild(docsReleaseTag)
18+
}
1819
})
1920
})
2021
}

guide/api-plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Vite 插件扩展了设计出色的 Rollup 接口,带有一些 Vite 独有的
66

77
## 约定 {#conventions}
88

9-
如果插件不使用 Vite 特有的钩子,可以实现为 [兼容的 Rollup 插件](#Rollup-插件兼容性),推荐使用 [Rollup 插件名称约定](https://rollupjs.org/guide/en/#conventions)
9+
如果插件不使用 Vite 特有的钩子,可以实现为 [兼容的 Rollup 插件](#rollup-plugin-compatibility),推荐使用 [Rollup 插件名称约定](https://rollupjs.org/guide/en/#conventions)
1010

1111
- Rollup 插件应该有一个带 `rollup-plugin-` 前缀、语义清晰的名称。
1212
- 在 package.json 中包含 `rollup-plugin``vite-plugin` 关键字。

guide/assets.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import InlineWorker from './shader.js?worker&inline'
8585

8686
## new URL(url, import.meta.url)
8787

88-
[import.meta.url](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta) 是一个 ESM 的原生功能,会暴露当前模块的 URL。将它与原生的 [URL 构造器](https://developer.mozilla.org/en-US/docs/Web/API/URL) 结合,传入静态资源与本模块相对的地址,即可得到被完整解析的 URL:
88+
[import.meta.url](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta) 是一个 ESM 的原生功能,会暴露当前模块的 URL。将它与原生的 [URL 构造器](https://developer.mozilla.org/en-US/docs/Web/API/URL) 组合使用,在一个 JavaScript 模块中,通过相对路径我们就能得到一个被完整解析的静态资源 URL:
8989

9090
```js
9191
const imgUrl = new URL('./img.png', import.meta.url)

guide/build.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898

9999
当你开发面向浏览器的库时,你可能会将大部分时间花在该库的测试/演示页面上。在 Vite 中你可以使用 `index.html` 获得如丝般顺滑的开发体验。
100100

101-
当你以发布为目的构建库时,请使用 [`build.lib` 配置项](/config/#build-lib),以确保将那些你不想打包进库的依赖进行外部化处理,例如 `vue``react`
101+
当这个库要进行发布构建时,请使用 [`build.lib` 配置项](/config/#build-lib),以确保将那些你不想打包进库的依赖进行外部化处理,例如 `vue``react`
102102

103103
```js
104104
// vite.config.js

guide/env-and-mode.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Vite 在一个特殊的 **`import.meta.env`** 对象上暴露环境变量。这里有一些在所有情况下都可以使用的内建变量:
66

7-
- **`import.meta.env.MODE`**: {string} 应用运行的[模式](#模式)
7+
- **`import.meta.env.MODE`**: {string} 应用运行的[模式](#modes)
88

99
- **`import.meta.env.BASE_URL`**: {string} 部署应用时的基本 URL。他由[`base` 配置项](/config/#base)决定。
1010

guide/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ npx degit user/project#main my-project
9696

9797
Vite 将 `index.html` 视为源码和模块图的一部分。Vite 解析 `<script type="module" src="...">` ,这个标签指向你的 JavaScript 源码。甚至内联引入 JavaScript 的 `<script type="module">` 和引用 CSS 的 `<link href>` 也能利用 Vite 特有的功能被解析。另外,`index.html` 中的 URL 将被自动转换,因此不再需要 `%PUBLIC_URL%` 占位符了。
9898

99-
与静态 HTTP 服务器类似,Vite 也有 “根目录” 的概念,即文件被提供的位置。你会看到它在整个文档中用 `<root>` 表示。源码中的绝对 URL 路径将以项目的 “根” 作为基础来解析,因此你可以像在普通的静态文件服务器上一样编写代码(并且功能更强大!)。Vite 还能够处理依赖关系,解析处于根目录外的文件位置,这使得它即使在基于 monorepo 的方案中也十分有用。
99+
与静态 HTTP 服务器类似,Vite 也有 “根目录” 的概念,即服务文件的位置,在接下来的文档中你将看到它会以 `<root>` 代称。源码中的绝对 URL 路径将以项目的 “根” 作为基础来解析,因此你可以像在普通的静态文件服务器上一样编写代码(并且功能更强大!)。Vite 还能够处理依赖关系,解析处于根目录外的文件位置,这使得它即使在基于 monorepo 的方案中也十分有用。
100100

101101
Vite 也支持多个 `.html` 作入口点的 [多页面应用模式](./build#multi-page-app)
102102

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"scripts": {
1515
"dev": "vitepress dev .",
16-
"build": "node ./.vitepress/rewrite-title/ && vitepress build .",
16+
"build": "node ./scripts/build.js",
1717
"serve": "vitepress serve ."
1818
},
1919
"gitHooks": {

scripts/build.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const vitpress = require('vitepress')
2+
const path = require('path')
3+
const chalk = require('chalk')
4+
5+
/** 构建说明:
6+
*
7+
* 所有需要的构建步骤,都应封装为返回 Promise 的函数
8+
* 保证不会因为任何异步操作导致构建错误或失败
9+
* (若有顺序必要,则依次调用)
10+
*/
11+
12+
const rewriteTitle = require('../.vitepress/rewrite-title')
13+
14+
rewriteTitle().then(() => {
15+
vitpress
16+
.build(path.resolve(__dirname, '..'))
17+
.then(() => {
18+
chalk.green('build success!')
19+
})
20+
.catch((err) => {
21+
console.error(chalk.red(`build error:\n`), err)
22+
process.exit(1)
23+
})
24+
})

0 commit comments

Comments
 (0)