1
1
import * as webpack from 'webpack' ;
2
2
import * as path from 'path' ;
3
3
import { GlobCopyWebpackPlugin } from '../plugins/glob-copy-webpack-plugin' ;
4
+ import { packageChunkSort } from '../utilities/package-chunk-sort' ;
4
5
import { BaseHrefWebpackPlugin } from '@angular-cli/base-href-webpack' ;
5
6
6
7
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
@@ -12,17 +13,20 @@ export function getWebpackCommonConfig(
12
13
environment : string ,
13
14
appConfig : any ,
14
15
baseHref : string ,
15
- sourcemap : boolean
16
+ sourcemap : boolean ,
17
+ vendorChunk : boolean
16
18
) {
17
19
18
20
const appRoot = path . resolve ( projectRoot , appConfig . root ) ;
19
21
const appMain = path . resolve ( appRoot , appConfig . main ) ;
22
+ const nodeModules = path . resolve ( projectRoot , 'node_modules' ) ;
20
23
const styles = appConfig . styles
21
24
? appConfig . styles . map ( ( style : string ) => path . resolve ( appRoot , style ) )
22
25
: [ ] ;
23
26
const scripts = appConfig . scripts
24
27
? appConfig . scripts . map ( ( script : string ) => path . resolve ( appRoot , script ) )
25
28
: [ ] ;
29
+ const extraPlugins : any [ ] = [ ] ;
26
30
27
31
let entry : { [ key : string ] : string [ ] } = {
28
32
main : [ appMain ]
@@ -32,11 +36,19 @@ export function getWebpackCommonConfig(
32
36
if ( appConfig . styles . length > 0 ) { entry [ 'styles' ] = styles ; }
33
37
if ( appConfig . scripts . length > 0 ) { entry [ 'scripts' ] = scripts ; }
34
38
39
+ if ( vendorChunk ) {
40
+ extraPlugins . push ( new webpack . optimize . CommonsChunkPlugin ( {
41
+ name : 'vendor' ,
42
+ chunks : [ 'main' ] ,
43
+ minChunks : ( module : any ) => module . userRequest && module . userRequest . startsWith ( nodeModules )
44
+ } ) ) ;
45
+ }
46
+
35
47
return {
36
48
devtool : sourcemap ? 'source-map' : false ,
37
49
resolve : {
38
50
extensions : [ '.ts' , '.js' ] ,
39
- modules : [ path . resolve ( projectRoot , 'node_modules' ) ]
51
+ modules : [ nodeModules ]
40
52
} ,
41
53
context : path . resolve ( __dirname , './' ) ,
42
54
entry : entry ,
@@ -52,9 +64,7 @@ export function getWebpackCommonConfig(
52
64
enforce : 'pre' ,
53
65
test : / \. j s $ / ,
54
66
loader : 'source-map-loader' ,
55
- exclude : [
56
- / n o d e _ m o d u l e s /
57
- ]
67
+ exclude : [ nodeModules ]
58
68
} ,
59
69
// in main, load css as raw text
60
70
{
@@ -91,7 +101,7 @@ export function getWebpackCommonConfig(
91
101
new HtmlWebpackPlugin ( {
92
102
template : path . resolve ( appRoot , appConfig . index ) ,
93
103
filename : path . resolve ( appConfig . outDir , appConfig . index ) ,
94
- chunksSortMode : 'dependency'
104
+ chunksSortMode : packageChunkSort ( [ 'inline' , 'styles' , 'scripts' , 'vendor' , 'main' ] )
95
105
} ) ,
96
106
new BaseHrefWebpackPlugin ( {
97
107
baseHref : baseHref
@@ -104,10 +114,6 @@ export function getWebpackCommonConfig(
104
114
. replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ) ,
105
115
path . resolve ( appRoot , appConfig . environments [ environment ] )
106
116
) ,
107
- new webpack . optimize . CommonsChunkPlugin ( {
108
- // Optimizing ensures loading order in index.html
109
- name : [ 'styles' , 'scripts' , 'main' ] . reverse ( )
110
- } ) ,
111
117
new webpack . optimize . CommonsChunkPlugin ( {
112
118
minChunks : Infinity ,
113
119
name : 'inline'
@@ -121,8 +127,8 @@ export function getWebpackCommonConfig(
121
127
options : {
122
128
postcss : [ autoprefixer ( ) ]
123
129
} ,
124
- } ) ,
125
- ] ,
130
+ } )
131
+ ] . concat ( extraPlugins ) ,
126
132
node : {
127
133
fs : 'empty' ,
128
134
global : true ,
0 commit comments