-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
128 lines (116 loc) · 3.48 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
/**
* Webpack configuration file
**/
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const postcssImport = require('postcss-import');
const postcssCssNext = require('postcss-cssnext');
const postcssBR = require('postcss-browser-reporter');
const postcssR = require('postcss-reporter');
const isProd = process.env.NODE_ENV === 'production';
function HashBundlePlugin() {}
HashBundlePlugin.prototype.apply = (compiler) => {
compiler.plugin('done', (statsData) => {
const stats = statsData.toJson();
if (!stats.errors.length) {
const htmlFileName = 'index.html';
const html = fs.readFileSync(path.join(__dirname, htmlFileName), 'utf8');
const htmlOutput = html.replace(/\/build\/.?bundle\.js/, `${'/build/'}${stats.hash}${'.bundle.js'}`);
fs.writeFileSync(path.join(__dirname, htmlFileName), htmlOutput);
}
});
};
const config = {
entry: ['babel-polyfill', path.resolve(__dirname, 'src')],
output: {
filename: `${isProd ? '[hash].' : ''}bundle.js`,
path: path.resolve(__dirname, 'build'),
publicPath: '/build/',
},
resolve: {
extensions: ['.jsx', '.js', '.css', '.json'],
modules: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'assets'),
path.resolve(__dirname, 'node_modules'),
],
},
context: __dirname,
module: {
rules: [{
test: /\.css$/,
loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]!postcss-loader',
exclude: /node_modules\/c3/,
}, {
test: /\.css$/,
loader: 'style-loader!css-loader',
include: /node_modules\/c3/,
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&minetype=application/font-woff&name=[hash].[ext]',
}, {
test: /\.(ttf|eot|svg|jpg|gif|png)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader?name=[hash].[ext]',
}, {
test: /\.(js|jsx)$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
}],
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
postcssImport({
addDependencyTo: webpack,
}),
postcssCssNext(),
postcssBR(),
postcssR(),
],
},
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
},
API_HOST: JSON.stringify(process.env.API_HOST || 'http://localhost:3000'),
}),
],
devServer: {
contentBase: __dirname,
host: 'localhost',
port: Number(process.env.PORT) || 8080,
historyApiFallback: true,
},
};
if (!isProd) {
config.devtool = 'eval-source-map';
config.devServer.hot = true;
config.entry = [
'react-hot-loader/patch',
`webpack-dev-server/client?http://${config.devServer.host}:${config.devServer.port}`,
'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
'babel-polyfill',
path.resolve(__dirname, 'src'),
];
config.plugins.push(new webpack.HotModuleReplacementPlugin());
config.plugins.push(new webpack.NamedModulesPlugin());
} else {
config.plugins.push(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
}));
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
output: {
comments: false,
},
sourceMap: false,
}));
config.plugins.push(new HashBundlePlugin());
}
module.exports = config;