-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
173 lines (160 loc) · 4.5 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
const { resolve } = require('path');
const webpack = require('webpack');
const { CommonsChunkPlugin/*, UglifyJsPlugin*/ } = webpack.optimize;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// webpack config helpers
const { getIfUtils, removeEmpty } = require('webpack-config-utils');
module.exports = (env) => {
const { ifProd, ifNotProd, ifTest, ifNotTest } = getIfUtils(env);
return {
context: resolve(__dirname, 'src'),
entry: {
polyfills: './polyfills.ts',
main: './main.ts',
},
output: {
filename: '[name].[hash].js',
path: resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.js', '.ts', '.tsx']
},
devtool: 'source-map',
performance: {
hints: ifProd() && 'warning'
},
devServer: {
stats: 'minimal',
},
module: {
rules: [
// addition - add source-map support
{
enforce: "pre",
test: /\.jsx?$/,
loader: "source-map-loader",
exclude: /src/
},
// Typescript
{
test: /\.tsx?$/,
include: /src/,
use: [{
loader: 'awesome-typescript-loader',
options: {
silent: ifProd(),
}
}]
},
// CSS
{
test: /\.css$/,
include: /node_modules/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { sourceMap: true }
}
]
},
// global app styles
{
test: /styles\.css$/,
include: /src/,
use: ifNotProd(
[
{ loader: "style-loader" },
{ loader: "css-loader" },
],
ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
)
},
// ShadowDom inline string styles
{
resource: {
test: /\.css$/,
include: /src/,
not: [resolve(__dirname, 'src/styles.css')]
},
use: [
{ loader: 'to-string-loader' },
{ loader: 'css-loader' },
]
},
]
},
plugins: removeEmpty([
new CopyWebpackPlugin([
// here we are just copying es5shim adapter from node_modules if you wanna use it directly in HTML - NOT RECOMMENDED
{ from: resolve(__dirname, 'node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js' ), to: 'wc'},
]),
// Set NODE_ENV to enable production react version
new webpack.DefinePlugin({
'process.env': { NODE_ENV: ifProd('"production"', '"development"') }
}),
new ProgressBarPlugin(),
ifProd(
new ExtractTextPlugin('[name].css')
),
new CommonsChunkPlugin({
name: 'polyfills',
chunks: ['polyfills']
}),
// This enables tree shaking of the vendor modules
new CommonsChunkPlugin({
name: 'vendor',
chunks: ['main'],
minChunks: (module, count) => /node_modules\//.test(module.resource)
}),
// Specify the correct order the scripts will be injected in
new CommonsChunkPlugin({
name: ['vendor', 'polyfills']
}),
// prints more readable module names in the browser console on HMR updates
ifNotProd(
new webpack.NamedModulesPlugin()
),
ifProd(
new UglifyJsPlugin({
sourceMap: true,
compress: {
screw_ie8: true,
warnings: false
},
output: { comments: false }
})
),
new webpack.LoaderOptionsPlugin(
ifProd({
minimize: true,
debug: false
})
),
new HtmlWebpackPlugin({
template: resolve('src', 'index.html'),
// https://github.com/kangax/html-minifier#options-quick-reference
// will minify html
minify: ifProd(
{
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
keepClosingSlash: true,
minifyURLs: true
},
false
)
}),
])
}
}