-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
97 lines (91 loc) · 2.13 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
/* eslint-disable global-require */
require('dotenv').config();
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const fs = require('fs');
const {NODE_ENV, PORT} = process.env;
const isProduction = NODE_ENV === 'production';
const rules = [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
plugins: [!isProduction && require.resolve('react-refresh/babel')].filter(Boolean)
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|svg)$/i,
type: 'asset/resource'
},
{
test: /\.(woff(2)?|eot|ttf|otf)$/,
type: 'asset/inline'
}
];
const plugins = [
new HtmlWebpackPlugin({
template: 'public/index.html',
title: require('./package.json').description,
favicon: 'public/favicon.png',
hash: true
}),
new Dotenv()
];
const buildAlias = () => {
const alias = {'@root': __dirname};
const directories = fs.readdirSync(path.resolve(__dirname, 'src'));
directories.forEach(dir => {
const directory = path.resolve(__dirname, 'src', dir);
if (fs.statSync(directory).isDirectory()) {
alias[`@/${dir}`] = directory;
}
});
return alias;
};
module.exports = () => {
if (!isProduction) {
plugins.push(new ReactRefreshWebpackPlugin());
}
return {
devtool: isProduction ? false : 'source-map',
entry: './index.js',
output: {
filename: isProduction ? '[name].[contenthash].js' : '[name].js',
path: path.resolve(__dirname, 'dist'),
clean: true,
publicPath: '/'
},
plugins,
resolve: {
alias: buildAlias()
},
module: {rules},
optimization: {
splitChunks: {
chunks: 'all'
}
},
devServer: {
open: true,
port: PORT,
compress: true,
client: {
overlay: true,
progress: true
},
hot: true,
historyApiFallback: true
}
};
};