This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
webpack.config.ts
179 lines (154 loc) · 3.72 KB
/
webpack.config.ts
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
import 'core-js/es6';
import 'core-js/es7/reflect';
import 'ts-helpers';
// needed to create context for resolveNgRoute
/**
* @author: @AngularClass
*/
const {
ContextReplacementPlugin,
HotModuleReplacementPlugin,
DefinePlugin,
ProgressPlugin,
optimize: {
CommonsChunkPlugin,
DedupePlugin
}
} = require('webpack');
const {ForkCheckerPlugin} = require('awesome-typescript-loader');
const resolveNgRoute = require('@angularclass/resolve-angular-routes')
const path = require('path');
function root(__path = '.') {
return path.join(__dirname, __path);
}
// type definition for WebpackConfig at the bottom
function webpackConfig(options: EnvOptions = {}): WebpackConfig {
const CONSTANTS = {
ENV: JSON.stringify(options.ENV),
HMR: options.HMR,
PORT: 3000,
HOST: 'localhost'
};
return {
cache: true,
// devtool: 'hidden-source-map',
devtool: 'source-map',
// devtool: 'cheap-module-eval-source-map',
entry: {
polyfills: './src/polyfills.browser',
vendor: './src/vendor.browser',
main: './src/main.browser'
},
output: {
path: root('dist'),
filename: '[name].bundle.js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js'
},
module: {
preLoaders: [
// fix angular2
{
test: /systemjs_component_resolver\.js$/,
loader: 'string-replace-loader',
query: {
search: 'lang_1\\.global.*[\\n\\r]\\s*\\.System.import',
replace: 'System.import',
flags: 'g'
}
}
],
// End AngularSystemJS
loaders: [
// Support for .ts files.
{
test: /\.ts$/,
loaders: [
'awesome-typescript-loader',
'@angularclass/conventions-loader'
],
exclude: [/\.(spec|e2e|d)\.ts$/]
},
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.html/, loader: 'raw-loader' },
{ test: /\.css$/, loader: 'raw-loader' },
]
},
plugins: [
new ContextReplacementPlugin(
/angular\/core\/src\/linker/,
root('./src'),
resolveNgRoute(root('./src'))
),
new HotModuleReplacementPlugin(),
new ForkCheckerPlugin(),
new CommonsChunkPlugin({ name: ['main', 'vendor', 'polyfills'], minChunks: Infinity }),
new DefinePlugin(CONSTANTS),
new ProgressPlugin({})
],
resolve: {
extensions: ['', '.ts', '.js', '.json'],
},
devServer: {
contentBase: './src',
port: CONSTANTS.PORT,
hot: CONSTANTS.HMR,
inline: CONSTANTS.HMR,
historyApiFallback: true
},
node: {
global: 'window',
process: true,
Buffer: false,
crypto: 'empty',
module: false,
clearImmediate: false,
setImmediate: false,
clearTimeout: true,
setTimeout: true
}
};
}
// Export
module.exports = webpackConfig;
// Types
type Entry = Array<string> | Object;
type Output = Array<string> | {
path: string,
filename: string
};
type EnvOptions = any;
interface WebpackConfig {
cache?: boolean;
target?: string;
devtool?: string;
entry: Entry;
output: any;
module?: any;
// module?: {
// loaders?: Array<any>
// };
plugins?: Array<any>;
resolve?: {
root?: string;
extensions?: Array<string>;
};
devServer?: {
contentBase?: string;
port?: number;
historyApiFallback?: boolean;
hot?: boolean;
inline?: boolean;
};
node?: {
process?: boolean;
global?: boolean | string;
Buffer?: boolean;
crypto?: string | boolean;
module?: boolean;
clearImmediate?: boolean;
setImmediate?: boolean
clearTimeout?: boolean;
setTimeout?: boolean
};
}