This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
/
webpack.config.ts
159 lines (150 loc) · 4.54 KB
/
webpack.config.ts
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
import * as webpack from 'webpack';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as WebpackPwaManifest from 'webpack-pwa-manifest';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import { InjectManifest } from 'workbox-webpack-plugin';
import { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
import * as ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
type WebpackPlugins = webpack.WebpackPluginInstance[];
function getPlugins(mode: string): WebpackPlugins {
let plugins: (webpack.WebpackPluginInstance | WebpackPwaManifest)[] = [];
plugins = plugins.concat([
new HtmlWebpackPlugin({
template: './public/index.html',
}),
new WebpackPwaManifest({
lang: 'en',
dir: 'ltr',
name: "mhxbe's React Starterkit",
short_name: 'React Kit',
start_url: '.',
scope: '.',
description:
'An opinionated starter kit for quickly bootstrapping client-side React projects written in TypeScript.',
display: 'standalone',
orientation: 'portrait',
background_color: '#00d8ff',
theme_color: '#00d8ff',
// categories: ['productivity', 'utilities'],
// screenshots: [],
filename: 'manifest.[hash].webmanifest',
inject: true,
ios: {
'apple-mobile-web-app-status-bar-style': 'black',
},
icons: [
{
src: 'public/images/icons/icon.png',
size: 180,
purpose: 'any maskable',
destination: 'images/icons',
ios: true,
},
{
src: 'public/images/icons/icon-512x512.png',
size: 512,
purpose: 'any maskable',
destination: 'images/icons',
ios: 'startup',
},
],
}),
new CopyWebpackPlugin({
patterns: [
{
from: './public/images',
to: 'images',
globOptions: { dot: true, ignore: ['**/.DS_Store', '**/icons/**'] },
},
{ from: './public/locales', to: 'locales' },
{ from: './public/robots.txt' },
{ from: './public/favicon.ico' },
{ from: './public/.htaccess' },
],
}),
]);
if (mode === 'development') {
plugins = plugins.concat([new ReactRefreshWebpackPlugin()]);
}
if (mode === 'production') {
plugins = plugins.concat([
new CleanWebpackPlugin(),
new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false }),
new InjectManifest({
swSrc: './service-worker.ts',
swDest: 'sw.js',
exclude: [
/\.DS_Store$/,
/\.map$/,
/\.open-graph.(?:png|jpg|jpeg)$/,
'report.html',
'robots.txt',
'.htaccess',
],
}),
]);
}
return plugins as WebpackPlugins;
}
type envType = string | undefined;
type argvType = { mode: string };
interface Configuration extends webpack.Configuration {
devServer?: WebpackDevServerConfiguration;
}
export default function (env: envType, { mode }: argvType): Configuration {
const isDevelopment = mode === 'development';
const outputPath = __dirname + '/dist/';
return {
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.ts(x?)$/,
use: [
{
loader: 'babel-loader',
options: {
plugins: isDevelopment ? ['react-refresh/babel'] : [],
},
},
],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
{
test: /\.woff2/,
type: 'asset/resource',
generator: {
filename: 'fonts/[hash][ext][query]',
},
},
],
},
resolve: { extensions: ['.js', '.ts', '.tsx'] },
target: isDevelopment ? 'web' : 'browserslist', // @todo: remove this 'web' value when https://github.com/webpack/webpack-dev-server/issues/2758#issuecomment-704457483 is fixed.
output: {
path: outputPath,
filename: '[name]-[contenthash].js',
publicPath: '/',
},
optimization: { splitChunks: { chunks: 'all' } },
plugins: getPlugins(mode),
devtool: isDevelopment ? 'inline-source-map' : 'source-map',
devServer: {
hot: true,
open: true,
host: '0.0.0.0',
historyApiFallback: true,
devMiddleware: {
publicPath: '/',
},
static: {
directory: outputPath,
},
},
};
}