|
2 | 2 | sidebar: auto
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# Design Concepts of VuePress 1.0 |
| 5 | +# Design Concepts of VuePress 1.x |
6 | 6 |
|
7 |
| -- TODO |
| 7 | +The design concepts of VuePress 1.x are mainly reflected in the following aspects: |
8 | 8 |
|
| 9 | +1. Pluggable. |
| 10 | +2. Convention over configuration. |
| 11 | +3. Reasonable priority management. |
| 12 | + |
| 13 | +## Pluggable |
| 14 | + |
| 15 | +VuePress 1.0 has been rewritten extensively, and the most important one is the introduction of the [Plugin API](../plugin/README.md). So what're the benefits of plugins? |
| 16 | + |
| 17 | +### Decoupling |
| 18 | + |
| 19 | +With plugins, we can implement many of the core functions with plugins, and you can see many built-in plugins [here](https://github.com/vuejs/vuepress/tree/master/packages/%40vuepress/core/lib/internal-plugins) that cover many of the core functions of VuePress, which used to blend in all parts of the code base, but now they're clear at a glance. |
| 20 | + |
| 21 | +### Configuration management |
| 22 | + |
| 23 | +In the past, when we came across some less common requirements, we had some doubts: if we wanted to not support it, VuePress usage scenarios were limited; but if we wanted to support it, we had to write it into the core code base and set up a separate configuration API for it. For the maintainers, apart from not conducive to long-term maintenance, this sometimes makes us feel exhausted. We must think of some better solutions. Yes, this is plugin. |
| 24 | + |
| 25 | +### `.vuepress/config.js` is also a plugin |
| 26 | + |
| 27 | +Yes, your configuration file is also a plugin, so you can use the Plugin API directly without having to create a new plugin for it and import it in the configuration. |
| 28 | + |
| 29 | +::: tip |
| 30 | +The options supported by `.vuepress/config.js` are actually based on the plugin options and add some specific options. |
| 31 | +::: |
| 32 | + |
| 33 | + |
| 34 | +### `theme/index.js` is also a plugin |
| 35 | + |
| 36 | +The root configuration file of the theme is also a plugin. |
| 37 | + |
| 38 | +::: tip |
| 39 | +As with `.vuepress/config.js`, the options supported by `theme/config.js` are based on the plugin options and add some specific options. Using a graph to express their relationship: |
| 40 | + |
| 41 | +<svg viewBox="0 0 2806 912" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 42 | + <!-- Generator: Sketch 51 (57462) - http://www.bohemiancoding.com/sketch --> |
| 43 | + <desc>Created with Sketch.</desc> |
| 44 | + <defs></defs> |
| 45 | + <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"> |
| 46 | + <rect id="Rectangle-3" fill-opacity="0" fill="#FFFFFF" x="0" y="0" width="2806" height="912"></rect> |
| 47 | + <circle id="Oval" stroke="#979797" fill="#EC5975" cx="1212.5" cy="455.5" r="355.5"></circle> |
| 48 | + <circle id="Oval" stroke="#979797" fill="#937AC4" cx="1592.5" cy="455.5" r="355.5"></circle> |
| 49 | + <path d="M1402.5,155.000018 C1501.96722,218.018606 1568,329.058303 1568,455.520781 C1568,581.983259 1501.96722,693.022956 1402.5,756.041544 C1303.03279,693.022977 1237,581.983271 1237,455.520781 C1237,329.058291 1303.03279,218.018585 1402.50003,155 Z" id="Combined-Shape" stroke="#FFFFFF" stroke-width="10" fill="#00BD8C"></path> |
| 50 | + <text id=".vuepress/-config.js" font-family="ArialMT, Arial" font-size="60" font-weight="normal" fill="#FFFFFF"> |
| 51 | + <tspan x="901.101562" y="436">.vuepress/</tspan> |
| 52 | + <tspan x="929.446289" y="503">config.js</tspan> |
| 53 | + </text> |
| 54 | + <text id="Plugin-API" font-family="ArialMT, Arial" font-size="72" font-weight="normal" fill="#FFFFFF"> |
| 55 | + <tspan x="1302.42773" y="436">Plugin</tspan> |
| 56 | + <tspan x="1344.47461" y="516">API</tspan> |
| 57 | + </text> |
| 58 | + <text id="theme/-index.js" font-family="ArialMT, Arial" font-size="60" font-weight="normal" fill="#FFFFFF"> |
| 59 | + <tspan x="1662.78613" y="436">theme/</tspan> |
| 60 | + <tspan x="1652.78125" y="503">index.js</tspan> |
| 61 | + </text> |
| 62 | + </g> |
| 63 | +</svg> |
| 64 | +::: |
| 65 | + |
| 66 | +### Apply plugins in a plugin |
| 67 | + |
| 68 | +In VuePress, you have the ability to apply some plugins in a plugin: |
| 69 | + |
| 70 | +```js |
| 71 | +// vuepress-plugin-xxx |
| 72 | +module.exports = { |
| 73 | + plugins: [ |
| 74 | + 'a', 'b', 'c' |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Convention over configuration. |
| 80 | + |
| 81 | +VuePress 1.0 begin to introduce some conventions to reduce the user's excessive configuration pressure, the most intuitive manifestation of this is the conventions for the [document directory structure](../guide/directory-structure.md) and the [theme directory structure](../theme/README.md#directory-structure). |
| 82 | + |
| 83 | +In the future, we may combine community feedback to introduce more agreements. Let's wait and see. |
| 84 | + |
| 85 | +## Reasonable priority management. |
| 86 | + |
| 87 | +Senior users have found that both theme developers and regular users have the ability to customize global `palettes`, `styles`, `templates` and `plugins`, so how do they work together? |
| 88 | + |
| 89 | +### Loading Priority |
| 90 | + |
| 91 | +For `templates/*`, follow the certain loading priority. Taking `templates/ssr.html` as an example: |
| 92 | + |
| 93 | +@flowstart |
| 94 | +cond1=>condition: User's ssr.html |
| 95 | +exists? |
| 96 | +cond2=>condition: Theme's ssr.html |
| 97 | +exists? |
| 98 | +stage1=>operation: Using user's ssr.html |
| 99 | +stage2=>operation: Using theme's ssr.html |
| 100 | +stage3=>operation: Using default ssr.html |
| 101 | + |
| 102 | +cond1(no, right)->cond2(no)->stage3 |
| 103 | +cond1(yes, bottom)->stage1 |
| 104 | +cond2(yes, bottom)->stage2 |
| 105 | +@flowend |
| 106 | + |
| 107 | +::: warning Note |
| 108 | +When customizing `templates/ssr.html`, or `templates/dev.html`, it is best to modify it on the basis of the [default template files](https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/core/lib/app/index.dev.html), otherwise it may cause a build failure. |
| 109 | +::: |
| 110 | + |
| 111 | +### Overriding |
| 112 | + |
| 113 | +For `palette.styl`, `index.styl` and `plugins`, follow the principles of overriding: |
| 114 | + |
| 115 | +#### palette.styl |
| 116 | + |
| 117 | +User's `styles/palette.styl` has a higher priority than the theme's `styles/palette.styl`, so the theme can define its own palette and the user can tweak it. e.g. |
| 118 | + |
| 119 | +```stylus |
| 120 | +// theme/styles/palette.styl |
| 121 | +$accentColor = #0f0 |
| 122 | +``` |
| 123 | + |
| 124 | +```stylus |
| 125 | +// .vuepress/styles/palette.styl |
| 126 | +$accentColor = #f00 |
| 127 | +``` |
| 128 | + |
| 129 | +So the final value of `$accentColor` is `#f00`. |
| 130 | + |
| 131 | +#### index.styl |
| 132 | + |
| 133 | +Both the user's `styles/index.styl` and the theme's `styles/index.styl` are generated into the final `CSS` file, but the user's style is generated later and therefore has higher priority. e.g. |
| 134 | + |
| 135 | +```stylus |
| 136 | +// theme/styles/index.styl |
| 137 | +.content |
| 138 | + font-size 14px |
| 139 | +``` |
| 140 | + |
| 141 | +```stylus |
| 142 | +// .vuepress/styles/index.styl |
| 143 | +.content |
| 144 | + font-size 15px |
| 145 | +``` |
| 146 | + |
| 147 | +The final generated CSS is as follows: |
| 148 | + |
| 149 | +```css |
| 150 | +/* theme/styles/index.styl */ |
| 151 | +.content { |
| 152 | + font-size: 14px; |
| 153 | +} |
| 154 | + |
| 155 | +/* theme/styles/index.styl */ |
| 156 | +.content { |
| 157 | + font-size: 15px; |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +#### plugins |
| 162 | + |
| 163 | +Since all plugins with the same name can be applied ONLY once by default, users can override the default options for plugins in theme. e.g. |
| 164 | + |
| 165 | +```js |
| 166 | +// theme/index.js |
| 167 | +module.exports = { |
| 168 | + plugins: [ |
| 169 | + '@vuepress/i18n-ui', |
| 170 | + { route: '/i18n-page/' } |
| 171 | + ] |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +```js |
| 176 | +// .vuepress/config.js |
| 177 | +module.exports = { |
| 178 | + plugins: [ |
| 179 | + '@vuepress/i18n-ui', |
| 180 | + { route: '/i18n/' } |
| 181 | + ] |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +Then the final route of i18n UI is `/i18n/`. |
| 186 | + |
| 187 | + |
| 188 | +## Others |
| 189 | + |
| 190 | +With the goal of decoupling, we were able to separate VuePress into the following libraries by introducing monorepo: |
| 191 | + |
| 192 | +- [@vuepress/cli](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/cli): Management of command line; |
| 193 | +- [@vuepress/core](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/core):Including the core implementation of `dev`, `build` and `Plugin API`; |
| 194 | +- [@vuepress/theme-default](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/theme-default):The default theme you see now. |
| 195 | + |
| 196 | +Of course, for most users, you don't need to care about the three libraries. Package [vuepress](https://www.npmjs.com/search?Q=vuepress) has already assembled the three packages together, so you can use VuePress like `0.x`. |
9 | 197 |
|
10 | 198 |
|
0 commit comments