-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
136 lines (126 loc) · 4.28 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
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const NodemonPlugin = require('nodemon-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
const { env: { NODE_ENV = 'development', ANALYZE = false } = {} } = process;
const isDevMode = NODE_ENV === 'development';
console.log(`NODE_ENV = ${NODE_ENV}`);
console.log(`ANALYZE = ${ANALYZE}`);
const cleanOptions = {
root: __dirname,
verbose: true,
};
const config = {
mode: NODE_ENV,
watch: isDevMode,
devtool: 'source-map',
};
const devModeOnly = (item) => {
return isDevMode ? [item] : [];
};
const browserConfig = {
...config,
context: path.resolve(__dirname, 'src', 'client'),
entry: [
...devModeOnly('react-hot-loader/patch'),
...devModeOnly('webpack-hot-middleware/client'),
'babel-polyfill',
'./client.js',
],
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js',
hotUpdateChunkFilename: 'hot/hot-update.js',
hotUpdateMainFilename: 'hot/hot-update.json',
publicPath: '/',
},
module: {
rules: [
{ test: /\.js$/, include: /src/, loader: 'babel-loader' },
{
test: /^((?!bootstrap\.min).)*\.css$/,
use: [{
loader: 'style-loader', // inject CSS to page
}, {
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: '[path][name]__[local]--[hash:base64:5]'
}
}, {
loader: 'postcss-loader', // Run post css actions
options: {
sourceMap: true,
plugins () { // post css plugins, can be exported to postcss.config.js
return [
require('postcss-cssnext')(),
require('postcss-nesting')(),
require('postcss-nested')(),
];
}
}
}]
}, {
test: /bootstrap\.min\.css$/,
use: ['style-loader', 'css-loader']
},
],
},
plugins: [
new webpack.DefinePlugin({
__isBrowser__: 'true',
NODE_ENV: JSON.stringify(NODE_ENV),
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
}),
new CleanWebpackPlugin(['public'], cleanOptions),
new HtmlWebpackPlugin({
template: `${__dirname}/src/index.html`,
inject: 'body'
}),
...devModeOnly(new webpack.HotModuleReplacementPlugin()),
// ...devModeOnly(new OpenBrowserPlugin({ url: 'http://localhost:3000' })),
...devModeOnly(new DuplicatePackageCheckerPlugin()),
...ANALYZE ? [new BundleAnalyzerPlugin({ analyzerPort: 7777 })] : [],
],
};
const serverConfig = {
...config,
context: path.resolve(__dirname, 'src', 'server'),
entry: './server.js',
target: 'node',
node: {
// for webpack not to mess up __dirname
__dirname: false,
__filename: false,
},
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname),
filename: 'server.js',
publicPath: '/',
},
module: {
rules: [
{ test: /\.(js)$/, use: 'babel-loader' },
],
},
plugins: [
new webpack.DefinePlugin({
__isBrowser__: 'false',
NODE_ENV: JSON.stringify(NODE_ENV),
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
}),
new CleanWebpackPlugin(['server.js'], cleanOptions),
// starts nodemon only in development mode
new NodemonPlugin({
nodeArgs: ['--inspect']
}),
],
};
module.exports = [browserConfig, serverConfig];