@@ -47,53 +47,55 @@ module.exports = function loader (css, map) {
47
47
48
48
validateOptions ( require ( './options.json' ) , options , 'PostCSS Loader' )
49
49
50
- const rc = {
51
- path : path . dirname ( file ) ,
52
- ctx : {
53
- file : {
54
- extname : path . extname ( file ) ,
55
- dirname : path . dirname ( file ) ,
56
- basename : path . basename ( file )
57
- } ,
58
- options : { }
50
+ Promise . resolve ( ) . then ( ( ) => {
51
+ if ( ! options . config ) {
52
+ return parseOptions . call ( this , options )
59
53
}
60
- }
61
54
62
- if ( options . config ) {
63
- if ( options . config . path ) {
64
- rc . path = path . resolve ( options . config . path )
55
+ const rc = {
56
+ path : path . dirname ( file ) ,
57
+ ctx : {
58
+ file : {
59
+ extname : path . extname ( file ) ,
60
+ dirname : path . dirname ( file ) ,
61
+ basename : path . basename ( file )
62
+ } ,
63
+ options : { }
64
+ }
65
65
}
66
66
67
- if ( options . config . ctx ) {
68
- rc . ctx . options = options . config . ctx
69
- }
70
- }
71
-
72
- const sourceMap = options . sourceMap
73
-
74
- Promise . resolve ( ) . then ( ( ) => {
75
- const length = Object . keys ( options ) . length
76
-
77
- // TODO
78
- // Refactor
79
- if ( ! options . config && ! sourceMap && length ) {
80
- return parseOptions . call ( this , options )
81
- } else if ( options . config && ! sourceMap && length > 1 ) {
82
- return parseOptions . call ( this , options )
83
- } else if ( ! options . config && sourceMap && length > 1 ) {
84
- return parseOptions . call ( this , options )
85
- } else if ( options . config && sourceMap && length > 2 ) {
86
- return parseOptions . call ( this , options )
67
+ // Read options from options.config only when options.config is object
68
+ if ( typeof options . config === 'object' &&
69
+ options . config !== null &&
70
+ Object . prototype . toString . call ( options . config ) === '[object Object]'
71
+ ) {
72
+ if ( options . config . path ) {
73
+ rc . path = path . resolve ( options . config . path )
74
+ }
75
+
76
+ if ( options . config . ctx ) {
77
+ rc . ctx . options = options . config . ctx
78
+ }
87
79
}
88
80
89
81
return postcssrc ( rc . ctx , rc . path , { argv : false } )
90
82
} ) . then ( ( config ) => {
91
83
if ( ! config ) config = { }
84
+ if ( ! config . options ) config . options = { }
92
85
93
86
if ( config . file ) this . addDependency ( config . file )
94
87
88
+ let sourceMap = options . sourceMap || config . options . map
95
89
let plugins = config . plugins || [ ]
96
- let options = Object . assign ( {
90
+
91
+ // Disable override `to` option
92
+ if ( config . options . to ) delete config . options . to
93
+ // Disable override `from` option
94
+ if ( config . options . from ) delete config . options . from
95
+ // Disable override `map` option
96
+ if ( config . options . map ) delete config . options . map
97
+
98
+ let postcssOption = Object . assign ( {
97
99
to : file ,
98
100
from : file ,
99
101
map : sourceMap
@@ -105,20 +107,20 @@ module.exports = function loader (css, map) {
105
107
106
108
// Loader Exec (Deprecated)
107
109
// https://webpack.js.org/api/loaders/#deprecated-context-properties
108
- if ( options . parser === 'postcss-js' ) {
110
+ if ( postcssOption . parser === 'postcss-js' ) {
109
111
css = this . exec ( css , this . resource )
110
112
}
111
113
112
- if ( typeof options . parser === 'string' ) {
113
- options . parser = require ( options . parser )
114
+ if ( typeof postcssOption . parser === 'string' ) {
115
+ postcssOption . parser = require ( postcssOption . parser )
114
116
}
115
117
116
- if ( typeof options . syntax === 'string' ) {
117
- options . syntax = require ( options . syntax )
118
+ if ( typeof postcssOption . syntax === 'string' ) {
119
+ postcssOption . syntax = require ( postcssOption . syntax )
118
120
}
119
121
120
- if ( typeof options . stringifier === 'string' ) {
121
- options . stringifier = require ( options . stringifier )
122
+ if ( typeof postcssOption . stringifier === 'string' ) {
123
+ postcssOption . stringifier = require ( postcssOption . stringifier )
122
124
}
123
125
124
126
// Loader API Exec (Deprecated)
@@ -132,10 +134,10 @@ module.exports = function loader (css, map) {
132
134
}
133
135
134
136
if ( sourceMap && typeof map === 'string' ) map = JSON . parse ( map )
135
- if ( sourceMap && map ) options . map . prev = map
137
+ if ( sourceMap && map ) postcssOption . map . prev = map
136
138
137
139
return postcss ( plugins )
138
- . process ( css , options )
140
+ . process ( css , postcssOption )
139
141
. then ( ( result ) => {
140
142
result . warnings ( ) . forEach ( ( msg ) => this . emitWarning ( msg . toString ( ) ) )
141
143
0 commit comments