-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
134 lines (130 loc) · 3.17 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
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import DotenvPlugin from 'dotenv-webpack';
import dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
import CompressionPlugin from 'compression-webpack-plugin';
import { execSync } from 'child_process';
import path from 'path';
const myEnv = dotenv.config();
dotenvExpand(myEnv);
const resolveOptions = {
extensions: ['*', '.js'],
modules: [
path.resolve(__dirname, './node_modules'),
path.resolve(__dirname, './src'),
],
// symlinks: false,
};
const webpackConfig = {
entry: ['./src/index.js'],
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.[hash].js',
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
module: {
rules: [
{
test: /\.js$/,
enforce: 'pre',
loader: 'eslint-loader',
options: { emitWarning: true },
},
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
},
},
],
},
{
exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/, /\.css$/],
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new HtmlWebpackPlugin({
template: 'public/index.html',
// favicon: 'public/favicon.ico'
}),
new DotenvPlugin(),
],
serve: {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 3001,
hot: true,
on: {
listening: () => {
execSync('ps cax | grep "Google Chrome"');
execSync(
`osascript chrome.applescript "${encodeURI(
`http://localhost:${process.env.PORT}`,
)}"`,
{
cwd: __dirname,
stdio: 'ignore',
},
);
},
},
},
resolve: resolveOptions,
};
export default (env, argv) => {
if (!argv || argv.mode === 'development') {
webpackConfig.mode = 'development';
webpackConfig.entry.push('react-hot-loader/patch');
webpackConfig.devtool = 'cheap-eval-source-map';
} else if (argv || argv.mode === 'production') {
webpackConfig.plugins.push(
new CleanWebpackPlugin(['dist']),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
}),
);
webpackConfig.optimization = {
minimizer: [
new UglifyJsPlugin(),
],
};
}
return webpackConfig;
};