Skip to content

Commit 63555c0

Browse files
committed
feat($core): support 'palette' API, deprecate override.styl
1 parent b0e3209 commit 63555c0

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

Diff for: packages/@vuepress/core/lib/app/style/config.styl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// colors
2-
$accentColor = #3eaf73
2+
$accentColor = #3eaf7c
33
$textColor = #2c3e50
44
$borderColor = #eaecef
55
$codeBgColor = #282c34
@@ -19,4 +19,4 @@ $MQMobileNarrow = 419px
1919
$lineNumbersWrapperWidth = 3.5rem
2020
$codeLang = js ts html md vue css sass scss less stylus go java c sh yaml py
2121

22-
@import '~@temp/override.styl'
22+
@import '~@temp/palette.styl'

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

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
const path = require('path')
2-
const { fs, logger, chalk } = require('@vuepress/shared-utils')
2+
const {
3+
fs, logger, chalk,
4+
datatypes: {
5+
isPlainObject,
6+
assertTypes,
7+
isString
8+
}
9+
} = require('@vuepress/shared-utils')
310

411
module.exports = (options, context) => ({
512
name: '@vuepress/internal-override-css',
613

714
async ready () {
815
const { sourceDir, writeTemp } = context
916

10-
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl').replace(/[\\]+/g, '/')
17+
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl')
1118
const hasUserOverride = fs.existsSync(overridePath)
12-
await writeTemp('override.styl', hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
1319

20+
if (hasUserOverride) {
21+
logger.tip(`${chalk.magenta('override.styl')} has been deprecated from v1.0.0, using ${chalk.cyan('config.palette')} instead.\n`)
22+
}
23+
24+
// palette API.
25+
const themePalette = context.themePalette
26+
const { palette: userPalette } = context.siteConfig
27+
const themePaletteContent = resolvePaletteContent(themePalette)
28+
const userPaletteContent = resolvePaletteContent(userPalette)
29+
// user's palette can override theme's palette.
30+
const paletteContent = themePaletteContent + userPaletteContent
31+
await writeTemp('palette.styl', paletteContent)
32+
33+
// style.styl API.
1434
const stylePath = path.resolve(sourceDir, '.vuepress/style.styl').replace(/[\\]+/g, '/')
1535
const hasUserStyle = fs.existsSync(stylePath)
1636
await writeTemp('style.styl', hasUserStyle ? `@import(${JSON.stringify(stylePath)})` : ``)
@@ -24,3 +44,26 @@ module.exports = (options, context) => ({
2444
}
2545
}
2646
})
47+
48+
function resolvePaletteContent (palette) {
49+
const { valid, warnMsg } = assertTypes(palette, [String, Object])
50+
if (!valid) {
51+
if (palette !== undefined) {
52+
logger.warn(
53+
`[vuepress] Invalid value for "palette": ${warnMsg}`
54+
)
55+
}
56+
return ''
57+
}
58+
59+
if (isString(palette)) {
60+
if (fs.existsSync(palette)) {
61+
return `@import(${JSON.stringify(palette)})\n`
62+
}
63+
return ''
64+
} else if (isPlainObject(palette)) {
65+
return Object.keys(palette).map(variableName => {
66+
return `${variableName} = ${palette[variableName]}`
67+
}).join('\n') + '\n'
68+
}
69+
}

Diff for: packages/@vuepress/core/lib/prepare/loadTheme.js

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,50 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
17
const path = require('path')
28
const fs = require('fs')
39
const {
4-
logger, chalk,
10+
shortcutPackageResolver: { resolveTheme },
511
datatypes: { isString },
6-
shortcutPackageResolver: { resolveTheme }
12+
logger, chalk
713
} = require('@vuepress/shared-utils')
814

15+
/**
16+
* Resolve theme.
17+
*
18+
* Resolving Priority:
19+
*
20+
* 1. If the theme was a absolute path and that path exists, respect it
21+
* as the theme directory.
22+
* 2. If 'theme' directory located at vuepressDir exists, respect it as
23+
* the theme directory.
24+
* 3. If 'theme' was a shortcut string, resolve it from deps.
25+
*
26+
* @param {string} theme
27+
* @param {string} sourceDir
28+
* @param {string} vuepressDir
29+
* @returns {Promise}
30+
*/
31+
932
module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
10-
// resolve theme
1133
const localThemePath = path.resolve(vuepressDir, 'theme')
1234
const useLocalTheme =
13-
fs.existsSync(localThemePath) && (fs.readdirSync(localThemePath)).length > 0
35+
!fs.existsSync(theme) &&
36+
fs.existsSync(localThemePath) &&
37+
(fs.readdirSync(localThemePath)).length > 0
1438

1539
let themePath = null // Mandatory
1640
let themeIndexFile = null // Optional
17-
let themePlugins = [] // Optional
1841
let themeName
1942
let themeShortcut
2043

2144
if (useLocalTheme) {
22-
// use local custom theme
2345
themePath = localThemePath
24-
logger.tip(`\nApply theme located at ${themePath}...`)
46+
logger.tip(`\nApply theme located at ${chalk.gray(themePath)}...`)
2547
} else if (isString(theme)) {
26-
// use external theme
2748
const { module: modulePath, name, shortcut } = resolveTheme(theme, sourceDir)
2849
if (modulePath.endsWith('.js') || modulePath.endsWith('.vue')) {
2950
themePath = path.parse(modulePath).dir
@@ -44,8 +65,13 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
4465
}
4566

4667
// handle theme api
47-
const { plugins, layoutDir = useLocalTheme ? '.' : 'layouts' } = themeIndexFile
48-
themePlugins = plugins
68+
const {
69+
plugins: themePlugins,
70+
palette: themePalette,
71+
layoutDir = useLocalTheme
72+
? '.'
73+
: 'layouts'
74+
} = themeIndexFile
4975

5076
const layoutDirPath = path.resolve(themePath, layoutDir)
5177

@@ -93,6 +119,7 @@ module.exports = async function loadTheme (theme, sourceDir, vuepressDir) {
93119
layoutComponentMap,
94120
themeIndexFile,
95121
themePlugins,
122+
themePalette,
96123
themeName,
97124
themeShortcut
98125
}

0 commit comments

Comments
 (0)