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 { SuppressEntryChunksWebpackPlugin } from '../plugins/suppress-entry-chunks-webpack-plugin' ;
5
4
import { packageChunkSort } from '../utilities/package-chunk-sort' ;
6
5
import { BaseHrefWebpackPlugin } from '@angular-cli/base-href-webpack' ;
7
- import { extraEntryParser , makeCssLoaders , getOutputHashFormat } from './webpack-build-utils' ;
6
+ import { extraEntryParser , lazyChunksFilter , getOutputHashFormat } from './webpack-build-utils' ;
8
7
9
8
const autoprefixer = require ( 'autoprefixer' ) ;
10
9
const ProgressPlugin = require ( 'webpack/lib/ProgressPlugin' ) ;
@@ -33,19 +32,22 @@ export function getWebpackCommonConfig(
33
32
vendorChunk : boolean ,
34
33
verbose : boolean ,
35
34
progress : boolean ,
36
- outputHashing : string ,
37
- extractCss : boolean ,
35
+ outputHashing : string
38
36
) {
39
37
40
38
const appRoot = path . resolve ( projectRoot , appConfig . root ) ;
41
39
const nodeModules = path . resolve ( projectRoot , 'node_modules' ) ;
42
40
43
41
let extraPlugins : any [ ] = [ ] ;
44
42
let extraRules : any [ ] = [ ] ;
45
- let lazyChunks : string [ ] = [ ] ;
46
-
47
43
let entryPoints : { [ key : string ] : string [ ] } = { } ;
48
44
45
+ // figure out which are the lazy loaded entry points
46
+ const lazyChunks = lazyChunksFilter ( [
47
+ ...extraEntryParser ( appConfig . scripts , appRoot , 'scripts' ) ,
48
+ ...extraEntryParser ( appConfig . styles , appRoot , 'styles' )
49
+ ] ) ;
50
+
49
51
if ( appConfig . main ) {
50
52
entryPoints [ 'main' ] = [ path . resolve ( appRoot , appConfig . main ) ] ;
51
53
}
@@ -54,51 +56,22 @@ export function getWebpackCommonConfig(
54
56
const hashFormat = getOutputHashFormat ( outputHashing ) ;
55
57
56
58
// process global scripts
57
- if ( appConfig . scripts && appConfig . scripts . length > 0 ) {
59
+ if ( appConfig . scripts . length > 0 ) {
58
60
const globalScripts = extraEntryParser ( appConfig . scripts , appRoot , 'scripts' ) ;
59
61
60
- // add entry points and lazy chunks
61
- globalScripts . forEach ( script => {
62
- if ( script . lazy ) { lazyChunks . push ( script . entry ) ; }
63
- entryPoints [ script . entry ] = ( entryPoints [ script . entry ] || [ ] ) . concat ( script . path ) ;
64
- } ) ;
62
+ // add script entry points
63
+ globalScripts . forEach ( script =>
64
+ entryPoints [ script . entry ]
65
+ ? entryPoints [ script . entry ] . push ( script . path )
66
+ : entryPoints [ script . entry ] = [ script . path ]
67
+ ) ;
65
68
66
69
// load global scripts using script-loader
67
70
extraRules . push ( {
68
71
include : globalScripts . map ( ( script ) => script . path ) , test : / \. j s $ / , loader : 'script-loader'
69
72
} ) ;
70
73
}
71
74
72
- // process global styles
73
- if ( ! appConfig . styles || appConfig . styles . length === 0 ) {
74
- // create css loaders for component css
75
- extraRules . push ( ...makeCssLoaders ( ) ) ;
76
- } else {
77
- const globalStyles = extraEntryParser ( appConfig . styles , appRoot , 'styles' ) ;
78
- let extractedCssEntryPoints : string [ ] = [ ] ;
79
- // add entry points and lazy chunks
80
- globalStyles . forEach ( style => {
81
- if ( style . lazy ) { lazyChunks . push ( style . entry ) ; }
82
- if ( ! entryPoints [ style . entry ] ) {
83
- // since this entry point doesn't exist yet, it's going to only have
84
- // extracted css and we can supress the entry point
85
- extractedCssEntryPoints . push ( style . entry ) ;
86
- entryPoints [ style . entry ] = ( entryPoints [ style . entry ] || [ ] ) . concat ( style . path ) ;
87
- } else {
88
- // existing entry point, just push the css in
89
- entryPoints [ style . entry ] . push ( style . path ) ;
90
- }
91
- } ) ;
92
-
93
- // create css loaders for component css and for global css
94
- extraRules . push ( ...makeCssLoaders ( globalStyles . map ( ( style ) => style . path ) ) ) ;
95
-
96
- if ( extractCss && extractedCssEntryPoints . length > 0 ) {
97
- // don't emit the .js entry point for extracted styles
98
- extraPlugins . push ( new SuppressEntryChunksWebpackPlugin ( { chunks : extractedCssEntryPoints } ) ) ;
99
- }
100
- }
101
-
102
75
if ( vendorChunk ) {
103
76
extraPlugins . push ( new webpack . optimize . CommonsChunkPlugin ( {
104
77
name : 'vendor' ,
@@ -158,26 +131,16 @@ export function getWebpackCommonConfig(
158
131
module : {
159
132
rules : [
160
133
{ enforce : 'pre' , test : / \. j s $ / , loader : 'source-map-loader' , exclude : [ nodeModules ] } ,
161
-
162
134
{ test : / \. j s o n $ / , loader : 'json-loader' } ,
163
- {
164
- test : / \. ( j p g | p n g | g i f ) $ / ,
165
- loader : `url-loader?name=[name]${ hashFormat . file } .[ext]&limit=10000`
166
- } ,
167
135
{ test : / \. h t m l $ / , loader : 'raw-loader' } ,
168
-
136
+ { test : / \. ( e o t | s v g ) $ / , loader : `file-loader?name=[name] ${ hashFormat . file } .[ext]` } ,
169
137
{
170
- test : / \. ( o t f | t t f | w o f f | w o f f 2 ) $ / ,
138
+ test : / \. ( j p g | p n g | g i f | o t f | t t f | w o f f | w o f f 2 ) $ / ,
171
139
loader : `url-loader?name=[name]${ hashFormat . file } .[ext]&limit=10000`
172
- } ,
173
- { test : / \. ( e o t | s v g ) $ / , loader : `file-loader?name=[name]${ hashFormat . file } .[ext]` }
140
+ }
174
141
] . concat ( extraRules )
175
142
} ,
176
143
plugins : [
177
- new ExtractTextPlugin ( {
178
- filename : `[name]${ hashFormat . extract } .bundle.css` ,
179
- disable : ! extractCss
180
- } ) ,
181
144
new HtmlWebpackPlugin ( {
182
145
template : path . resolve ( appRoot , appConfig . index ) ,
183
146
filename : path . resolve ( appConfig . outDir , appConfig . index ) ,
@@ -191,18 +154,6 @@ export function getWebpackCommonConfig(
191
154
new webpack . optimize . CommonsChunkPlugin ( {
192
155
minChunks : Infinity ,
193
156
name : 'inline'
194
- } ) ,
195
- new webpack . LoaderOptionsPlugin ( {
196
- test : / \. ( c s s | s c s s | s a s s | l e s s | s t y l ) $ / ,
197
- options : {
198
- postcss : [ autoprefixer ( ) ] ,
199
- cssLoader : { sourceMap : sourcemap } ,
200
- sassLoader : { sourceMap : sourcemap } ,
201
- lessLoader : { sourceMap : sourcemap } ,
202
- stylusLoader : { sourceMap : sourcemap } ,
203
- // context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
204
- context : projectRoot ,
205
- } ,
206
157
} )
207
158
] . concat ( extraPlugins ) ,
208
159
node : {
0 commit comments