Skip to content

Commit f322105

Browse files
ulivzyyx990803
authored andcommitted
feat: support built-in pug config and document using pro-processors at component (#151)
1 parent d136e22 commit f322105

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

docs/guide/using-vue.md

+29
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ Inside any markdown file you can then directly use the components (names are inf
121121
Make sure a custom component's name either contains a hyphen or is in PascalCase. Otherwise it will be treated as an inline element and wrapped inside a `<p>` tag, which will lead to hydration mismatch because `<p>` does not allow block elements to be placed inside it.
122122
:::
123123

124+
### Using Pre-processors
125+
126+
VuePress has built-in webpack config for the following pre-processors: `sass`, `scss`, `less`, `stylus` and `pug`. All you need to do is installing the corresposnding dependencies. For example, to enable `sass`, install the following in your project:
127+
128+
``` bash
129+
yarn add -D sass-loader node-sass
130+
```
131+
132+
Now you can use the following in markdown and theme components:
133+
134+
``` vue
135+
<style lang="sass">
136+
.title
137+
font-size: 20px
138+
</style>
139+
```
140+
141+
Using `<template lang="pug">` requires installing `pug` and `pug-plain-loader`:
142+
143+
``` bash
144+
yarn add -D pug pug-plain-loader
145+
```
146+
147+
::: tip
148+
If you are a Stylus user, you don't need to install `stylus` and `stylus-loader` in your project because VuePress uses Stylus internally.
149+
150+
For pre-processors that do not have built-in webpack config support, you will need to [extend the internal webpack config](../config/#configurewebpack) in addition to installing the necessary dependencies.
151+
:::
152+
124153
## Script & Style Hoisting
125154

126155
Sometimes you may need to apply some JavaScript or CSS only to the current page. In those cases you can directly write root-level `<script>` or `<style>` blocks in the markdown file, and they will be hoisted out of the compiled HTML and used as the `<script>` and `<style>` blocks for the resulting Vue single-file component.

docs/zh/guide/using-vue.md

+31
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ export default {
121121
请确保一个自定义组件的名字包含连接符或者是 PascalCase,否则,它将会被视为一个内联元素,并被包裹在一个 `<p>` 标签中,这将会导致 HTML 渲染紊乱,因为 HTML 标准规定, `<p>` 标签中不允许放置任何块级元素。
122122
:::
123123

124+
125+
## 使用预处理器
126+
127+
VuePress 对以下预处理器已经内置相关的 webpack 配置:`sass``scss``less``stylus``pug`。要使用它们你只需要在项目中安装对应的依赖即可。例如,要使用 `sass`,需要安装:
128+
129+
```bash
130+
yarn add -D sass-loader node-sass
131+
```
132+
133+
然后你就可以在 Markdown 或是组件中使用如下代码:
134+
135+
```vue
136+
<style lang="sass">
137+
.title
138+
font-size: 20px
139+
</style>
140+
```
141+
142+
要在组件中使用 `<template lang="pug">`,则需要安装 `pug``pug-plain-loader`:
143+
144+
```bash
145+
yarn add -D pug pug-plain-loader
146+
```
147+
148+
::: tip
149+
需要指出的是,如果你是一个 `stylus` 用户,你并不需要在你的项目中安装 `stylus``stylus-loader`,因为 VuePress 已经内置了它们。
150+
151+
对于那些没有内置的预处理器,除了安装对应的依赖,你还需要 [拓展内部的 Webpack 配置](../config/#configurewebpack)
152+
:::
153+
154+
124155
## 脚本和样式提升
125156

126157
有时,你可以只想在当前页面应用一些 JavaScript 或者 CSS,在这种情况下,你可以直接在 Markdown 文件中使用原生的 `<script>` 或者 `<style>` 标签,它们将会从编译后的 HTML 文件中提取出来,并作为生成的 Vue 单文件组件的 `<script>``<style>` 标签。

lib/webpack/createBaseConfig.js

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ module.exports = function createBaseConfig ({
7171
}
7272
})
7373

74+
config.module
75+
.rule('pug')
76+
.test(/\.pug$/)
77+
.use('pug-plain-loader')
78+
.loader('pug-plain-loader')
79+
.end()
80+
7481
if (!siteConfig.evergreen) {
7582
const libDir = path.join(__dirname, '..')
7683
config.module

0 commit comments

Comments
 (0)