@@ -3,17 +3,15 @@ import path from 'path'
3
3
import { RawSourceMap } from 'source-map'
4
4
import { SFCStyleCompileOptions } from './compileStyle'
5
5
6
- export interface StylePreprocessor {
7
- render (
8
- source : string ,
9
- map : RawSourceMap | undefined ,
10
- options : {
11
- [ key : string ] : any
12
- filename : string
13
- } ,
14
- customRequire : SFCStyleCompileOptions [ 'preprocessCustomRequire' ]
15
- ) : StylePreprocessorResults
16
- }
6
+ export type StylePreprocessor = (
7
+ source : string ,
8
+ map : RawSourceMap | undefined ,
9
+ options : {
10
+ [ key : string ] : any
11
+ filename : string
12
+ } ,
13
+ customRequire : SFCStyleCompileOptions [ 'preprocessCustomRequire' ]
14
+ ) => StylePreprocessorResults
17
15
18
16
export interface StylePreprocessorResults {
19
17
code : string
@@ -23,118 +21,109 @@ export interface StylePreprocessorResults {
23
21
}
24
22
25
23
// .scss/.sass processor
26
- const scss : StylePreprocessor = {
27
- render ( source , map , options , load = require ) {
28
- const nodeSass = load ( 'sass' )
29
- const finalOptions = {
30
- ...options ,
31
- data : source ,
32
- file : options . filename ,
33
- outFile : options . filename ,
34
- sourceMap : ! ! map
35
- }
36
-
37
- try {
38
- const result = nodeSass . renderSync ( finalOptions )
39
- // sass output path is position path
40
- const dependencies = result . stats . includedFiles
41
- if ( map ) {
42
- return {
43
- code : result . css . toString ( ) ,
44
- map : merge ( map , JSON . parse ( result . map . toString ( ) ) ) ,
45
- errors : [ ] ,
46
- dependencies
47
- }
48
- }
49
-
50
- return { code : result . css . toString ( ) , errors : [ ] , dependencies }
51
- } catch ( e ) {
52
- return { code : '' , errors : [ e ] , dependencies : [ ] }
53
- }
54
- }
55
- }
56
-
57
- const sass : StylePreprocessor = {
58
- render ( source , map , options , load ) {
59
- return scss . render (
60
- source ,
61
- map ,
62
- {
63
- ...options ,
64
- indentedSyntax : true
65
- } ,
66
- load
67
- )
24
+ const scss : StylePreprocessor = ( source , map , options , load = require ) => {
25
+ const nodeSass = load ( 'sass' )
26
+ const finalOptions = {
27
+ ...options ,
28
+ data : source ,
29
+ file : options . filename ,
30
+ outFile : options . filename ,
31
+ sourceMap : ! ! map
68
32
}
69
- }
70
33
71
- // .less
72
- const less : StylePreprocessor = {
73
- render ( source , map , options , load = require ) {
74
- const nodeLess = load ( 'less' )
75
-
76
- let result : any
77
- let error : Error | null = null
78
- nodeLess . render (
79
- source ,
80
- { ...options , syncImport : true } ,
81
- ( err : Error | null , output : any ) => {
82
- error = err
83
- result = output
84
- }
85
- )
86
-
87
- if ( error ) return { code : '' , errors : [ error ] , dependencies : [ ] }
88
- // less output path is relative path
89
- const dependencies = getAbsolutePaths (
90
- result . imports ,
91
- path . dirname ( options . filename )
92
- )
34
+ try {
35
+ const result = nodeSass . renderSync ( finalOptions )
36
+ // sass output path is position path
37
+ const dependencies = result . stats . includedFiles
93
38
if ( map ) {
94
39
return {
95
40
code : result . css . toString ( ) ,
96
- map : merge ( map , result . map ) ,
41
+ map : merge ( map , JSON . parse ( result . map . toString ( ) ) ) ,
97
42
errors : [ ] ,
98
- dependencies : dependencies
43
+ dependencies
99
44
}
100
45
}
101
46
47
+ return { code : result . css . toString ( ) , errors : [ ] , dependencies }
48
+ } catch ( e ) {
49
+ return { code : '' , errors : [ e ] , dependencies : [ ] }
50
+ }
51
+ }
52
+
53
+ const sass : StylePreprocessor = ( source , map , options , load ) =>
54
+ scss (
55
+ source ,
56
+ map ,
57
+ {
58
+ ...options ,
59
+ indentedSyntax : true
60
+ } ,
61
+ load
62
+ )
63
+
64
+ // .less
65
+ const less : StylePreprocessor = ( source , map , options , load = require ) => {
66
+ const nodeLess = load ( 'less' )
67
+
68
+ let result : any
69
+ let error : Error | null = null
70
+ nodeLess . render (
71
+ source ,
72
+ { ...options , syncImport : true } ,
73
+ ( err : Error | null , output : any ) => {
74
+ error = err
75
+ result = output
76
+ }
77
+ )
78
+
79
+ if ( error ) return { code : '' , errors : [ error ] , dependencies : [ ] }
80
+ // less output path is relative path
81
+ const dependencies = getAbsolutePaths (
82
+ result . imports ,
83
+ path . dirname ( options . filename )
84
+ )
85
+ if ( map ) {
102
86
return {
103
87
code : result . css . toString ( ) ,
88
+ map : merge ( map , result . map ) ,
104
89
errors : [ ] ,
105
90
dependencies : dependencies
106
91
}
107
92
}
93
+
94
+ return {
95
+ code : result . css . toString ( ) ,
96
+ errors : [ ] ,
97
+ dependencies : dependencies
98
+ }
108
99
}
109
100
110
101
// .styl
111
- const styl : StylePreprocessor = {
112
- render ( source , map , options , load = require ) {
113
- const nodeStylus = load ( 'stylus' )
114
- try {
115
- const ref = nodeStylus ( source )
116
- Object . keys ( options ) . forEach ( key => ref . set ( key , options [ key ] ) )
117
- if ( map ) ref . set ( 'sourcemap' , { inline : false , comment : false } )
118
-
119
- const result = ref . render ( )
120
- // stylus output path is relative path
121
- const dependencies = getAbsolutePaths (
122
- ref . deps ( ) ,
123
- path . dirname ( options . fileName )
124
- )
125
- if ( map ) {
126
- return {
127
- code : result ,
128
- map : merge ( map , ref . sourcemap ) ,
129
- errors : [ ] ,
130
- dependencies
131
- }
102
+ const styl : StylePreprocessor = ( source , map , options , load = require ) => {
103
+ const nodeStylus = load ( 'stylus' )
104
+ try {
105
+ const ref = nodeStylus ( source )
106
+ Object . keys ( options ) . forEach ( key => ref . set ( key , options [ key ] ) )
107
+ if ( map ) ref . set ( 'sourcemap' , { inline : false , comment : false } )
108
+
109
+ const result = ref . render ( )
110
+ // stylus output path is relative path
111
+ const dependencies = getAbsolutePaths (
112
+ ref . deps ( ) ,
113
+ path . dirname ( options . fileName )
114
+ )
115
+ if ( map ) {
116
+ return {
117
+ code : result ,
118
+ map : merge ( map , ref . sourcemap ) ,
119
+ errors : [ ] ,
120
+ dependencies
132
121
}
133
-
134
- return { code : result , errors : [ ] , dependencies }
135
- } catch ( e ) {
136
- return { code : '' , errors : [ e ] , dependencies : [ ] }
137
122
}
123
+
124
+ return { code : result , errors : [ ] , dependencies }
125
+ } catch ( e ) {
126
+ return { code : '' , errors : [ e ] , dependencies : [ ] }
138
127
}
139
128
}
140
129
0 commit comments