-
Notifications
You must be signed in to change notification settings - Fork 26
/
webpack.config.js
150 lines (134 loc) · 3.87 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
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const CleanPlugin = require('clean-webpack-plugin')
/* allow us to visualise our bundles and optimise
const Visualizer = require('webpack-visualizer-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
*/
const env = require('./.setup/client/env')
const clientDir = path.join(__dirname, 'client')
const config = {
context: clientDir,
entry: {
main: './index.js',
vendor: [
// vendors added here will be included in vendor chunk
// so they only get requested once and can easily be cached
'react',
'react-dom',
'react-router-dom',
'redux',
'react-redux',
'redux-form',
'prop-types',
'lodash',
'material-ui'
]
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'initial',
cacheGroups: {
default: false,
vendors: false
}
}
},
output: {
path: path.join(__dirname, 'server', 'public'),
publicPath: '/',
filename: '[name].[chunkhash].js'
// adding the chunk hash helps with caching, easier to tell when file has changed
// but make sure to delete contents of output folder each time before running yarn build
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader?sourceMap',
use: 'css-loader?modules&importLoaders=1&localIdentName="[local]__[hash:base64:5]"'
})
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader?sourceMap',
// resolve-url-loader may be chained before sass-loader if necessary
use: [
'css-loader?modules&importLoaders=1&localIdentName="[local]__[hash:base64:5]"',
'sass-loader?sourceMap'
]
})
},
{
test: /\.(png|jpg|gif|svg)$/,
use: {
loader: 'url-loader',
options: {
limit: 8192
}
}
}
]
},
plugins: [
new CleanPlugin(['server/public/'], {root: __dirname}),
new HtmlWebpackPlugin({
inject: true,
template: path.join(clientDir, 'index.html'),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
new ExtractTextPlugin({
filename: '[name].css'
}),
new webpack.DefinePlugin({
$_ENV: JSON.stringify(env)
}),
new CopyPlugin([
{from: path.join(clientDir, 'robots.txt')}
]),
// ensures vendor bundle hash only changes when it needs to
new webpack.HashedModuleIdsPlugin() /*,
new Visualizer()
*/
/*
turn on to create a visualisation of module sizings with each build
will be output as stats.html in the output directory after running yarn build
*/
/*,
new BundleAnalyzerPlugin()
*/
/*
turn on to create an interactive treemap visualization of the contents of all our bundles
will open automatically after running yarn build
these plugins are going to really help us as the app grows
and we split it into multiple chunks
to make sure common components and vendor packages
aren't being requested multiple times without need
*/
],
devtool: 'source-map'
}
module.exports = config