1
- 'use strict'
2
-
3
1
const path = require ( 'path' )
4
2
5
- const loaderUtils = require ( 'loader-utils' )
6
-
7
- const parseOptions = require ( './options' )
3
+ const { getOptions } = require ( 'loader-utils' )
8
4
const validateOptions = require ( 'schema-utils' )
9
5
10
6
const postcss = require ( 'postcss' )
@@ -42,7 +38,7 @@ const parseOptions = require('./options.js')
42
38
* @return {cb } cb Result
43
39
*/
44
40
module . exports = function loader ( css , map , meta ) {
45
- const options = Object . assign ( { } , loaderUtils . getOptions ( this ) )
41
+ const options = Object . assign ( { } , getOptions ( this ) )
46
42
47
43
validateOptions ( require ( './options.json' ) , options , 'PostCSS Loader' )
48
44
@@ -96,16 +92,25 @@ module.exports = function loader (css, map, meta) {
96
92
97
93
return postcssrc ( rc . ctx , rc . path )
98
94
} ) . then ( ( config ) => {
99
- if ( ! config ) config = { }
95
+ if ( ! config ) {
96
+ config = { }
97
+ }
100
98
101
- if ( config . file ) this . addDependency ( config . file )
99
+ if ( config . file ) {
100
+ this . addDependency ( config . file )
101
+ }
102
102
103
103
// Disable override `to` option from `postcss.config.js`
104
- if ( config . options . to ) delete config . options . to
104
+ if ( config . options . to ) {
105
+ delete config . options . to
106
+ }
105
107
// Disable override `from` option from `postcss.config.js`
106
- if ( config . options . from ) delete config . options . from
108
+ if ( config . options . from ) {
109
+ delete config . options . from
110
+ }
107
111
108
112
let plugins = config . plugins || [ ]
113
+
109
114
let options = Object . assign ( {
110
115
from : file ,
111
116
map : sourceMap
@@ -139,29 +144,39 @@ module.exports = function loader (css, map, meta) {
139
144
css = this . exec ( css , this . resource )
140
145
}
141
146
142
- if ( sourceMap && typeof map === 'string' ) map = JSON . parse ( map )
143
- if ( sourceMap && map ) options . map . prev = map
147
+ if ( sourceMap && typeof map === 'string' ) {
148
+ map = JSON . parse ( map )
149
+ }
150
+
151
+ if ( sourceMap && map ) {
152
+ options . map . prev = map
153
+ }
144
154
145
155
return postcss ( plugins )
146
156
. process ( css , options )
147
157
. then ( ( result ) => {
158
+ let { css, map, root, processor, messages } = result
159
+
148
160
result . warnings ( ) . forEach ( ( warning ) => {
149
161
this . emitWarning ( new Warning ( warning ) )
150
162
} )
151
163
152
- result . messages . forEach ( ( msg ) => {
153
- if ( msg . type === 'dependency' ) this . addDependency ( msg . file )
164
+ messages . forEach ( ( msg ) => {
165
+ if ( msg . type === 'dependency' ) {
166
+ this . addDependency ( msg . file )
167
+ }
154
168
} )
155
169
156
- css = result . css
157
- map = result . map ? result . map . toJSON ( ) : null
170
+ map = map ? map . toJSON ( ) : null
158
171
159
172
if ( map ) {
160
173
map . file = path . resolve ( map . file )
161
174
map . sources = map . sources . map ( ( src ) => path . resolve ( src ) )
162
175
}
163
176
164
- if ( ! meta ) meta = { }
177
+ if ( ! meta ) {
178
+ meta = { }
179
+ }
165
180
166
181
const ast = {
167
182
type : 'postcss' ,
@@ -170,7 +185,7 @@ module.exports = function loader (css, map, meta) {
170
185
}
171
186
172
187
meta . ast = ast
173
- meta . messages = result . messages
188
+ meta . messages = messages
174
189
175
190
if ( this . loaderIndex === 0 ) {
176
191
/**
@@ -199,8 +214,12 @@ module.exports = function loader (css, map, meta) {
199
214
return null
200
215
} )
201
216
} ) . catch ( ( err ) => {
202
- if ( err . file ) this . addDependency ( err . file )
217
+ if ( err . file ) {
218
+ this . addDependency ( err . file )
219
+ }
203
220
204
- return err . name === 'CssSyntaxError' ? cb ( new SyntaxError ( err ) ) : cb ( err )
221
+ return err . name === 'CssSyntaxError'
222
+ ? cb ( new SyntaxError ( err ) )
223
+ : cb ( err )
205
224
} )
206
225
}
0 commit comments