-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
123 lines (120 loc) · 3.58 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const webpack = require('webpack');
require('dotenv').config();
const isDevelopment = process.env.NODE_ENV === 'development';
console.log(`isDevelopment: ${isDevelopment}`);
module.exports = {
mode: isDevelopment ? 'development' : 'production',
entry: {
main: [path.join(process.cwd(), 'src/index.js')],
},
output: {
path: path.resolve(process.cwd(), 'build'),
publicPath: '/',
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].chunk.js',
clean: true, // Automatically clean the output directory before each build
},
optimization: {
moduleIds: 'deterministic', // Enable consistent hashing for long term caching
runtimeChunk: 'single', // Create a runtime file to be shared for all generated chunks
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
return `npm.${packageName.replace('@', '')}`;
},
},
},
},
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [isDevelopment && require.resolve('react-refresh/babel')].filter(Boolean),
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/,
type: 'asset', // Use asset module type for SVG
parser: {
dataUrlCondition: {
maxSize: 8 * 1024, // 8kb
},
},
},
{
test: /\.(jpg|png|gif)$/,
type: 'asset/resource', // Use asset module type for images
},
{
test: /\.html$/,
use: 'html-loader',
},
],
},
plugins: [
isDevelopment && new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
inject: true,
template: 'src/index.html',
favicon: 'src/favicon.ico',
manifest: 'src/manifest.json',
}),
isDevelopment && new ReactRefreshWebpackPlugin(),
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
new webpack.EnvironmentPlugin([
'REACT_APP_OPEN_BALENA_UI_URL',
'REACT_APP_OPEN_BALENA_POSTGREST_URL',
'REACT_APP_OPEN_BALENA_REMOTE_URL',
'REACT_APP_OPEN_BALENA_API_URL',
'REACT_APP_OPEN_BALENA_API_VERSION',
'REACT_APP_BANNER_IMAGE',
]),
].filter(Boolean),
resolve: {
modules: ['node_modules', 'src'],
extensions: ['.js', '.jsx', '.react.js'],
mainFields: ['browser', 'module', 'main'],
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
},
},
devtool: 'eval-cheap-module-source-map',
target: ['web', 'es5'],
performance: {
hints: false,
},
devServer: {
static: {
directory: path.join(__dirname, 'src'), // Path to serve static files from
},
compress: false, // Enable gzip compression
historyApiFallback: true, // Fallback to /index.html for Single Page Applications
open: true, // Open the browser after server has been started
hot: true, // Enable Hot Module Replacement
port: 3000,
},
};