Skip to content

Commit e2253c2

Browse files
committed
refactor($core): clarify palette.styl and style.styl
1 parent 6222170 commit e2253c2

File tree

8 files changed

+125
-97
lines changed

8 files changed

+125
-97
lines changed

Diff for: β€Žpackages/@vuepress/core/lib/app/app.js

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import globalUIComponents from '@internal/global-ui'
99
import ClientComputedMixin from '@transform/ClientComputedMixin'
1010
import Store from './Store'
1111

12-
// generated from user config
13-
import('@temp/style.styl')
14-
1512
// built-in components
1613
import Content from './components/Content'
1714
import OutboundLink from './components/OutboundLink.vue'

Diff for: β€Žpackages/@vuepress/core/lib/internal-plugins/overrideCSS.js

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const {
2+
fs, logger,
3+
datatypes: {
4+
isPlainObject,
5+
assertTypes,
6+
isString
7+
}
8+
} = require('@vuepress/shared-utils')
9+
10+
module.exports = (options, ctx) => ({
11+
name: '@vuepress/internal-palette',
12+
13+
async ready () {
14+
const { writeTemp } = ctx
15+
16+
const themePalette = ctx.themePalette
17+
const { palette: userPalette } = ctx.siteConfig
18+
const themePaletteContent = resolvePaletteContent(themePalette)
19+
const userPaletteContent = resolvePaletteContent(userPalette)
20+
21+
// user's palette can override theme's palette.
22+
const paletteContent = themePaletteContent + userPaletteContent
23+
console.log(paletteContent)
24+
await writeTemp('palette.styl', paletteContent)
25+
}
26+
})
27+
28+
/**
29+
* resolve palette content
30+
* @param {string|object} palette
31+
* @returns {string}
32+
*/
33+
34+
function resolvePaletteContent (palette) {
35+
const { valid, warnMsg } = assertTypes(palette, [String, Object])
36+
if (!valid) {
37+
if (palette !== undefined) {
38+
logger.warn(
39+
`[vuepress] Invalid value for "palette": ${warnMsg}`
40+
)
41+
}
42+
return ''
43+
}
44+
45+
if (isString(palette)) {
46+
if (fs.existsSync(palette)) {
47+
return `@import(${JSON.stringify(palette)})\n`
48+
}
49+
return ''
50+
} else if (isPlainObject(palette)) {
51+
return Object.keys(palette).map(variableName => {
52+
return `${variableName} = ${palette[variableName]}`
53+
}).join('\n') + '\n'
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// generated from user config
2+
import('@temp/style.styl')
3+
4+
window.__VUEPRESS_STYLE_STYLE_LOADED__ = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const path = require('path')
2+
const { fs, logger, chalk } = require('@vuepress/shared-utils')
3+
4+
module.exports = (options, context) => ({
5+
name: '@vuepress/internal-style',
6+
7+
enhanceAppFiles: [path.resolve(__dirname, 'client.js')],
8+
9+
async ready () {
10+
const { sourceDir, writeTemp } = context
11+
12+
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl')
13+
const hasUserOverride = fs.existsSync(overridePath)
14+
15+
if (hasUserOverride) {
16+
logger.tip(`${chalk.magenta('override.styl')} has been deprecated from v1.0.0, using ${chalk.cyan('config.palette')} instead.\n`)
17+
}
18+
19+
// style.styl API.
20+
const stylePath = path.resolve(sourceDir, '.vuepress/style.styl').replace(/[\\]+/g, '/')
21+
const hasUserStyle = fs.existsSync(stylePath)
22+
await writeTemp('style.styl', hasUserStyle ? `@import(${JSON.stringify(stylePath)})` : ``)
23+
24+
// Temporary tip, will be removed at next release.
25+
if (hasUserOverride && !hasUserStyle) {
26+
logger.tip(
27+
`${chalk.magenta('override.styl')} has been split into 2 APIs, we recommend you upgrade to continue.\n` +
28+
` See: ${chalk.magenta('https://vuepress.vuejs.org/default-theme-config/#simple-css-override')}`
29+
)
30+
}
31+
}
32+
})
33+

Diff for: β€Žpackages/@vuepress/core/lib/prepare/AppContext.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ module.exports = class AppContext {
111111
.use(require('../internal-plugins/routes'))
112112
.use(require('../internal-plugins/rootMixins'))
113113
.use(require('../internal-plugins/enhanceApp'))
114-
.use(require('../internal-plugins/overrideCSS'))
114+
.use(require('../internal-plugins/palette'))
115+
.use(require('../internal-plugins/style'))
115116
.use(require('../internal-plugins/layoutComponents'))
116117
.use(require('../internal-plugins/pageComponents'))
117118
.use(require('../internal-plugins/transformModule'))

Diff for: β€Žpackages/docs/docs/default-theme-config/README.md

+13-24
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ editLink: false
436436
---
437437
```
438438

439-
## Simple CSS Override
439+
## Simple Color Variables Override
440440

441-
If you wish to apply simple overrides to the styling of the default theme, you can create an `.vuepress/override.styl` file. This is a [Stylus](http://stylus-lang.com/) file but you can use normal CSS syntax as well.
441+
If you wish to apply simple color overrides to the styling of the default theme, you can create an `.vuepress/palette.styl` file. This is a [Stylus](http://stylus-lang.com/) file but you can use normal CSS syntax as well.
442442

443443
There are a few color variables you can tweak:
444444

@@ -450,34 +450,23 @@ $borderColor = #eaecef
450450
$codeBgColor = #282c34
451451
```
452452

453-
### Existing issues <Badge text="< 0.12.0" type='error'/>
454-
455-
In order to override the default variables mentioned above, `override.styl` will be imported at the end of the `config.styl` in default theme, and this file will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times. See [#637](https://github.com/vuejs/vuepress/issues/637).
456-
457-
### Migrate your styles to `style.styl` <Badge text="0.12.0+"/>
458-
459-
In fact, The `stylus constants override` should be completed before all Stylus files are compiled; and the `user's additional CSS styles` should be generated at the end of the final style file. Therefore, these two duties should not be completed by the same stylus file.
453+
::: danger Note
454+
You should ONLY write color variables in this file. since `palette.styl` will be imported at the end of the root stylus config file, as a config, it will be used by multiple files, so once you wrote styles here, your style would be duplicated by multiple times.
455+
:::
460456

461-
Start from `0.12.0`, we split `override.styl` into two APIs: `override.styl` and `style.styl`. If you wrote styles at `override.styl` in the past, e.g.
457+
## Simple CSS Override
462458

463-
``` stylus
464-
// .vuepress/override.styl
465-
$textColor = red // stylus constants override.
459+
VuePress provides a convenient way to add extra styles. you can create an `.vuepress/style.styl` file for that:
466460

467-
#my-style {} // your extra styles.
461+
```stylus
462+
content {
463+
font-size 30px
464+
}
468465
```
469466

470-
You'll need to separate the style part to `style.styl`:
471-
472-
``` stylus
473-
// .vuepress/override.styl, SHOULD ONLY contain "stylus constants override".
474-
$textColor = red
475-
```
467+
**Also see:**
476468

477-
``` stylus
478-
// .vuepress/style.styl, your extra styles.
479-
#my-style {}
480-
```
469+
- [FAQ > Why can't `palette.styl` and `style.styl` merge into one API?](../faq/README.md#why-can-t-palette-styl-and-style-styl-merge-into-one-api)
481470

482471
## Custom Page Class
483472

Diff for: β€Žpackages/docs/docs/faq/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# FAQ
2+
3+
## Why can't `palette.styl` and `style.styl` merge into one API?
4+
5+
The `palette.styl` is responsible for global color settings. During compilation, theme color constants should be resolved by the preprocessor first and then applied to the global. But for `style.styl`. its job is to override the $default style. According to the priority principle of css, the later style has a higher priority, so it should be generated at the end of the CSS file. A simple diagram is used to describe the compiler's compilation order as follows:
6+
7+
```
8+
$palette.styl
9+
10+
↓ ↓ ↓
11+
12+
$default styles
13+
14+
↓ ↓ ↓
15+
16+
$style.styl
17+
```
18+

0 commit comments

Comments
Β (0)