/
webpack.config.ts
92 lines (90 loc) · 2.59 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
import { resolve } from 'path';
import {
Configuration,
optimize,
HotModuleReplacementPlugin,
NamedModulesPlugin,
Entry,
DefinePlugin,
Plugin,
LoaderOptionsPlugin,
Rule,
} from 'webpack';
import * as ExtractTextPlugin from 'extract-text-webpack-plugin';
export default (env: string): Configuration => {
// add hot module replacement if not in production
let entry: Entry = {
main: './src/index.tsx',
vendor: [
'react',
'react-dom',
'react-router-dom',
'react-router-config',
'redux',
'react-helmet',
'react-redux',
'serialize-javascript',
],
};
entry = env !== 'production' ? {
hot: ['react-hot-loader/patch', 'webpack-hot-middleware/client'],
...entry,
} : entry;
// set devtool according to the environment
const devtool: 'source-map' | 'eval-source-map' = env === 'production' ? 'source-map' : 'eval-source-map';
let plugins: Plugin[] = [new optimize.CommonsChunkPlugin({
names: ['vendor', 'common'],
}), new ExtractTextPlugin('styles.css')];
// set plugins hot module replacement plugins if not in production
plugins = env === 'production' ? [
...plugins,
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
new LoaderOptionsPlugin({
minimize: true,
debug: false,
}),
new optimize.UglifyJsPlugin({
compress: {
screw_ie8: true,
warnings: false,
},
sourceMap: true,
}),
] : [
...plugins,
new HotModuleReplacementPlugin(),
new NamedModulesPlugin(),
];
const cssRule: Rule = env === 'production' ? {
test: /\.css$/,
use: ExtractTextPlugin.extract(['css-loader', 'postcss-loader']),
} : {
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
};
return {
entry,
output: {
filename: '[name].js',
path: resolve(__dirname, 'dist', 'static'),
publicPath: '/',
},
devtool,
resolve: {
extensions: ['.tsx', '.ts', '.js', '.css'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: ['awesome-typescript-loader'],
exclude: /node_modules/,
},
cssRule,
],
},
plugins,
};
};