-
Notifications
You must be signed in to change notification settings - Fork 433
/
webpack.config.js
216 lines (210 loc) · 6.73 KB
/
webpack.config.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Webpack (JavaScriptServices) with a few changes & updates
* - This is to keep us inline with JSServices, and help those using that template to add things from this one
*
* Things updated or changed:
* module -> rules []
* .ts$ test : Added 'angular2-router-loader' for lazy-loading in development
* added ...sharedModuleRules (for scss & font-awesome loaders)
*/
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
.BundleAnalyzerPlugin;
const TerserPlugin = require('terser-webpack-plugin');
const { sharedModuleRules } = require('./webpack.additions');
module.exports = env => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
mode: isDevBuild ? 'development' : 'production',
stats: {
modules: false
},
context: __dirname,
resolve: {
extensions: ['.js', '.ts']
},
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{
test: /^(?!.*\.spec\.ts$).*\.ts$/,
use: isDevBuild
? [
'awesome-typescript-loader?silent=true',
'angular2-template-loader',
'angular2-router-loader'
]
: '@ngtools/webpack'
},
{
test: /\.html$/,
use: 'html-loader?minimize=false'
},
{
test: /\.css$/,
use: [
'to-string-loader',
isDevBuild ? 'css-loader' : 'css-loader?minimize'
]
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
use: 'url-loader?limit=25000'
},
...sharedModuleRules
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: {
'main-client': './ClientApp/boot.browser.ts'
},
output: {
path: path.join(__dirname, clientBundleOutputDir)
},
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(
isDevBuild
? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(
clientBundleOutputDir,
'[resourcePath]'
) // Point sourcemap entries to the original file locations on disk
})
]
: [
// new BundleAnalyzerPlugin(),
// Plugins that apply in production builds only
new AngularCompilerPlugin({
mainPath: path.join(__dirname, 'ClientApp/boot.browser.ts'),
tsConfigPath: './ClientApp/tsconfig.app.json',
entryModule: path.join(
__dirname,
'ClientApp/app/app.module.browser#AppModule'
),
exclude: ['./**/*.server.ts'],
sourceMap: isDevBuild
})
]
),
devtool: isDevBuild ? 'cheap-eval-source-map' : false,
node: {
fs: 'empty'
},
optimization: {
minimizer: [].concat(
isDevBuild
? []
: [
// we specify a custom TerserPlugin here to get source maps in production
new TerserPlugin({
sourceMap: true,
terserOptions: {
compress: true,
ecma: 6,
mangle: true,
keep_classnames: true,
keep_fnames: true,
},
}),
]
)
}
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
// resolve: { mainFields: ['main'] },
entry: {
'main-server': isDevBuild
? './ClientApp/boot.server.ts'
: './ClientApp/boot.server.PRODUCTION.ts'
},
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(
isDevBuild
? [
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'),
{}
)
]
: [
// Plugins that apply in production builds only
new AngularCompilerPlugin({
mainPath: path.join(
__dirname,
'ClientApp/boot.server.PRODUCTION.ts'
),
tsConfigPath: './ClientApp/tsconfig.app.json',
entryModule: path.join(
__dirname,
'ClientApp/app/app.module.server#AppModule'
),
exclude: ['./**/*.browser.ts'],
sourceMap: isDevBuild
})
]
),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
// switch to "inline-source-map" if you want to debug the TS during SSR
devtool: isDevBuild ? 'cheap-eval-source-map' : false,
optimization: {
minimizer: [].concat(
isDevBuild
? []
: [
// we specify a custom TerserPlugin here to get source maps in production
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
compress: false,
ecma: 6,
mangle: true,
keep_classnames: true,
keep_fnames: true,
},
})
]
)
}
});
return [clientBundleConfig, serverBundleConfig];
};