refactor(compiler-sfc): simplify style preprocessors
This commit is contained in:
parent
9cb29eea3a
commit
d5055cd8dd
@ -213,7 +213,7 @@ function preprocess(
|
||||
)
|
||||
}
|
||||
|
||||
return preprocessor.render(
|
||||
return preprocessor(
|
||||
options.source,
|
||||
options.map,
|
||||
{
|
||||
|
@ -3,8 +3,7 @@ import path from 'path'
|
||||
import { RawSourceMap } from 'source-map'
|
||||
import { SFCStyleCompileOptions } from './compileStyle'
|
||||
|
||||
export interface StylePreprocessor {
|
||||
render(
|
||||
export type StylePreprocessor = (
|
||||
source: string,
|
||||
map: RawSourceMap | undefined,
|
||||
options: {
|
||||
@ -12,8 +11,7 @@ export interface StylePreprocessor {
|
||||
filename: string
|
||||
},
|
||||
customRequire: SFCStyleCompileOptions['preprocessCustomRequire']
|
||||
): StylePreprocessorResults
|
||||
}
|
||||
) => StylePreprocessorResults
|
||||
|
||||
export interface StylePreprocessorResults {
|
||||
code: string
|
||||
@ -23,8 +21,7 @@ export interface StylePreprocessorResults {
|
||||
}
|
||||
|
||||
// .scss/.sass processor
|
||||
const scss: StylePreprocessor = {
|
||||
render(source, map, options, load = require) {
|
||||
const scss: StylePreprocessor = (source, map, options, load = require) => {
|
||||
const nodeSass = load('sass')
|
||||
const finalOptions = {
|
||||
...options,
|
||||
@ -52,11 +49,9 @@ const scss: StylePreprocessor = {
|
||||
return { code: '', errors: [e], dependencies: [] }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sass: StylePreprocessor = {
|
||||
render(source, map, options, load) {
|
||||
return scss.render(
|
||||
const sass: StylePreprocessor = (source, map, options, load) =>
|
||||
scss(
|
||||
source,
|
||||
map,
|
||||
{
|
||||
@ -65,12 +60,9 @@ const sass: StylePreprocessor = {
|
||||
},
|
||||
load
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// .less
|
||||
const less: StylePreprocessor = {
|
||||
render(source, map, options, load = require) {
|
||||
const less: StylePreprocessor = (source, map, options, load = require) => {
|
||||
const nodeLess = load('less')
|
||||
|
||||
let result: any
|
||||
@ -105,11 +97,9 @@ const less: StylePreprocessor = {
|
||||
dependencies: dependencies
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// .styl
|
||||
const styl: StylePreprocessor = {
|
||||
render(source, map, options, load = require) {
|
||||
const styl: StylePreprocessor = (source, map, options, load = require) => {
|
||||
const nodeStylus = load('stylus')
|
||||
try {
|
||||
const ref = nodeStylus(source)
|
||||
@ -136,7 +126,6 @@ const styl: StylePreprocessor = {
|
||||
return { code: '', errors: [e], dependencies: [] }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user