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' ,
@@ -157,26 +130,16 @@ export function getWebpackCommonConfig(
157
130
module : {
158
131
rules : [
159
132
{ enforce : 'pre' , test : / \. j s $ / , loader : 'source-map-loader' , exclude : [ nodeModules ] } ,
160
-
161
133
{ test : / \. j s o n $ / , loader : 'json-loader' } ,
162
- {
163
- test : / \. ( j p g | p n g | g i f ) $ / ,
164
- loader : `url-loader?name=[name]${ hashFormat . file } .[ext]&limit=10000`
165
- } ,
166
134
{ test : / \. h t m l $ / , loader : 'raw-loader' } ,
167
-
135
+ { test : / \. ( e o t | s v g ) $ / , loader : `file-loader?name=[name] ${ hashFormat . file } .[ext]` } ,
168
136
{
169
- test : / \. ( o t f | t t f | w o f f | w o f f 2 ) $ / ,
137
+ 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 ) $ / ,
170
138
loader : `url-loader?name=[name]${ hashFormat . file } .[ext]&limit=10000`
171
- } ,
172
- { test : / \. ( e o t | s v g ) $ / , loader : `file-loader?name=[name]${ hashFormat . file } .[ext]` }
139
+ }
173
140
] . concat ( extraRules )
174
141
} ,
175
142
plugins : [
176
- new ExtractTextPlugin ( {
177
- filename : `[name]${ hashFormat . extract } .bundle.css` ,
178
- disable : ! extractCss
179
- } ) ,
180
143
new HtmlWebpackPlugin ( {
181
144
template : path . resolve ( appRoot , appConfig . index ) ,
182
145
filename : path . resolve ( appConfig . outDir , appConfig . index ) ,
@@ -190,18 +153,6 @@ export function getWebpackCommonConfig(
190
153
new webpack . optimize . CommonsChunkPlugin ( {
191
154
minChunks : Infinity ,
192
155
name : 'inline'
193
- } ) ,
194
- new webpack . LoaderOptionsPlugin ( {
195
- test : / \. ( c s s | s c s s | s a s s | l e s s | s t y l ) $ / ,
196
- options : {
197
- postcss : [ autoprefixer ( ) ] ,
198
- cssLoader : { sourceMap : sourcemap } ,
199
- sassLoader : { sourceMap : sourcemap } ,
200
- lessLoader : { sourceMap : sourcemap } ,
201
- stylusLoader : { sourceMap : sourcemap } ,
202
- // context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
203
- context : projectRoot ,
204
- } ,
205
156
} )
206
157
] . concat ( extraPlugins ) ,
207
158
node : {
0 commit comments