@@ -11,6 +11,7 @@ import {
11
11
getWebpackMobileConfigPartial ,
12
12
getWebpackMobileProdConfigPartial
13
13
} from './' ;
14
+ import * as webpack from 'webpack' ;
14
15
15
16
export class NgCliWebpackConfig {
16
17
// TODO: When webpack2 types are finished lets replace all these any types
@@ -45,13 +46,13 @@ export class NgCliWebpackConfig {
45
46
let mobileConfigPartial = getWebpackMobileConfigPartial ( this . ngCliProject . root , appConfig ) ;
46
47
let mobileProdConfigPartial = getWebpackMobileProdConfigPartial ( this . ngCliProject . root ,
47
48
appConfig ) ;
48
- baseConfig = webpackMerge ( baseConfig , mobileConfigPartial ) ;
49
+ baseConfig = this . betterWebpackMerge ( baseConfig , mobileConfigPartial ) ;
49
50
if ( this . target == 'production' ) {
50
- targetConfigPartial = webpackMerge ( targetConfigPartial , mobileProdConfigPartial ) ;
51
+ targetConfigPartial = this . betterWebpackMerge ( targetConfigPartial , mobileProdConfigPartial ) ;
51
52
}
52
53
}
53
54
54
- this . config = webpackMerge (
55
+ this . config = this . betterWebpackMerge (
55
56
baseConfig ,
56
57
targetConfigPartial ,
57
58
typescriptConfigPartial
@@ -68,4 +69,43 @@ export class NgCliWebpackConfig {
68
69
throw new Error ( "Invalid build target. Only 'development' and 'production' are available." ) ;
69
70
}
70
71
}
72
+
73
+ betterWebpackMerge ( baseConfig : any , targetConfig : any , targetTypeScriptConfig ?: any ) {
74
+ let basePlugins = ( baseConfig && baseConfig . plugins ) || [ ] ;
75
+ let targetPlugins = ( targetConfig && targetConfig . plugins ) || [ ] ;
76
+ let targetTypeScriptPlugins = ( targetTypeScriptConfig && targetTypeScriptConfig . plugins ) || [ ] ;
77
+ let baseOptionsPlugins = basePlugins
78
+ . filter ( ( plugin : any ) => plugin instanceof webpack . LoaderOptionsPlugin )
79
+ . map ( ( plugin : any ) => plugin . options . options ) ;
80
+ let targetOptionsPlugins = targetPlugins
81
+ . filter ( ( plugin : any ) => plugin instanceof webpack . LoaderOptionsPlugin )
82
+ . map ( ( plugin : any ) => plugin . options . options ) ;
83
+ let targetTypeScriptOptionsPlugins = targetTypeScriptPlugins
84
+ . filter ( ( plugin : any ) => plugin instanceof webpack . LoaderOptionsPlugin )
85
+ . map ( ( plugin : any ) => plugin . options . options ) ;
86
+ let mergedConfig = webpackMerge ( baseConfig , targetConfig , targetTypeScriptConfig ) ;
87
+ if ( baseOptionsPlugins . length > 0 || targetOptionsPlugins . length > 0 || targetTypeScriptOptionsPlugins . length > 0 ) {
88
+ let mergedBase = baseOptionsPlugins . reduce ( ( base : any , next : any ) => {
89
+ Object . assign ( base , next ) ;
90
+ return base ;
91
+ } , { } ) ;
92
+ let mergedTarget = targetOptionsPlugins . reduce ( ( base : any , next : any ) => {
93
+ Object . assign ( base , next ) ;
94
+ return base ;
95
+ } , { } ) ;
96
+ let mergedTargetTypeScript = targetTypeScriptOptionsPlugins . reduce ( ( base : any , next : any ) => {
97
+ Object . assign ( base , next ) ;
98
+ return base ;
99
+ } , { } ) ;
100
+ if ( mergedConfig . plugins ) {
101
+ mergedConfig . plugins = mergedConfig . plugins . filter ( ( plugin : any ) => {
102
+ return ! ( plugin instanceof webpack . LoaderOptionsPlugin ) ;
103
+ } ) ;
104
+ mergedConfig . plugins . push ( new webpack . LoaderOptionsPlugin ( {
105
+ options : Object . assign ( mergedBase , mergedTarget , mergedTargetTypeScript )
106
+ } ) ) ;
107
+ }
108
+ }
109
+ return mergedConfig ;
110
+ }
71
111
}
0 commit comments