-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
127 lines (119 loc) · 2.82 KB
/
webpack.config.mjs
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
import path from 'path'
import { fileURLToPath } from 'url'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import { CleanWebpackPlugin } from 'clean-webpack-plugin'
import Dotenv from 'dotenv-webpack'
import FaviconsWebpackPlugin from 'favicons-webpack-plugin'
import { merge } from 'webpack-merge'
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const production = process.env.NODE_ENV === 'production'
const commonConfig = {
entry: path.resolve(__dirname, 'src/index.tsx'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: production
? 'static/scripts/[name].[contenthash].js'
: 'static/scripts/[name].js',
publicPath: production ? 'auto' : '/',
},
module: {
rules: [
{
test: /\.[tj]sx?$/,
use: [
{
loader: 'ts-loader',
},
],
exclude: /node_modules/,
},
{
test: /\.scss$/,
use: [
production ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
modules: {
mode: 'local',
localIdentName: '[hash:base64:5]',
auto: /\.module\.\w+$/i,
namedExport: false,
},
importLoaders: 2,
},
},
'postcss-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.(png|jpg|gif|webp)$/,
type: 'asset/resource',
generator: {
filename: 'static/images/[hash][ext][query]',
},
},
{
test: /\.(woff(2)?|eot|ttf|otf)$/,
type: 'asset/resource',
generator: {
filename: 'static/fonts/[hash][ext][query]',
},
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack', 'url-loader'],
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'public/index.html'),
}),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'static/styles/index.css',
}),
new Dotenv(),
new FaviconsWebpackPlugin({
logo: path.resolve(__dirname, 'public/favicon.svg'),
outputPath: 'static/favicons/',
prefix: 'static/favicons/',
inject: true,
mode: production ? 'webapp' : 'light',
}),
],
}
const developmentConfig = {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
static: path.resolve(__dirname, 'dist'),
port: 8080,
open: false,
hot: true,
},
plugins: [new ReactRefreshWebpackPlugin()],
}
const productionConfig = {
mode: 'production',
devtool: false,
}
export default (envVars) => {
const { env } = envVars
const envConfig = env === 'production' ? productionConfig : developmentConfig
const config = merge(commonConfig, envConfig)
return config
}