forked from S-ResearchStack/web-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
177 lines (169 loc) · 4.99 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
166
167
168
169
170
171
172
173
174
175
176
177
const path = require('path');
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const { DefinePlugin, ProvidePlugin } = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
const CircularDependencyPlugin = require('circular-dependency-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const BUILD_DIR = path.resolve(__dirname, 'build');
const styledComponentsTransformer = createStyledComponentsTransformer();
const transformEnvDefine = (env) =>
Object.keys(env).reduce((res, k) => {
res[`process.env.${k}`] = JSON.stringify(env[k]);
return res;
}, {});
module.exports = (env, argv) => {
const isDev = argv.mode !== 'production';
const PUBLIC_PATH = process.env.PUBLIC_PATH
? ['', ...process.env.PUBLIC_PATH.trim().split('/').filter(Boolean), ''].join('/')
: '/';
const useApiProxy = !!process.env.USE_API_PROXY;
let API_URL = process.env.API_URL;
let proxy = undefined;
if (useApiProxy && API_URL) {
proxy = {
'/api': {
target: API_URL,
pathRewrite: { '^/api': '' },
},
logLevel: 'debug',
};
API_URL = '/api';
}
return {
entry: './src/index.tsx',
devtool: isDev ? 'inline-source-map' : undefined,
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: 'style-loader',
options: {
esModule: false,
},
},
!isDev && MiniCssExtractPlugin.loader,
'css-loader',
].filter(Boolean),
},
{
test: /\.tsx?$/,
exclude: [/node_modules/, /\.stories\.tsx?$/, /\.test\.tsx?$/],
...(isDev
? {
loader: 'ts-loader',
options: {
getCustomTransformers: () => ({ before: [styledComponentsTransformer] }),
},
}
: { use: 'ts-loader' }),
},
{
test: /\.svg$/i,
type: 'asset',
resourceQuery: /url/,
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] },
use: [
{
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
cleanupIDs: false,
},
},
},
],
},
},
},
],
},
{
test: /\.(png|jpe?g|gif)$/i,
type: 'asset/resource',
},
],
},
resolve: {
alias: {
src: path.resolve(__dirname, 'src/'),
},
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: '[name].[contenthash].js',
path: BUILD_DIR,
clean: true,
publicPath: PUBLIC_PATH,
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
minimize: !isDev,
minimizer: [new CssMinimizerPlugin()],
},
plugins: [
new NodePolyfillPlugin(),
isDev && new ReactRefreshPlugin(),
!isDev && new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
template: './public/index.html',
}),
isDev && new ForkTsCheckerWebpackPlugin(),
env.analyzeBundle === 'true' && new BundleAnalyzerPlugin(),
new DefinePlugin(
transformEnvDefine({
NODE_ENV: argv.mode,
API_URL,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
AUTHENTICATION_MODE: process.env.AUTHENTICATION_MODE,
GOOGLE_OAUTH_URL: process.env.GOOGLE_OAUTH_URL || "https://accounts.google.com/gsi/client",
PUBLIC_PATH,
MOCK_API: process.env.MOCK_API,
VERSION: require('./package.json').version,
})
),
new ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
isDev &&
new CircularDependencyPlugin({
exclude: /node_modules/,
include: /src/,
failOnError: true,
allowAsyncCycles: false,
cwd: process.cwd(),
}),
].filter(Boolean),
devServer: {
historyApiFallback: true,
hot: true,
proxy,
port: process.env.PORT,
},
};
};