This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.js
129 lines (117 loc) · 4.21 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
const webpack = require('webpack');
// builtin node path module
const path = require('path');
// It moves every require('style.css') in entry chunks into a separate css output file.
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// Create html files from the bundle
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const nodeExternals = require('webpack-node-externals');
module.exports = ({ base }) => {
const nodeEnv = process.env.NODE_ENV || 'development';
const isProd = nodeEnv === 'production';
const isClient = base === 'client' ? true : false
return {
devtool: isProd ? false : 'eval-source-map',
mode: isProd ? 'production' : 'development',
externals: isClient ? [] : [nodeExternals()],
entry: isClient
? { index: path.join(__dirname, './app/index.jsx'), vendor: ['react', 'react-dom'] }
: { server: path.join(__dirname, 'server.js') },
output: {
path: path.join(__dirname, './build'),
filename: '[name].js',
publicPath: '/'
},
optimization: {
splitChunks: {
chunks: 'all'
// name: 'vendor',
// minChunks: Infinity,
// filename: 'vendor.bundle.js'
},
removeEmptyChunks: true,
minimizer: [
new UglifyJsPlugin({
parallel: true,
sourceMap: !isProd
})
]
},
resolve: {
alias: {
'~': path.join(__dirname, './app')
},
extensions: ['.js', '.jsx']
},
target: isClient ? 'web' : 'node',
module: {
rules: [{
test: /\.jsx?$/,
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
],
plugins: [
"@babel/plugin-syntax-dynamic-import"
]
}
}, {
test: /\.(scss|sass)$/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true
}
},
{ loader: 'sass-loader' }
]
}]
},
devServer: {
historyApiFallback: true,
},
plugins: isClient ? [
new webpack.DefinePlugin({
// http://stackoverflow.com/a/35372706/2177568
// for server side code, just require, don't chunk
// use `if (ONSERVER) { ...` for server specific code
ONSERVER: false,
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
}),
new FaviconsWebpackPlugin({
logo: './app/images/favicon.png',
prefix: 'icons-[hash]/',
emitStats: true,
persisentCache: true,
background: '#000',
inject: true
}),
new HtmlWebpackPlugin({
template: 'app/index.html',
inject: 'body',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css"
})
] : [
new webpack.DefinePlugin({
// http://stackoverflow.com/a/35372706/2177568
// for server side code, just require, don't chunk
// use `if (ONSERVER) { ...` for server specific code
ONSERVER: true,
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
})
]
}
}