This repository was archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebpack.config.js
executable file
·161 lines (137 loc) · 4.96 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
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SimpleProgressPlugin = require('webpack-simple-progress-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const RobotstxtPlugin = require('robotstxt-webpack-plugin').default;
const pkg = require('./package.json');
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || 'localhost';
const NODE_ENV = process.env.NODE_ENV || 'production';
const isProduction = (NODE_ENV === 'production');
const isDevelopment = (NODE_ENV === 'development');
const webpackConfig = {
entry: isDevelopment
? [
'babel-polyfill',
'react-hot-loader/patch', // activate HMR for React
`webpack-hot-middleware/client?path=http://${HOST}:${PORT}/__webpack_hmr`, // bundle the client for webpack-hot-middleware and connect to the provided endpoint
'./src/client.jsx',
]
: [
'babel-polyfill',
'./src/client.jsx',
],
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
output: {
path: path.join(__dirname, 'dist/public/'),
chunkFilename: 'assets/scripts/[name].[chunkhash].js',
filename: isDevelopment
? 'main.js'
: 'assets/scripts/[name].[chunkhash].js',
},
module: {
rules: [
{
test: /\.s?css$/,
use: ['css-hot-loader'].concat(
ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: isProduction,
sourceMap: !isProduction
},
},
{
loader: 'sass-loader',
options: {
sourceMap: !isProduction
}
},
],
})
),
},
{
test: /\.jsx?$/,
use: ['babel-loader'],
include: path.join(__dirname, 'src'),
},
],
},
plugins: [
new SimpleProgressPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
isDevelopment
? null
: new webpack.optimize.ModuleConcatenationPlugin(),
isDevelopment
? new webpack.HotModuleReplacementPlugin() // enable HMR globally
: null,
isDevelopment
? new webpack.NamedModulesPlugin() // prints more readable module names in the browser console on HMR updates
: null,
isDevelopment
? new webpack.NoEmitOnErrorsPlugin() // do not emit compiled assets that include errors
: null,
new ExtractTextPlugin({
filename: isDevelopment
? 'assets/styles/main.css'
: 'assets/styles/[name].[chunkhash].css',
}),
isDevelopment
? null
: new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: module => /node_modules/.test(module.resource),
}),
isDevelopment
? null
: new webpack.optimize.CommonsChunkPlugin({name: 'manifest'}),
isProduction
? new webpack.optimize.UglifyJsPlugin()
: null,
isDevelopment
? null
: new webpack.BannerPlugin(`${pkg.version} ${new Date().toString()}`),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src/index.html'),
minify: isProduction ? {collapseWhitespace: true, collapseInlineTagWhitespace: true} : false,
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin(),
new CopyWebpackPlugin([
{
context: 'src/assets',
from: '**/*',
to: 'assets',
ignore: ['styles/**/*'],
},
]),
new RobotstxtPlugin({
policy: [
isProduction
? {userAgent: '*', allow: '/'}
: {userAgent: '*', disallow: '/'},
],
}),
new WriteFilePlugin(), // Forces webpack-dev-server to write files.
].filter(Boolean),
devtool: isProduction
? 'none'
: 'source-map',
performance: {
maxAssetSize: 500000,
},
};
module.exports = webpackConfig;