1
1
import merge from 'merge-source-map'
2
2
import { RawSourceMap } from 'source-map'
3
3
import { SFCStyleCompileOptions } from './compileStyle'
4
+ import { isFunction } from '@vue/shared'
4
5
5
6
export type StylePreprocessor = (
6
7
source : string ,
7
8
map : RawSourceMap | undefined ,
8
9
options : {
9
10
[ key : string ] : any
11
+ additionalData ?: string | ( ( source : string , filename : string ) => string )
10
12
filename : string
11
13
} ,
12
14
customRequire : SFCStyleCompileOptions [ 'preprocessCustomRequire' ]
@@ -24,7 +26,7 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
24
26
const nodeSass = load ( 'sass' )
25
27
const finalOptions = {
26
28
...options ,
27
- data : ( options . additionalData || '' ) + source ,
29
+ data : getSource ( source , options . filename , options . additionalData ) ,
28
30
file : options . filename ,
29
31
outFile : options . filename ,
30
32
sourceMap : ! ! map
@@ -66,7 +68,7 @@ const less: StylePreprocessor = (source, map, options, load = require) => {
66
68
let result : any
67
69
let error : Error | null = null
68
70
nodeLess . render (
69
- source ,
71
+ getSource ( source , options . filename , options . additionalData ) ,
70
72
{ ...options , syncImport : true } ,
71
73
( err : Error | null , output : any ) => {
72
74
error = err
@@ -117,6 +119,18 @@ const styl: StylePreprocessor = (source, map, options, load = require) => {
117
119
}
118
120
}
119
121
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
+
120
134
export type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'
121
135
122
136
export const processors : Record < PreprocessLang , StylePreprocessor > = {
0 commit comments