@@ -47,53 +47,63 @@ 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 : { }
59
- }
60
- }
61
-
62
- if ( options . config ) {
63
- if ( options . config . path ) {
64
- rc . path = path . resolve ( options . config . path )
50
+ Promise . resolve ( ) . then ( ( ) => {
51
+ if ( ! options . config ) {
52
+ return parseOptions . call ( this , options )
65
53
}
66
54
67
- if ( options . config . ctx ) {
68
- rc . ctx . options = options . config . ctx
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
+ }
69
65
}
70
- }
71
-
72
- const sourceMap = options . sourceMap
73
66
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 ) {
93
+ delete config . options . to ;
94
+ }
95
+
96
+ // Disable override `from` option
97
+ if ( config . options . from ) {
98
+ delete config . options . from ;
99
+ }
100
+
101
+ // Disable override `map` option
102
+ if ( config . options . map ) {
103
+ delete config . options . map ;
104
+ }
105
+
106
+ let postcssOption = Object . assign ( {
97
107
to : file ,
98
108
from : file ,
99
109
map : sourceMap
@@ -105,20 +115,20 @@ module.exports = function loader (css, map) {
105
115
106
116
// Loader Exec (Deprecated)
107
117
// https://webpack.js.org/api/loaders/#deprecated-context-properties
108
- if ( options . parser === 'postcss-js' ) {
118
+ if ( postcssOption . parser === 'postcss-js' ) {
109
119
css = this . exec ( css , this . resource )
110
120
}
111
121
112
- if ( typeof options . parser === 'string' ) {
113
- options . parser = require ( options . parser )
122
+ if ( typeof postcssOption . parser === 'string' ) {
123
+ postcssOption . parser = require ( postcssOption . parser )
114
124
}
115
125
116
- if ( typeof options . syntax === 'string' ) {
117
- options . syntax = require ( options . syntax )
126
+ if ( typeof postcssOption . syntax === 'string' ) {
127
+ postcssOption . syntax = require ( postcssOption . syntax )
118
128
}
119
129
120
- if ( typeof options . stringifier === 'string' ) {
121
- options . stringifier = require ( options . stringifier )
130
+ if ( typeof postcssOption . stringifier === 'string' ) {
131
+ postcssOption . stringifier = require ( postcssOption . stringifier )
122
132
}
123
133
124
134
// Loader API Exec (Deprecated)
@@ -132,10 +142,10 @@ module.exports = function loader (css, map) {
132
142
}
133
143
134
144
if ( sourceMap && typeof map === 'string' ) map = JSON . parse ( map )
135
- if ( sourceMap && map ) options . map . prev = map
145
+ if ( sourceMap && map ) postcssOption . map . prev = map
136
146
137
147
return postcss ( plugins )
138
- . process ( css , options )
148
+ . process ( css , postcssOption )
139
149
. then ( ( result ) => {
140
150
result . warnings ( ) . forEach ( ( msg ) => this . emitWarning ( msg . toString ( ) ) )
141
151
0 commit comments