-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
138 lines (130 loc) · 3.88 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
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const { PurifyPlugin } = require('@angular-devkit/build-optimizer')
const { AngularCompilerPlugin } = require('@ngtools/webpack');
const path = require('path');
const root = (...dir) => path.resolve(__dirname, ...dir);
const dist = root('dist');
const nodeModules = root('node_modules');
const styles = root('src', 'styles.css');
const buildOptimizer = {
loader: "@angular-devkit/build-optimizer/webpack-loader",
options: {
sourceMap: false
}
};
module.exports = function (env = {}) {
const isProd = !!env.prod;
const aot = !!env.aot;
let config = {
entry: {
styles: [
'./src/styles.css'
],
vendor: [
'core-js/es6',
'core-js/es7/reflect',
'zone.js/dist/zone'
],
main: [
'./src/main.ts'
]
},
output: {
path: dist,
filename: isProd ? '[name].[chunkhash:7].bundle.js' : '[name].bundle.js',
sourceMapFilename: isProd ? undefined : '[name].map',
chunkFilename: isProd ? '[id].[chunkhash:7].chunk.js' : '[id].chunk.js'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
// don't touch, must be on first position in array
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
use: ['@ngtools/webpack'],
exclude: [/\.(spec|e2e)\.ts$/]
},
{ test: /\.html$/, use: ['raw-loader'] },
{ test: /\.css$/, use: ['raw-loader', 'postcss-loader'], exclude: [styles] },
{ test: /\.less$/, use: ['raw-loader', 'postcss-loader', 'less-loader'] },
{ test: /\.scss$/, use: ['raw-loader', 'postcss-loader', 'sass-loader'] }
]
},
plugins: [
new webpack.LoaderOptionsPlugin({
debug: !isProd,
minimize: isProd,
options: {
context: __dirname,
}
}),
new webpack.DefinePlugin({
IS_PROD: JSON.stringify(isProd)
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'inline',
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
chunks: ['main'],
minChunks: (module) => module.userRequest && module.userRequest.startsWith(nodeModules)
}),
new AngularCompilerPlugin({
tsConfigPath: './src/tsconfig.app.json',
mainPath: './main.ts',
skipCodeGeneration: !aot
}),
new HtmlWebpackPlugin({
template: './src/index.html',
chunks: [
'inline',
'styles',
'vendor',
'main'
],
chunksSortMode: 'manual'
})
],
devtool: !isProd ? 'source-map' : undefined,
stats: {
children: false,
modules: false
},
devServer: {
port: 9000,
contentBase: './src',
historyApiFallback: true
}
};
if (isProd) {
config.module.rules.push({
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?importLoaders=1', 'postcss-loader']
}),
include: [styles]
});
config.module.rules[0].use.splice(0, 0, buildOptimizer);
config.module.rules.push(buildOptimizer);
config.plugins.push(new PurifyPlugin());
config.plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
config.plugins.push(new ExtractTextPlugin('[name].[chunkhash:7].css'));
config.plugins.push(new UglifyJSPlugin());
} else {
config.entry.vendor.push('zone.js/dist/long-stack-trace-zone');
config.module.rules.push({
test: /\.css$/,
use: ['style-loader', 'css-loader?importLoaders=1', 'postcss-loader'],
include: [styles]
});
}
return config;
};