@@ -35,38 +35,6 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
3535 // Determine hashing format.
3636 const hashFormat = getOutputHashFormat ( buildOptions . outputHashing as string ) ;
3737
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-
7038 // use includePaths from appConfig
7139 const includePaths : string [ ] = [ ] ;
7240 let lessPathOptions : { paths ?: string [ ] } = { } ;
@@ -186,7 +154,56 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
186154 } ,
187155 ] ;
188156
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+
189196 // 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+
190207 const rules : webpack . RuleSetRule [ ] = baseRules . map ( ( { test, use } ) => ( {
191208 exclude : globalStylePaths ,
192209 test,
@@ -195,15 +212,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
195212 {
196213 loader : require . resolve ( 'postcss-loader' ) ,
197214 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 ) ,
207216 } ,
208217 } ,
209218 ...use ,
@@ -212,6 +221,10 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
212221
213222 // load global css as css files
214223 if ( globalStylePaths . length > 0 ) {
224+ const globalSourceMap = ! ! (
225+ cssSourceMap && ! buildOptions . extractCss && ! buildOptions . sourceMap . hidden
226+ ) ;
227+
215228 rules . push (
216229 ...baseRules . map ( ( { test, use } ) => {
217230 return {
@@ -230,18 +243,13 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
230243 loader : require . resolve ( 'css-loader' ) ,
231244 options : {
232245 url : false ,
233- sourceMap : cssSourceMap ,
246+ sourceMap : globalSourceMap ,
234247 } ,
235248 } ,
236249 {
237250 loader : require . resolve ( 'postcss-loader' ) ,
238251 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 ) ,
245253 } ,
246254 } ,
247255 ...use ,
0 commit comments