11import merge from 'merge-source-map'
22import { RawSourceMap } from 'source-map'
33import { SFCStyleCompileOptions } from './compileStyle'
4+ import { isFunction } from '@vue/shared'
45
56export type StylePreprocessor = (
67 source : string ,
78 map : RawSourceMap | undefined ,
89 options : {
910 [ key : string ] : any
11+ additionalData ?: string | ( ( source : string , filename : string ) => string )
1012 filename : string
1113 } ,
1214 customRequire : SFCStyleCompileOptions [ 'preprocessCustomRequire' ]
@@ -24,7 +26,7 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
2426 const nodeSass = load ( 'sass' )
2527 const finalOptions = {
2628 ...options ,
27- data : ( options . additionalData || '' ) + source ,
29+ data : getSource ( source , options . filename , options . additionalData ) ,
2830 file : options . filename ,
2931 outFile : options . filename ,
3032 sourceMap : ! ! map
@@ -66,7 +68,7 @@ const less: StylePreprocessor = (source, map, options, load = require) => {
6668 let result : any
6769 let error : Error | null = null
6870 nodeLess . render (
69- source ,
71+ getSource ( source , options . filename , options . additionalData ) ,
7072 { ...options , syncImport : true } ,
7173 ( err : Error | null , output : any ) => {
7274 error = err
@@ -117,6 +119,18 @@ const styl: StylePreprocessor = (source, map, options, load = require) => {
117119 }
118120}
119121
122+ function getSource (
123+ source : string ,
124+ filename : string ,
125+ additionalData ?: string | ( ( source : string , filename : string ) => string )
126+ ) {
127+ if ( ! additionalData ) return source
128+ if ( isFunction ( additionalData ) ) {
129+ return additionalData ( source , filename )
130+ }
131+ return additionalData + source
132+ }
133+
120134export type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'
121135
122136export const processors : Record < PreprocessLang , StylePreprocessor > = {
0 commit comments