1- 'use strict'
2-
31const path = require ( 'path' )
42
5- const loaderUtils = require ( 'loader-utils' )
6-
7- const parseOptions = require ( './options' )
3+ const { getOptions } = require ( 'loader-utils' )
84const validateOptions = require ( 'schema-utils' )
95
106const postcss = require ( 'postcss' )
@@ -42,7 +38,7 @@ const parseOptions = require('./options.js')
4238 * @return {cb } cb Result
4339 */
4440module . exports = function loader ( css , map , meta ) {
45- const options = Object . assign ( { } , loaderUtils . getOptions ( this ) )
41+ const options = Object . assign ( { } , getOptions ( this ) )
4642
4743 validateOptions ( require ( './options.json' ) , options , 'PostCSS Loader' )
4844
@@ -96,16 +92,25 @@ module.exports = function loader (css, map, meta) {
9692
9793 return postcssrc ( rc . ctx , rc . path )
9894 } ) . then ( ( config ) => {
99- if ( ! config ) config = { }
95+ if ( ! config ) {
96+ config = { }
97+ }
10098
101- if ( config . file ) this . addDependency ( config . file )
99+ if ( config . file ) {
100+ this . addDependency ( config . file )
101+ }
102102
103103 // 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+ }
105107 // 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+ }
107111
108112 let plugins = config . plugins || [ ]
113+
109114 let options = Object . assign ( {
110115 from : file ,
111116 map : sourceMap
@@ -139,29 +144,39 @@ module.exports = function loader (css, map, meta) {
139144 css = this . exec ( css , this . resource )
140145 }
141146
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+ }
144154
145155 return postcss ( plugins )
146156 . process ( css , options )
147157 . then ( ( result ) => {
158+ let { css, map, root, processor, messages } = result
159+
148160 result . warnings ( ) . forEach ( ( warning ) => {
149161 this . emitWarning ( new Warning ( warning ) )
150162 } )
151163
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+ }
154168 } )
155169
156- css = result . css
157- map = result . map ? result . map . toJSON ( ) : null
170+ map = map ? map . toJSON ( ) : null
158171
159172 if ( map ) {
160173 map . file = path . resolve ( map . file )
161174 map . sources = map . sources . map ( ( src ) => path . resolve ( src ) )
162175 }
163176
164- if ( ! meta ) meta = { }
177+ if ( ! meta ) {
178+ meta = { }
179+ }
165180
166181 const ast = {
167182 type : 'postcss' ,
@@ -170,7 +185,7 @@ module.exports = function loader (css, map, meta) {
170185 }
171186
172187 meta . ast = ast
173- meta . messages = result . messages
188+ meta . messages = messages
174189
175190 if ( this . loaderIndex === 0 ) {
176191 /**
@@ -199,8 +214,12 @@ module.exports = function loader (css, map, meta) {
199214 return null
200215 } )
201216 } ) . catch ( ( err ) => {
202- if ( err . file ) this . addDependency ( err . file )
217+ if ( err . file ) {
218+ this . addDependency ( err . file )
219+ }
203220
204- return err . name === 'CssSyntaxError' ? cb ( new SyntaxError ( err ) ) : cb ( err )
221+ return err . name === 'CssSyntaxError'
222+ ? cb ( new SyntaxError ( err ) )
223+ : cb ( err )
205224 } )
206225}
0 commit comments