@@ -47,53 +47,57 @@ 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
66
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
- } ) . then ( ( config ) => {
91
- if ( ! config ) config = { }
82
+ . then ( ( config ) => {
83
+ if ( ! config . options . map ) config . options . map = options . sourceMap
92
84
85
+ return config
86
+ } )
87
+ } ) . then ( ( config ) => {
93
88
if ( config . file ) this . addDependency ( config . file )
94
89
90
+ let sourceMap = config . options . map
95
91
let plugins = config . plugins || [ ]
96
- let options = Object . assign ( {
92
+
93
+ // Disable override `to` option
94
+ if ( config . options . to ) delete config . options . to
95
+ // Disable override `from` option
96
+ if ( config . options . from ) delete config . options . from
97
+ // Disable override `map` option
98
+ if ( config . options . map ) delete config . options . map
99
+
100
+ let postcssOption = Object . assign ( {
97
101
to : file ,
98
102
from : file ,
99
103
map : sourceMap
@@ -105,20 +109,20 @@ module.exports = function loader (css, map) {
105
109
106
110
// Loader Exec (Deprecated)
107
111
// https://webpack.js.org/api/loaders/#deprecated-context-properties
108
- if ( options . parser === 'postcss-js' ) {
112
+ if ( postcssOption . parser === 'postcss-js' ) {
109
113
css = this . exec ( css , this . resource )
110
114
}
111
115
112
- if ( typeof options . parser === 'string' ) {
113
- options . parser = require ( options . parser )
116
+ if ( typeof postcssOption . parser === 'string' ) {
117
+ postcssOption . parser = require ( postcssOption . parser )
114
118
}
115
119
116
- if ( typeof options . syntax === 'string' ) {
117
- options . syntax = require ( options . syntax )
120
+ if ( typeof postcssOption . syntax === 'string' ) {
121
+ postcssOption . syntax = require ( postcssOption . syntax )
118
122
}
119
123
120
- if ( typeof options . stringifier === 'string' ) {
121
- options . stringifier = require ( options . stringifier )
124
+ if ( typeof postcssOption . stringifier === 'string' ) {
125
+ postcssOption . stringifier = require ( postcssOption . stringifier )
122
126
}
123
127
124
128
// Loader API Exec (Deprecated)
@@ -132,10 +136,10 @@ module.exports = function loader (css, map) {
132
136
}
133
137
134
138
if ( sourceMap && typeof map === 'string' ) map = JSON . parse ( map )
135
- if ( sourceMap && map ) options . map . prev = map
139
+ if ( sourceMap && map ) postcssOption . map . prev = map
136
140
137
141
return postcss ( plugins )
138
- . process ( css , options )
142
+ . process ( css , postcssOption )
139
143
. then ( ( result ) => {
140
144
result . warnings ( ) . forEach ( ( msg ) => this . emitWarning ( msg . toString ( ) ) )
141
145
0 commit comments