@@ -5,8 +5,11 @@ import { SuppressEntryChunksWebpackPlugin } from '../plugins/suppress-entry-chun
5
5
import { packageChunkSort } from '../utilities/package-chunk-sort' ;
6
6
import { BaseHrefWebpackPlugin } from '@angular-cli/base-href-webpack' ;
7
7
import { extraEntryParser , makeCssLoaders } from './webpack-build-utils' ;
8
+ import { CliConfig } from '../lib/config/schema.d' ;
9
+
8
10
9
11
const autoprefixer = require ( 'autoprefixer' ) ;
12
+ const postcssDiscardComments = require ( 'postcss-discard-comments' ) ;
10
13
const ProgressPlugin = require ( 'webpack/lib/ProgressPlugin' ) ;
11
14
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
12
15
const SilentError = require ( 'silent-error' ) ;
@@ -27,6 +30,7 @@ export function getWebpackCommonConfig(
27
30
projectRoot : string ,
28
31
environment : string ,
29
32
appConfig : any ,
33
+ cliConfig : CliConfig ,
30
34
baseHref : string ,
31
35
sourcemap : boolean ,
32
36
vendorChunk : boolean ,
@@ -46,6 +50,39 @@ export function getWebpackCommonConfig(
46
50
main : [ appMain ]
47
51
} ;
48
52
53
+ // Configure webpack style loaders
54
+
55
+ /**
56
+ * Base settings for webpack style loaders
57
+ * @type {Object }
58
+ */
59
+ const baseStyleLoaderOptions = {
60
+ sourceMap : sourcemap ,
61
+ } ;
62
+
63
+ // set default to base
64
+ let styleLoaderOptions = baseStyleLoaderOptions ;
65
+
66
+ if ( appConfig . webpackStyleLoaderOptions ) {
67
+ if ( appConfig . webpackStyleLoaderOptions . includePaths ) {
68
+ // resolve paths relative to project root
69
+ let includePaths = appConfig . webpackStyleLoaderOptions . includePaths . map (
70
+ ( includePath : string ) => path . resolve ( projectRoot , includePath )
71
+ ) ;
72
+ if ( cliConfig . defaults . styleExt === 'styl' ) {
73
+ // stylus uses paths
74
+ styleLoaderOptions = Object . assign ( { } , baseStyleLoaderOptions , {
75
+ paths : includePaths
76
+ } ) ;
77
+ } else {
78
+ // less and sass use includePaths
79
+ styleLoaderOptions = Object . assign ( { } , baseStyleLoaderOptions , {
80
+ includePaths
81
+ } ) ;
82
+ }
83
+ }
84
+ }
85
+
49
86
if ( ! ( environment in appConfig . environments ) ) {
50
87
throw new SilentError ( `Environment "${ environment } " does not exist.` ) ;
51
88
}
@@ -163,11 +200,15 @@ export function getWebpackCommonConfig(
163
200
new webpack . LoaderOptionsPlugin ( {
164
201
test : / \. ( c s s | s c s s | s a s s | l e s s | s t y l ) $ / ,
165
202
options : {
166
- postcss : [ autoprefixer ( ) ] ,
167
- cssLoader : { sourceMap : sourcemap } ,
168
- sassLoader : { sourceMap : sourcemap } ,
169
- lessLoader : { sourceMap : sourcemap } ,
170
- stylusLoader : { sourceMap : sourcemap } ,
203
+ postcss : [
204
+ autoprefixer ( ) ,
205
+ // NOTE: Moved check here for prod build
206
+ ...( environment === 'prod' ? [ postcssDiscardComments ] : [ ] )
207
+ ] ,
208
+ cssLoader : styleLoaderOptions ,
209
+ sassLoader : styleLoaderOptions ,
210
+ lessLoader : styleLoaderOptions ,
211
+ stylusLoader : styleLoaderOptions ,
171
212
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
172
213
context : projectRoot ,
173
214
} ,
0 commit comments