-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor($core): clarify palette.styl and style.styl
- Loading branch information
Showing
8 changed files
with
125 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
packages/@vuepress/core/lib/internal-plugins/overrideCSS.js
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
packages/@vuepress/core/lib/internal-plugins/palette/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const { | ||
fs, logger, | ||
datatypes: { | ||
isPlainObject, | ||
assertTypes, | ||
isString | ||
} | ||
} = require('@vuepress/shared-utils') | ||
|
||
module.exports = (options, ctx) => ({ | ||
name: '@vuepress/internal-palette', | ||
|
||
async ready () { | ||
const { writeTemp } = ctx | ||
|
||
const themePalette = ctx.themePalette | ||
const { palette: userPalette } = ctx.siteConfig | ||
const themePaletteContent = resolvePaletteContent(themePalette) | ||
const userPaletteContent = resolvePaletteContent(userPalette) | ||
|
||
// user's palette can override theme's palette. | ||
const paletteContent = themePaletteContent + userPaletteContent | ||
console.log(paletteContent) | ||
await writeTemp('palette.styl', paletteContent) | ||
} | ||
}) | ||
|
||
/** | ||
* resolve palette content | ||
* @param {string|object} palette | ||
* @returns {string} | ||
*/ | ||
|
||
function resolvePaletteContent (palette) { | ||
const { valid, warnMsg } = assertTypes(palette, [String, Object]) | ||
if (!valid) { | ||
if (palette !== undefined) { | ||
logger.warn( | ||
`[vuepress] Invalid value for "palette": ${warnMsg}` | ||
) | ||
} | ||
return '' | ||
} | ||
|
||
if (isString(palette)) { | ||
if (fs.existsSync(palette)) { | ||
return `@import(${JSON.stringify(palette)})\n` | ||
} | ||
return '' | ||
} else if (isPlainObject(palette)) { | ||
return Object.keys(palette).map(variableName => { | ||
return `${variableName} = ${palette[variableName]}` | ||
}).join('\n') + '\n' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// generated from user config | ||
import('@temp/style.styl') | ||
|
||
window.__VUEPRESS_STYLE_STYLE_LOADED__ = true |
33 changes: 33 additions & 0 deletions
33
packages/@vuepress/core/lib/internal-plugins/style/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const path = require('path') | ||
const { fs, logger, chalk } = require('@vuepress/shared-utils') | ||
|
||
module.exports = (options, context) => ({ | ||
name: '@vuepress/internal-style', | ||
|
||
enhanceAppFiles: [path.resolve(__dirname, 'client.js')], | ||
|
||
async ready () { | ||
const { sourceDir, writeTemp } = context | ||
|
||
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl') | ||
const hasUserOverride = fs.existsSync(overridePath) | ||
|
||
if (hasUserOverride) { | ||
logger.tip(`${chalk.magenta('override.styl')} has been deprecated from v1.0.0, using ${chalk.cyan('config.palette')} instead.\n`) | ||
} | ||
|
||
// style.styl API. | ||
const stylePath = path.resolve(sourceDir, '.vuepress/style.styl').replace(/[\\]+/g, '/') | ||
const hasUserStyle = fs.existsSync(stylePath) | ||
await writeTemp('style.styl', hasUserStyle ? `@import(${JSON.stringify(stylePath)})` : ``) | ||
|
||
// Temporary tip, will be removed at next release. | ||
if (hasUserOverride && !hasUserStyle) { | ||
logger.tip( | ||
`${chalk.magenta('override.styl')} has been split into 2 APIs, we recommend you upgrade to continue.\n` + | ||
` See: ${chalk.magenta('https://vuepress.vuejs.org/default-theme-config/#simple-css-override')}` | ||
) | ||
} | ||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# FAQ | ||
|
||
## Why can't `palette.styl` and `style.styl` merge into one API? | ||
|
||
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: | ||
|
||
``` | ||
$palette.styl | ||
β β β | ||
$default styles | ||
β β β | ||
$style.styl | ||
``` | ||
|