@@ -5,8 +5,10 @@ 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 , getOutputHashFormat } from './webpack-build-utils' ;
8
+ import { CliConfig } from '../lib/config/schema.d' ;
8
9
9
10
const autoprefixer = require ( 'autoprefixer' ) ;
11
+ const postcssDiscardComments = require ( 'postcss-discard-comments' ) ;
10
12
const ProgressPlugin = require ( 'webpack/lib/ProgressPlugin' ) ;
11
13
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
12
14
const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ;
@@ -28,6 +30,7 @@ export function getWebpackCommonConfig(
28
30
projectRoot : string ,
29
31
environment : string ,
30
32
appConfig : any ,
33
+ cliConfig : CliConfig ,
31
34
baseHref : string ,
32
35
sourcemap : boolean ,
33
36
vendorChunk : boolean ,
@@ -49,6 +52,39 @@ export function getWebpackCommonConfig(
49
52
entryPoints [ 'main' ] = [ path . resolve ( appRoot , appConfig . main ) ] ;
50
53
}
51
54
55
+ // Configure webpack style loaders
56
+
57
+ /**
58
+ * Base settings for webpack preprocessor loaders
59
+ * @type {Object }
60
+ */
61
+ const basePreprocessorLoaderOptions = {
62
+ sourceMap : sourcemap ,
63
+ } ;
64
+
65
+ // set default to base
66
+ let preprocessorLoaderOptions = basePreprocessorLoaderOptions ;
67
+
68
+ if ( appConfig . webpackStyleLoaderOptions ) {
69
+ if ( appConfig . webpackStyleLoaderOptions . includePaths ) {
70
+ // resolve paths relative to project root
71
+ let includePaths = appConfig . webpackStyleLoaderOptions . includePaths . map (
72
+ ( includePath : string ) => path . resolve ( projectRoot , includePath )
73
+ ) ;
74
+ if ( cliConfig . defaults . styleExt === 'styl' || cliConfig . defaults . styleExt === 'less' ) {
75
+ // stylus and less uses paths
76
+ preprocessorLoaderOptions = Object . assign ( { } , basePreprocessorLoaderOptions , {
77
+ paths : includePaths
78
+ } ) ;
79
+ } else {
80
+ // sass use includePaths
81
+ preprocessorLoaderOptions = Object . assign ( { } , basePreprocessorLoaderOptions , {
82
+ includePaths
83
+ } ) ;
84
+ }
85
+ }
86
+ }
87
+
52
88
// determine hashing format
53
89
const hashFormat = getOutputHashFormat ( outputHashing ) ;
54
90
@@ -191,11 +227,15 @@ export function getWebpackCommonConfig(
191
227
new webpack . LoaderOptionsPlugin ( {
192
228
test : / \. ( c s s | s c s s | s a s s | l e s s | s t y l ) $ / ,
193
229
options : {
194
- postcss : [ autoprefixer ( ) ] ,
195
- cssLoader : { sourceMap : sourcemap } ,
196
- sassLoader : { sourceMap : sourcemap } ,
197
- lessLoader : { sourceMap : sourcemap } ,
198
- stylusLoader : { sourceMap : sourcemap } ,
230
+ postcss : [
231
+ autoprefixer ( ) ,
232
+ // NOTE: Moved check here for prod build
233
+ ...( environment === 'prod' ? [ postcssDiscardComments ] : [ ] )
234
+ ] ,
235
+ cssLoader : preprocessorLoaderOptions ,
236
+ sassLoader : preprocessorLoaderOptions ,
237
+ lessLoader : preprocessorLoaderOptions ,
238
+ stylusLoader : preprocessorLoaderOptions ,
199
239
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
200
240
context : projectRoot ,
201
241
} ,
0 commit comments