forked from TrilonIO/aspnetcore-angular-universal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
196 lines (163 loc) · 6.74 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
// Webpack file
// Author : Mark Pieszak
var webpack = require('webpack');
var path = require('path');
var clone = require('js.clone');
// TS/Webpack things to enable paths:[] from tsconfig
// This helps Webpack understand what they are, and where they are coming from so it can "map" them
// & create the modules correctly
var ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
var TsConfigPathsPlugin = require('awesome-typescript-loader').TsConfigPathsPlugin;
var ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
var DefinePlugin = require('webpack/lib/DefinePlugin');
// Test if Development build from ASPNETCore environment
var isDevBuild = process.env.ASPNETCORE_ENVIRONMENT === 'Production' ? false : true;
// Sourcemaps (for Development only)
var devTool = isDevBuild ? 'source-map' : '';
// Webpack configuration Export
// Here we wrap the configuration around setTypeScriptAlias so that it can properly map our "paths: []" from tsconfig
// to the correct corresponding module resource locations
module.exports = setTypeScriptAlias(require('./tsconfig.json'), {
devtool: devTool,
// What types of files should webpack handle
resolve: {
extensions: ['.js', '.ts']
},
// What are our "entry" points for webpack to create "Chunks"
entry: {
main: ['./Client/bootstrap-client.ts']
},
// Where should webpack put files after they're processed
output: {
// Path location
path: path.join(__dirname, 'wwwroot', 'dist'),
// This is a dynamic way to handle multiple entry[] files [name] in our case would be the Key for each one
// ex: main.js | vendor.js | etc
filename: '[name].js',
// public location, (you could have them pointing to external CDNs here for example)
publicPath: '/dist/'
},
// Here we tell Webpack how to handle Modules
module: {
// I wish this was just called "Processors" instead of Loaders
// Think of this as, test the regex against each file webpack runs into, if it matches that type,
// Use the following "Loaders" or processors, to -handle- that file
// (whether it be to compile it, parse it, or run some other magic on it)
loaders: [
{ // TypeScript files
test: /\.ts$/,
exclude: [/\.(spec|e2e)\.ts$/], // Exclude test files | end2end test spec files etc
loaders: [
'awesome-typescript-loader', // Amazing TS loader
'angular2-template-loader' // Handles templateUrl stylesUrl and automatically just inserts them into the template|styles
// instead of having the module resource loader handle it
]
},
// Html files
{ test: /\.html$/, loader: 'raw-loader' },
// CSS files
{ test: /\.css/, loader: 'raw-loader' },
// SASS files
//{ test: /\.scss$/, loaders: ['raw-loader', 'sass-loader?sourceMap'] },
// JSON files
{ test: /\.json$/, loader: 'json-loader' },
// Font files of all types
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
}]
},
// Our webpack-dev-server (used for HotModuleReplacement, and in general to serve up our files when we're in the middle
// of developing our Application
//devServer: {
// port: 9999,
// // host: METADATA.host,
// historyApiFallback: true,
// watchOptions: {
// aggregateTimeout: 300,
// poll: 1000
// },
// outputPath: root('/wwwroot/dist')
//},
// Plugins are middleware we can use during webpacks processing cycles to handle other things we want to do
// Here we need to fix some Angular2 issues
// Handle TsConfig paths:[] so webpack is aware of them and it's actually able to process them correctly
// Also we need to let our development webpack file be aware of our separate VENDOR webpack file
// (the vendor file has all the 3rd party things we need to get our project going)
plugins: [
new DefinePlugin({
'process.env': {
'development': true
}
}),
new ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
root('./Client')
),
new TsConfigPathsPlugin({
tsconfig: 'tsconfig.json'
}),
new ForkCheckerPlugin(),
new webpack.DllReferencePlugin({
context: '.',
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new DefinePlugin({
'process.env': {
'production': true
}
}),
// problem with platformUniversalDynamic on the server/client
new webpack.NormalModuleReplacementPlugin(
/@angular(\\|\/)compiler/,
root('empty.js')
),
//new webpack.optimize.OccurrenceOrderPlugin(), <-- not needed in webpack2 anymore
new webpack.optimize.UglifyJsPlugin()
]),
node: {
global: true,
crypto: 'empty',
process: true,
module: false,
clearImmediate: false,
setImmediate: false
}
});
////////////////////////////////////////////////
// Internal methods
//////////////////
function setTypeScriptAlias(tsConfig, config) {
var newConfig = clone(config);
newConfig = newConfig || {};
newConfig.resolve = newConfig.resolve || {};
newConfig.resolve.alias = newConfig.resolve.alias || {};
var tsPaths = tsConfig.compilerOptions.paths;
for (var prop in tsPaths) {
newConfig.resolve.alias[prop] = root(tsPaths[prop][0]);
}
return newConfig;
}
function plugins(plugins, config) {
config.plugins = config.plugins.concat(plugins);
return config
}
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}