/
client.webpack.config.js
275 lines (243 loc) · 7.64 KB
/
client.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict' // eslint-disable-line strict
const path = require('path')
const webpack = require('webpack')
const cssnano = require('cssnano')
const log = require('debug')('app:config:webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const clientConfig = require('./client.config')
const pkg = require('../package.json')
const __PROD__ = process.env.NODE_ENV === 'production'
const __DEV__ = process.env.NODE_ENV === 'development'
const vendor = [
'apollo-client',
'react',
'react-apollo',
'react-dom',
'react-redux',
'react-router',
'redux',
'redux-thunk'
]
vendor.forEach((name) => {
if (pkg.dependencies[name]) return true
log(`Vendor "${name}" was not found in package.json. It will not be included in the bundle`)
})
const config = {
// The base directory, an absolute path, for resolving entry points and loaders from configuration.
// `entry` and `module.rules.loader` options are resolved relative to this directory
context: path.resolve(__dirname, './'),
// The point or points to enter the application. At this point, the application starts executing.
// If an array is passed, all items will be executed. Simple rule: one entry point per HTML page.
// SPA: one entry point. MPA: multiple entry points https://webpack.js.org/configuration/entry-context
entry: entry(),
// Options related to how webpack emits results
output: output(),
// enhance debugging by adding meta info for the browser devtools: 'source-map' | 'eval'
devtool: devtool(),
// options for resolving module requests (does not apply to resolving to loaders)
resolve: resolve(),
plugins: plugins(),
// The configuration regarding modules
module: modules(),
// the environment in which the bundle should run
// changes chunk loading behavior and available modules: web | node | webworker etc...
target: 'web',
name: 'client'
}
function entry() {
let entry = { app: null, vendor }
let app = [path.resolve(clientConfig.paths.client, './index')]
if (!__PROD__) app.unshift('webpack-hot-middleware/client?reload=true')
entry.app = app
return entry
}
function output() {
return {
// the target directory for all output files must be an absolute path
// path: path.join(__dirname, '/dist', '/assets'),
path: clientConfig.paths.dist,
// the filename template for entry chunks: "bundle.js" "[name].js" "[chunkhash].js"
filename: '[name].[hash].js',
// the url to the output directory resolved relative to the HTML page
// uncomment this if you're not using docker
// publicPath: __PROD__ ? '/' : `http://${clientConfig.devServer.host}:${clientConfig.devServer.client_port}/`
publicPath: '/',
}
}
function devtool() {
return __PROD__ ? 'source-map' : 'eval-source-map'
}
function plugins() {
log('Enable common plugins: Define, LoaderOptions, HtmlWebpack, CommonsChunk')
const common = [
// map variables
new webpack.DefinePlugin(clientConfig.globals),
new webpack.LoaderOptionsPlugin({
options: {
context: __dirname,
postcss: [
cssnano({
autoprefixer: {
add: true,
remove: true,
browsers: ['last 2 versions']
},
discardComments: {
removeAll: true
},
discardUnused: false,
mergeIdents: false,
reduceIdents: false,
safe: true,
sourcemap: true
})
],
sassLoader: {
includePaths: path.resolve(clientConfig.paths.client, './styles')
}
}
}),
// parse html
new HtmlWebpackPlugin({
template: path.resolve(clientConfig.paths.public, './index.html'),
hash: false,
favicon: path.resolve(clientConfig.paths.public, './favicon.ico'),
filename: 'index.html',
inject: 'body',
minify: { collapseWhitespace: true }
}),
// split bundles
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor']
})
]
if (__PROD__) {
log('Enable plugins for production: OccurrenceOrder, UglifyJs')
return common.concat([
// optimize order of modules based on how often it is used
new webpack.optimize.OccurrenceOrderPlugin(),
// uglify and minify javascript files
new webpack.optimize.UglifyJsPlugin({
compress: {
unused: true,
dead_code: true,
warnings: false
}
})
])
} else {
log('Enable plugins for development: HotModuleReplacement, NamedModules, NoErrors')
return common.concat([
// enable HMR globally
new webpack.HotModuleReplacementPlugin(),
// prints more readable module names in the browser console on HMR updates
new webpack.NamedModulesPlugin(),
new webpack.NoErrorsPlugin()
])
}
}
function resolve() {
return {
modules: [
'node_modules',
clientConfig.paths.client
],
extensions: ['.js', '.jsx', '.json']
}
}
function modules() {
// cssnano minimizes already so disable minimize in cssLoader
const cssLoader = 'css-loader?modules&sourceMap&-minimizeimportLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
return {
rules: [
{
test: /\.json$/,
use: [
{ loader: 'json-loader' }
]
},
{
test: /\.(js|jsx)$/,
include: [
clientConfig.paths.client
],
exclude: [
/node_modules/,
],
use: __PROD__ ? [{ loader: 'babel-loader' }]
: [
{ loader: 'react-hot-loader' },
{ loader: 'babel-loader' }
]
},
{
test: /\.css$/,
loaders: [
'style-loader',
cssLoader,
'postcss-loader',
]
},
{
test: /\.scss$/,
loaders: [
'style-loader',
cssLoader,
'postcss-loader',
'sass-loader?sourceMap'
]
},
{
test: /\.woff(\?.*)?$/,
use: [{ loader: 'url-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff' }]
},
{
test: /\.woff2(\?.*)?$/,
use: [{ loader: 'url-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff2' }]
},
{
test: /\.otf(\?.*)?$/,
use: [{ loader: 'file-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=font/opentype' }]
},
{
test: /\.ttf(\?.*)?$/,
use: [{ loader: 'url-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/octet-stream' }]
},
{
test: /\.eot(\?.*)?$/,
use: [{ loader: 'file-loader?prefix=fonts/&name=[path][name].[ext]' }]
},
{
test: /\.svg(\?.*)?$/,
use: [{ loader: 'url-loader?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=image/svg+xml' }]
},
{
test: /\.(png|jpg)$/,
use: [{ loader: 'url-loader?limit=8192' }]
}
],
}
}
if (!__DEV__) {
log('Adding ExtractTextPlugin to CSS loaders.')
// extract all text from css
config.module.rules.filter((rules) => {
return rules.loaders && rules.loaders.find((name) => {
return /css/.test(name.split('?')[0])
})
}).forEach((loader) => {
const first = loader.loaders[0]
const rest = loader.loaders.slice(1)
loader.loader = ExtractTextPlugin.extract({
fallbackLoader: first,
loader: rest.join('!')
})
delete loader.loaders
})
// place all extracted text into a file
config.plugins.push(
new ExtractTextPlugin({ filename: '[name].[contenthash].css', allChunks: true })
)
}
module.exports = config