Skip to content

Commit

Permalink
feat(css): allow async additionalData function for css pre-processors
Browse files Browse the repository at this point in the history
close #2002
  • Loading branch information
yyx990803 committed Feb 15, 2021
1 parent 9925d86 commit 20f609d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,16 @@ AtImportHoistPlugin.postcss = true

type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'

type PreprocessorAdditionalData =
| string
| ((source: string, filename: string) => string | Promise<string>)

type StylePreprocessor = (
source: string,
root: string,
options: {
[key: string]: any
additionalData?: string | ((source: string, filename: string) => string)
additionalData?: PreprocessorAdditionalData
filename: string
},
resolvers: CSSResolvers
Expand Down Expand Up @@ -826,7 +830,7 @@ const scss: StylePreprocessor = async (source, root, options, resolvers) => {
const render = loadPreprocessor('sass', root).render as typeof Sass.render
const finalOptions: Sass.Options = {
...options,
data: getSource(source, options.filename, options.additionalData),
data: await getSource(source, options.filename, options.additionalData),
file: options.filename,
outFile: options.filename,
importer(url, importer, done) {
Expand Down Expand Up @@ -916,7 +920,7 @@ const less: StylePreprocessor = async (source, root, options, resolvers) => {
options.filename,
resolvers
)
source = getSource(source, options.filename, options.additionalData)
source = await getSource(source, options.filename, options.additionalData)

let result: Less.RenderOutput | undefined
try {
Expand Down Expand Up @@ -1024,8 +1028,8 @@ const styl: StylePreprocessor = (source, root, options) => {
function getSource(
source: string,
filename: string,
additionalData?: string | ((source: string, filename: string) => string)
): string {
additionalData?: PreprocessorAdditionalData
): string | Promise<string> {
if (!additionalData) return source
if (typeof additionalData === 'function') {
return additionalData(source, filename)
Expand Down

0 comments on commit 20f609d

Please sign in to comment.