-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
193 lines (170 loc) · 4.81 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
'use strict';
const path = require('path');
const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const NODE_ENV = process.env.NODE_ENV;
const ENV_DEVELOPMENT = NODE_ENV === 'development';
const ENV_PRODUCTION = NODE_ENV === 'production';
const ENV_TEST = NODE_ENV === 'test';
const HOST = 'localhost';
const PORT = 4000;
//=========================================================
// LOADERS
//---------------------------------------------------------
const rules = {
componentStyles: {
test: /\.scss$/,
loader: 'raw!postcss!sass',
exclude: path.resolve('src/shared/styles')
},
sharedStyles: {
test: /\.scss$/,
loader: 'style!css!postcss!sass',
include: path.resolve('src/shared/styles')
},
typescript: {
test: /\.ts$/,
loader: 'ts',
exclude: /node_modules/
}
};
//=========================================================
// CONFIG
//---------------------------------------------------------
const config = module.exports = {};
config.resolve ={
extensions: ['.ts', '.js'],
mainFields: ['module', 'browser', 'main'],
modules: [
path.resolve('.'),
'node_modules'
]
};
config.module = {
rules: [
rules.typescript,
rules.componentStyles
]
};
config.plugins = [
new webpack.optimize.OccurrenceOrderPlugin(true),
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
}),
new LoaderOptionsPlugin({
debug: false,
minimize: ENV_PRODUCTION,
options: {
postcss: [
autoprefixer({ browser: ['last 3 versions'] })
],
resolve: {},
sassLoader: {
includePaths: ['src/shared'],
outputStyle: 'compressed',
precision: 10,
sourceComments: false
}
}
}),
new ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
path.resolve('src')
)
];
//=========================================================
// DEVELOPMENT or PRODUCTION
//---------------------------------------------------------
if (ENV_DEVELOPMENT || ENV_PRODUCTION) {
config.entry = {
main: './src/main.ts',
polyfills: './src/polyfills.ts'
};
config.output = {
path: path.resolve('./public'),
publicPath: '/'
};
config.plugins.push(
new CommonsChunkPlugin({
name: ['polyfills'],
minChunks: Infinity
}),
new HtmlWebpackPlugin({
chunkSortMode: 'dependency',
filename: 'index.html',
hash: false,
inject: 'body',
template: './src/index.html'
})
);
}
//=========================================================
// DEVELOPMENT
//---------------------------------------------------------
if (ENV_DEVELOPMENT) {
config.devtool = 'cheap-module-source-map';
config.output.filename = '[name].js';
config.module.rules.push(rules.sharedStyles);
config.plugins.push(new ProgressPlugin());
config.devServer = {
contentBase: './src',
historyApiFallback: true,
host: HOST,
port: PORT,
stats: {
cached: true,
cachedAssets: true,
chunks: true,
chunkModules: false,
colors: true,
hash: false,
reasons: true,
timings: true,
version: false
}
};
}
//=========================================================
// PRODUCTION
//---------------------------------------------------------
if (ENV_PRODUCTION) {
config.devtool = 'hidden-source-map';
config.output.filename = '[name].[chunkhash].js';
config.module.rules.push({
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css?-autoprefixer!postcss!sass'),
include: path.resolve('src/shared/styles')
});
config.plugins.push(
new WebpackMd5Hash(),
new ExtractTextPlugin('styles.[contenthash].css'),
new UglifyJsPlugin({
comments: false,
compress: {
dead_code: true, // eslint-disable-line camelcase
screw_ie8: true, // eslint-disable-line camelcase
unused: true,
warnings: false
},
mangle: {
screw_ie8: true // eslint-disable-line camelcase
}
})
);
}
//=========================================================
// TEST
//---------------------------------------------------------
if (ENV_TEST) {
config.devtool = 'inline-source-map';
config.module.rules.push(rules.sharedStyles);
}