-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
165 lines (156 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin')
const dotenv = require('dotenv')
const envPath = path.resolve(__dirname, '.env')
if (fs.existsSync(envPath)) dotenv.config({ path: envPath })
const appPublic = path.resolve(__dirname, 'public')
const appBuild = path.resolve(__dirname, 'build')
const appSrc = path.resolve(__dirname, 'src')
const sources = {}
const pages = [
{
name: 'index',
js: path.resolve(appSrc, 'index.jsx'),
html: path.resolve(appPublic, 'index.html')
}
]
pages.forEach(page => {
sources[page.name] = page.js
})
const rewrites = pages.map(page => {
const filename = path.basename(page.html)
const name = path.basename(page.html, '.html')
return {
from: new RegExp(`\/${name}`),
to: `/${filename}`
}
})
rewrites.push({
from: /./,
to: '/'
})
const envs = { 'process.env': {} };
for (const key in process.env) {
if (key.startsWith('REACT_APP_')) {
envs['process.env'][key] = JSON.stringify(process.env[key]);
}
}
module.exports = (env) => {
process.env.NODE_ENV = env.NODE_ENV || 'development'
const isProd = process.env.NODE_ENV === 'production'
return {
target: ['web', 'es5'],
stats: 'errors-warnings',
mode: env.NODE_ENV || 'development',
performance: false,
devtool: isProd ? false : 'source-map',
entry: sources,
output: {
path: appBuild,
publicPath: '/',
filename: isProd ? 'js/[name].[contenthash:8].js' : 'js/[name].bundle.js',
chunkFilename: isProd ? 'js/[name].[contenthash:8].chunk.js' : 'js/[name].chunk.js'
},
resolve: {
extensions: ['.js', '.jsx', '.json']
},
plugins: [
...pages.map(page => {
return new HtmlWebpackPlugin({
inject: true,
chunks: [page.name],
template: page.html,
filename: path.basename(page.html)
})
}),
new webpack.DefinePlugin(envs),
new InterpolateHtmlPlugin(HtmlWebpackPlugin, process.env),
isProd && new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].chunk.css'
}),
new CopyWebpackPlugin({
patterns: [
{
from: appPublic,
to: appBuild,
filter: (filepath) => !filepath.endsWith('.html')
}
]
})
].filter(Boolean),
module: {
strictExportPresence: true,
rules: [
{
enforce: 'pre',
exclude: /@babel(?:\/|\\{1,2})runtime/,
test: /\.(js|jsx|css)$/,
use: 'source-map-loader'
},
{
test: /\.(js|jsx)$/i,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.(s[ac]ss)$/i,
use: [
isProd ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(css)$/i,
use: ['style-loader', 'css-loader']
},
{
test: /\.(bmp|png|jpe?g|gif)$/i,
type: 'asset/resource'
},
{
test: /\.svg$/i,
use: ['@svgr/webpack', 'file-loader']
}
]
},
optimization: {
minimize: isProd,
minimizer: [
new TerserPlugin(),
new CssMinimizerPlugin()
]
},
devServer: {
compress: true,
open: true,
hot: true,
liveReload: true,
port: process.env.PORT || '3000',
proxy: {
'/api': 'http://localhost:3001'
},
static: {
directory: appBuild
},
historyApiFallback: {
disableDotRule: true,
index: appPublic,
rewrites
},
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*'
}
}
}
}