forked from vsternbach/angularjs-typescript-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·131 lines (123 loc) · 3.21 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
const webpack = require('webpack');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const path = require('path');
const sourcePath = path.resolve(__dirname, 'src');
const distPath = path.resolve(__dirname, 'dist');
module.exports = (env, argv) => {
const isProd = argv.mode === 'production';
const plugins = [
new HtmlWebPackPlugin({
template: sourcePath + '/index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:4].css',
chunkFilename: '[id].[contenthash:4].css'
}),
new ForkTsCheckerWebpackPlugin({
tslint: true,
checkSyntacticErrors: true
})
];
if (isProd) {
plugins.push(
new webpack.NormalModuleReplacementPlugin(
/\/environments\/environment\.ts/, `${sourcePath}/environments/environment.prod.ts`
),
new UglifyJsPlugin({ sourceMap: true })
);
} else {
plugins.push(new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin());
}
const config = {
entry: {
app: sourcePath + '/main.ts',
},
output: {
path: distPath,
filename: '[name].bundle.[hash:4].js',
},
module: {
rules: [
{
test: /\.html$/,
loader: 'html-loader',
options: { minimize: true }
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'resolve-url-loader']
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'resolve-url-loader', 'sass-loader']
},
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: 'ng-annotate-loader',
options: {
ngAnnotate: 'ng-annotate-patched',
sourcemap: !isProd,
},
},
{
loader: 'ts-loader',
options: {
configFile: sourcePath + '/tsconfig.app.json',
// disable type checker - we will use it in fork plugin
transpileOnly: true,
}
}
]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'file-loader',
options: {
name: 'images/[name].[ext]'
}
},
{
test: /\.(eot|ttf|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]'
}
}
],
},
resolve: {
extensions: ['.js', '.ts'],
modules: [
path.resolve(__dirname, 'node_modules'),
sourcePath
]
},
plugins,
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
// devtool: 'eval-source-map',
devServer: {
contentBase: distPath,
hot: true
}
};
if (!isProd) {
config.devtool = 'source-map';
}
return config;
};