@@ -18,6 +18,7 @@ import {
18
18
Configuration ,
19
19
ContextReplacementPlugin ,
20
20
HashedModuleIdsPlugin ,
21
+ compilation ,
21
22
debug ,
22
23
} from 'webpack' ;
23
24
import { RawSource } from 'webpack-sources' ;
@@ -146,34 +147,34 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
146
147
const hashFormat = getOutputHashFormat ( buildOptions . outputHashing || 'none' ) ;
147
148
148
149
// process global scripts
149
- if ( buildOptions . scripts . length > 0 ) {
150
- const globalScriptsByBundleName = normalizeExtraEntryPoints (
151
- buildOptions . scripts ,
152
- 'scripts' ,
153
- ) . reduce ( ( prev : { bundleName : string ; paths : string [ ] ; inject : boolean } [ ] , curr ) => {
154
- const bundleName = curr . bundleName ;
155
- const resolvedPath = path . resolve ( root , curr . input ) ;
156
- const existingEntry = prev . find ( el => el . bundleName === bundleName ) ;
157
- if ( existingEntry ) {
158
- if ( existingEntry . inject && ! curr . inject ) {
159
- // All entries have to be lazy for the bundle to be lazy.
160
- throw new Error (
161
- `The ${ curr . bundleName } bundle is mixing injected and non-injected scripts.` ,
162
- ) ;
163
- }
164
-
165
- existingEntry . paths . push ( resolvedPath ) ;
166
- } else {
167
- prev . push ( {
168
- bundleName,
169
- paths : [ resolvedPath ] ,
170
- inject : curr . inject ,
171
- } ) ;
150
+ const globalScriptsByBundleName = normalizeExtraEntryPoints (
151
+ buildOptions . scripts ,
152
+ 'scripts' ,
153
+ ) . reduce ( ( prev : { bundleName : string ; paths : string [ ] ; inject : boolean } [ ] , curr ) => {
154
+ const bundleName = curr . bundleName ;
155
+ const resolvedPath = path . resolve ( root , curr . input ) ;
156
+ const existingEntry = prev . find ( el => el . bundleName === bundleName ) ;
157
+ if ( existingEntry ) {
158
+ if ( existingEntry . inject && ! curr . inject ) {
159
+ // All entries have to be lazy for the bundle to be lazy.
160
+ throw new Error (
161
+ `The ${ curr . bundleName } bundle is mixing injected and non-injected scripts.` ,
162
+ ) ;
172
163
}
173
164
174
- return prev ;
175
- } , [ ] ) ;
165
+ existingEntry . paths . push ( resolvedPath ) ;
166
+ } else {
167
+ prev . push ( {
168
+ bundleName,
169
+ paths : [ resolvedPath ] ,
170
+ inject : curr . inject ,
171
+ } ) ;
172
+ }
173
+
174
+ return prev ;
175
+ } , [ ] ) ;
176
176
177
+ if ( globalScriptsByBundleName . length > 0 ) {
177
178
// Add a new asset for each entry.
178
179
globalScriptsByBundleName . forEach ( script => {
179
180
// Lazy scripts don't get a hash, otherwise they can't be loaded by name.
@@ -321,8 +322,8 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
321
322
322
323
if ( buildOptions . aot ) {
323
324
// Also try to load AOT-only global definitions.
324
- const GLOBAL_DEFS_FOR_TERSER_WITH_AOT =
325
- require ( '@angular/compiler-cli' ) . GLOBAL_DEFS_FOR_TERSER_WITH_AOT ;
325
+ const GLOBAL_DEFS_FOR_TERSER_WITH_AOT = require ( '@angular/compiler-cli' )
326
+ . GLOBAL_DEFS_FOR_TERSER_WITH_AOT ;
326
327
if ( GLOBAL_DEFS_FOR_TERSER_WITH_AOT ) {
327
328
angularGlobalDefinitions = {
328
329
...angularGlobalDefinitions ,
@@ -332,17 +333,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
332
333
}
333
334
334
335
const terserOptions = {
335
- // Use 5 if using bundle downleveling to ensure script bundles do not use ES2015+ features
336
- // Script bundles are shared for differential loading
337
- // Bundle processing will use the ES2015+ optimizations on the ES2015 bundles
338
- ecma :
339
- wco . supportES2015 &&
340
- ( ! differentialLoadingNeeded || ( differentialLoadingNeeded && fullDifferential ) )
341
- ? 6
342
- : 5 ,
343
336
warnings : ! ! buildOptions . verbose ,
344
337
safari10 : true ,
345
338
output : {
339
+ ecma : wco . supportES2015 ? 6 : 5 ,
346
340
comments : false ,
347
341
webkit : true ,
348
342
} ,
@@ -351,10 +345,12 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
351
345
compress :
352
346
buildOptions . platform == 'server'
353
347
? {
348
+ ecma : wco . supportES2015 ? 6 : 5 ,
354
349
global_defs : angularGlobalDefinitions ,
355
350
keep_fnames : true ,
356
351
}
357
352
: {
353
+ ecma : wco . supportES2015 ? 6 : 5 ,
358
354
pure_getters : buildOptions . buildOptimizer ,
359
355
// PURE comments work best with 3 passes.
360
356
// See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.
@@ -375,8 +371,32 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
375
371
parallel : true ,
376
372
cache : true ,
377
373
extractComments : false ,
374
+ chunkFilter : ( chunk : compilation . Chunk ) =>
375
+ ! globalScriptsByBundleName . some ( s => s . bundleName === chunk . name ) ,
378
376
terserOptions,
379
377
} ) ,
378
+ // Script bundles are fully optimized here in one step since they are never downleveled.
379
+ // They are shared between ES2015 & ES5 outputs so must support ES5.
380
+ new TerserPlugin ( {
381
+ sourceMap : scriptsSourceMap ,
382
+ parallel : true ,
383
+ cache : true ,
384
+ extractComments : false ,
385
+ chunkFilter : ( chunk : compilation . Chunk ) =>
386
+ globalScriptsByBundleName . some ( s => s . bundleName === chunk . name ) ,
387
+ terserOptions : {
388
+ ...terserOptions ,
389
+ compress : {
390
+ ...terserOptions . compress ,
391
+ ecma : 5 ,
392
+ } ,
393
+ output : {
394
+ ...terserOptions . output ,
395
+ ecma : 5 ,
396
+ } ,
397
+ mangle : ! manglingDisabled && buildOptions . platform !== 'server' ,
398
+ } ,
399
+ } ) ,
380
400
) ;
381
401
}
382
402
0 commit comments