2019-11-06 21:58:15 -05:00
|
|
|
import merge from 'merge-source-map'
|
|
|
|
|
|
|
|
export interface StylePreprocessor {
|
|
|
|
render(source: string, map?: object, options?: any): StylePreprocessorResults
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface StylePreprocessorResults {
|
|
|
|
code: string
|
|
|
|
map?: object
|
2019-12-11 13:33:45 -05:00
|
|
|
errors: Error[]
|
2019-11-06 21:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// .scss/.sass processor
|
|
|
|
const scss: StylePreprocessor = {
|
|
|
|
render(source, map, options) {
|
|
|
|
const nodeSass = require('sass')
|
2019-11-28 12:49:39 -03:00
|
|
|
const finalOptions = {
|
|
|
|
...options,
|
2019-11-06 21:58:15 -05:00
|
|
|
data: source,
|
|
|
|
file: options.filename,
|
|
|
|
outFile: options.filename,
|
|
|
|
sourceMap: !!map
|
2019-11-28 12:49:39 -03:00
|
|
|
}
|
2019-11-06 21:58:15 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
const result = nodeSass.renderSync(finalOptions)
|
|
|
|
|
|
|
|
if (map) {
|
|
|
|
return {
|
|
|
|
code: result.css.toString(),
|
|
|
|
map: merge(map, JSON.parse(result.map.toString())),
|
|
|
|
errors: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { code: result.css.toString(), errors: [] }
|
|
|
|
} catch (e) {
|
|
|
|
return { code: '', errors: [e] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const sass: StylePreprocessor = {
|
|
|
|
render(source, map, options) {
|
2019-11-28 12:49:39 -03:00
|
|
|
return scss.render(source, map, {
|
|
|
|
...options,
|
|
|
|
indentedSyntax: true
|
|
|
|
})
|
2019-11-06 21:58:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// .less
|
|
|
|
const less: StylePreprocessor = {
|
|
|
|
render(source, map, options) {
|
|
|
|
const nodeLess = require('less')
|
|
|
|
|
|
|
|
let result: any
|
|
|
|
let error: Error | null = null
|
|
|
|
nodeLess.render(
|
|
|
|
source,
|
2019-11-28 12:49:39 -03:00
|
|
|
{ ...options, syncImport: true },
|
2019-11-06 21:58:15 -05:00
|
|
|
(err: Error | null, output: any) => {
|
|
|
|
error = err
|
|
|
|
result = output
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if (error) return { code: '', errors: [error] }
|
|
|
|
|
|
|
|
if (map) {
|
|
|
|
return {
|
|
|
|
code: result.css.toString(),
|
|
|
|
map: merge(map, result.map),
|
|
|
|
errors: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { code: result.css.toString(), errors: [] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// .styl
|
|
|
|
const styl: StylePreprocessor = {
|
|
|
|
render(source, map, options) {
|
|
|
|
const nodeStylus = require('stylus')
|
|
|
|
try {
|
|
|
|
const ref = nodeStylus(source)
|
|
|
|
Object.keys(options).forEach(key => ref.set(key, options[key]))
|
|
|
|
if (map) ref.set('sourcemap', { inline: false, comment: false })
|
|
|
|
|
|
|
|
const result = ref.render()
|
|
|
|
if (map) {
|
|
|
|
return {
|
|
|
|
code: result,
|
|
|
|
map: merge(map, ref.sourcemap),
|
|
|
|
errors: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { code: result, errors: [] }
|
|
|
|
} catch (e) {
|
|
|
|
return { code: '', errors: [e] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:19:39 +08:00
|
|
|
export type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'
|
|
|
|
|
|
|
|
export const processors: Record<PreprocessLang, StylePreprocessor> = {
|
2019-11-06 21:58:15 -05:00
|
|
|
less,
|
|
|
|
sass,
|
|
|
|
scss,
|
|
|
|
styl,
|
|
|
|
stylus: styl
|
|
|
|
}
|