-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathbrowser.ts
More file actions
147 lines (135 loc) · 4.74 KB
/
browser.ts
File metadata and controls
147 lines (135 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.
import * as path from 'path';
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import { generateEntryPoints, packageChunkSort } from '../../utilities/package-chunk-sort';
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
import { IndexHtmlWebpackPlugin } from '../../plugins/index-html-webpack-plugin';
import { WebpackConfigOptions } from '../build-options';
import { normalizeExtraEntryPoints } from './utils';
/**
+ * license-webpack-plugin has a peer dependency on webpack-sources, list it in a comment to
+ * let the dependency validator know it is used.
+ *
+ * require('webpack-sources')
+ */
export function getBrowserConfig(wco: WebpackConfigOptions) {
const { root, projectRoot, buildOptions } = wco;
let extraPlugins: any[] = [];
// Figure out which are the lazy loaded bundle names.
const lazyChunkBundleNames = normalizeExtraEntryPoints(
// We don't really need a default name because we pre-filtered by lazy only entries.
[...buildOptions.styles, ...buildOptions.scripts], 'not-lazy')
.filter(entry => entry.lazy)
.map(entry => entry.bundleName)
const generateIndexHtml = false;
if (generateIndexHtml) {
extraPlugins.push(new HtmlWebpackPlugin({
template: path.resolve(root, buildOptions.index),
filename: path.resolve(buildOptions.outputPath, buildOptions.index),
chunksSortMode: packageChunkSort(buildOptions),
excludeChunks: lazyChunkBundleNames,
xhtml: true,
minify: buildOptions.optimization ? {
caseSensitive: true,
collapseWhitespace: true,
keepClosingSlash: true
} : false
}));
extraPlugins.push(new BaseHrefWebpackPlugin({
baseHref: buildOptions.baseHref as string
}));
}
let sourcemaps: string | false = false;
if (buildOptions.sourceMap) {
// See https://webpack.js.org/configuration/devtool/ for sourcemap types.
if (buildOptions.evalSourceMap && !buildOptions.optimization) {
// Produce eval sourcemaps for development with serve, which are faster.
sourcemaps = 'eval';
} else {
// Produce full separate sourcemaps for production.
sourcemaps = 'source-map';
}
}
if (buildOptions.subresourceIntegrity) {
extraPlugins.push(new SubresourceIntegrityPlugin({
hashFuncNames: ['sha384']
}));
}
if (buildOptions.extractLicenses) {
extraPlugins.push(new LicenseWebpackPlugin({
stats: {
warnings: false,
errors: false
},
perChunkOutput: false,
outputFilename: `3rdpartylicenses.txt`
}));
}
const globalStylesBundleNames = normalizeExtraEntryPoints(buildOptions.styles, 'styles')
.map(style => style.bundleName);
return {
devtool: sourcemaps,
resolve: {
mainFields: [
...(wco.supportES2015 ? ['es2015'] : []),
'browser', 'module', 'main'
]
},
output: {
crossOriginLoading: buildOptions.subresourceIntegrity ? 'anonymous' : false
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
maxAsyncRequests: Infinity,
cacheGroups: {
default: buildOptions.commonChunk && {
chunks: 'async',
minChunks: 2,
priority: 10,
},
common: buildOptions.commonChunk && {
name: 'common',
chunks: 'async',
minChunks: 2,
enforce: true,
priority: 5,
},
vendors: false,
vendor: buildOptions.vendorChunk && {
name: 'vendor',
chunks: 'initial',
enforce: true,
test: (module: any, chunks: Array<{ name: string }>) => {
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
return /[\\/]node_modules[\\/]/.test(moduleName)
&& !chunks.some(({ name }) => name === 'polyfills'
|| globalStylesBundleNames.includes(name));
},
},
}
}
},
plugins: extraPlugins.concat([
new IndexHtmlWebpackPlugin({
input: path.resolve(root, buildOptions.index),
output: path.basename(buildOptions.index),
baseHref: buildOptions.baseHref,
entrypoints: generateEntryPoints(buildOptions),
deployUrl: buildOptions.deployUrl,
sri: buildOptions.subresourceIntegrity,
}),
]),
node: false,
};
}