@@ -35,38 +35,6 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
35
35
// Determine hashing format.
36
36
const hashFormat = getOutputHashFormat ( buildOptions . outputHashing as string ) ;
37
37
38
- const postcssPluginCreator = function ( loader : webpack . loader . LoaderContext ) {
39
- return [
40
- postcssImports ( {
41
- resolve : ( url : string ) => ( url . startsWith ( '~' ) ? url . substr ( 1 ) : url ) ,
42
- load : ( filename : string ) => {
43
- return new Promise < string > ( ( resolve , reject ) => {
44
- loader . fs . readFile ( filename , ( err : Error , data : Buffer ) => {
45
- if ( err ) {
46
- reject ( err ) ;
47
-
48
- return ;
49
- }
50
-
51
- const content = data . toString ( ) ;
52
- resolve ( content ) ;
53
- } ) ;
54
- } ) ;
55
- } ,
56
- } ) ,
57
- PostcssCliResources ( {
58
- baseHref : buildOptions . baseHref ,
59
- deployUrl : buildOptions . deployUrl ,
60
- resourcesOutputPath : buildOptions . resourcesOutputPath ,
61
- loader,
62
- rebaseRootRelative : buildOptions . rebaseRootRelativeCssUrls ,
63
- filename : `[name]${ hashFormat . file } .[ext]` ,
64
- emitFile : buildOptions . platform !== 'server' ,
65
- } ) ,
66
- autoprefixer ( ) ,
67
- ] ;
68
- } ;
69
-
70
38
// use includePaths from appConfig
71
39
const includePaths : string [ ] = [ ] ;
72
40
let lessPathOptions : { paths ?: string [ ] } = { } ;
@@ -186,7 +154,56 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
186
154
} ,
187
155
] ;
188
156
157
+ const postcssOptionsCreator = ( sourceMap : boolean , extracted : boolean | undefined ) => {
158
+ return ( loader : webpack . loader . LoaderContext ) => ( {
159
+ map : sourceMap && {
160
+ inline : true ,
161
+ annotation : false ,
162
+ } ,
163
+ plugins : [
164
+ postcssImports ( {
165
+ resolve : ( url : string ) => url . startsWith ( '~' ) ? url . substr ( 1 ) : url ,
166
+ load : ( filename : string ) => {
167
+ return new Promise < string > ( ( resolve , reject ) => {
168
+ loader . fs . readFile ( filename , ( err : Error , data : Buffer ) => {
169
+ if ( err ) {
170
+ reject ( err ) ;
171
+
172
+ return ;
173
+ }
174
+
175
+ const content = data . toString ( ) ;
176
+ resolve ( content ) ;
177
+ } ) ;
178
+ } ) ;
179
+ } ,
180
+ } ) ,
181
+ PostcssCliResources ( {
182
+ baseHref : buildOptions . baseHref ,
183
+ deployUrl : buildOptions . deployUrl ,
184
+ resourcesOutputPath : buildOptions . resourcesOutputPath ,
185
+ loader,
186
+ rebaseRootRelative : buildOptions . rebaseRootRelativeCssUrls ,
187
+ filename : `[name]${ hashFormat . file } .[ext]` ,
188
+ emitFile : buildOptions . platform !== 'server' ,
189
+ extracted,
190
+ } ) ,
191
+ autoprefixer ( ) ,
192
+ ] ,
193
+ } ) ;
194
+ } ;
195
+
189
196
// load component css as raw strings
197
+ const componentsSourceMap = ! ! (
198
+ cssSourceMap
199
+ // Never use component css sourcemap when style optimizations are on.
200
+ // It will just increase bundle size without offering good debug experience.
201
+ && ! buildOptions . optimization . styles
202
+ // Inline all sourcemap types except hidden ones, which are the same as no sourcemaps
203
+ // for component css.
204
+ && ! buildOptions . sourceMap . hidden
205
+ ) ;
206
+
190
207
const rules : webpack . RuleSetRule [ ] = baseRules . map ( ( { test, use } ) => ( {
191
208
exclude : globalStylePaths ,
192
209
test,
@@ -195,15 +212,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
195
212
{
196
213
loader : require . resolve ( 'postcss-loader' ) ,
197
214
options : {
198
- ident : 'embedded' ,
199
- plugins : postcssPluginCreator ,
200
- sourceMap : cssSourceMap
201
- // Never use component css sourcemap when style optimizations are on.
202
- // It will just increase bundle size without offering good debug experience.
203
- && ! buildOptions . optimization . styles
204
- // Inline all sourcemap types except hidden ones, which are the same as no sourcemaps
205
- // for component css.
206
- && ! buildOptions . sourceMap . hidden ? 'inline' : false ,
215
+ postcssOptions : postcssOptionsCreator ( componentsSourceMap , false ) ,
207
216
} ,
208
217
} ,
209
218
...use ,
@@ -212,6 +221,10 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
212
221
213
222
// load global css as css files
214
223
if ( globalStylePaths . length > 0 ) {
224
+ const globalSourceMap = ! ! (
225
+ cssSourceMap && ! buildOptions . extractCss && ! buildOptions . sourceMap . hidden
226
+ ) ;
227
+
215
228
rules . push (
216
229
...baseRules . map ( ( { test, use } ) => {
217
230
return {
@@ -230,18 +243,13 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
230
243
loader : require . resolve ( 'css-loader' ) ,
231
244
options : {
232
245
url : false ,
233
- sourceMap : cssSourceMap ,
246
+ sourceMap : globalSourceMap ,
234
247
} ,
235
248
} ,
236
249
{
237
250
loader : require . resolve ( 'postcss-loader' ) ,
238
251
options : {
239
- ident : buildOptions . extractCss ? 'extracted' : 'embedded' ,
240
- plugins : postcssPluginCreator ,
241
- sourceMap :
242
- cssSourceMap && ! buildOptions . extractCss && ! buildOptions . sourceMap . hidden
243
- ? 'inline'
244
- : cssSourceMap ,
252
+ postcssOptions : postcssOptionsCreator ( globalSourceMap , buildOptions . extractCss ) ,
245
253
} ,
246
254
} ,
247
255
...use ,
0 commit comments