-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
163 lines (158 loc) · 4.68 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
/**
* WebPack config for development and production
*/
import path from 'node:path';
import webpack from 'webpack';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
function parseEnv(name) {
/*
* Next.js expects env files to be in it's root. Webpack needs to reuse some
* of those variables, so we must piggyback on Next.js's env files.
*/
const envFileLocation = `../backend/${name}`;
const envFile = fs.readFileSync(envFileLocation, 'utf8');
return Object.fromEntries(
envFile
.split('\n')
.filter((line) => line.includes('=') && !line.startsWith('#'))
.map((line) => line.split('='))
.map(([name, value]) => [name.trim(), value.trim()]),
);
}
const productionGoogleClientId = parseEnv(
'.env.production.local',
).GOOGLE_CLIENT_ID;
if (productionGoogleClientId === undefined)
throw new Error('Production GOOGLE_CLIENT_ID is not defined');
const developmentGoogleClientId = parseEnv(
'.env.development.local',
).GOOGLE_CLIENT_ID;
if (developmentGoogleClientId === undefined)
throw new Error('Development GOOGLE_CLIENT_ID is not defined');
const productionAuthUrl = parseEnv('.env.production').AUTH_URL;
if (productionAuthUrl === undefined)
throw new Error('Production AUTH_URL is not defined');
const developmentAuthUrl = parseEnv('.env.development').AUTH_URL;
if (developmentAuthUrl === undefined)
throw new Error('Development AUTH_URL is not defined');
const outputPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'dist',
);
export default (_env, argv) =>
/** @type { import('webpack').Configuration } */ ({
module: {
rules: [
{
test: /\.(png|gif|jpg|jpeg|svg)$/,
type: 'asset',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
{
test: /\.[tj]sx?$/,
exclude: /(node_modules)/,
use: [
{
loader: 'babel-loader?+cacheDirectory',
options: {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: {
version: '3.25.2',
proposals: true,
},
bugfixes: true,
// See "browserslist" section of package.json
browserslistEnv: argv.mode,
},
],
['@babel/preset-react'],
['@babel/preset-typescript'],
],
},
},
],
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
symlinks: false,
},
// Set appropriate process.env.NODE_ENV
mode: argv.mode,
/*
* Use recommended source map type in production
* Can't use the recommended "eval-source-map" in development due to
* https://stackoverflow.com/questions/48047150/chrome-extension-compiled-by-webpack-throws-unsafe-eval-error
*/
devtool:
argv.mode === 'development' ? 'cheap-module-source-map' : 'source-map',
entry: {
main:
argv.mode === 'development'
? './src/components/Core/development.tsx'
: './src/components/Core/index.tsx',
background: './src/components/Background/index.ts',
},
plugins: [
new webpack.DefinePlugin({
'process.env.AUTH_URL': JSON.stringify(
argv.mode === 'development' ? developmentAuthUrl : productionAuthUrl,
),
'process.env.GOOGLE_CLIENT_ID': JSON.stringify(
argv.mode === 'development'
? developmentGoogleClientId
: productionGoogleClientId,
),
}),
argv.mode === 'development'
? new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
})
: undefined,
],
output: {
path: outputPath,
clean: true,
publicPath: '/public/',
filename: '[name].bundle.js',
environment: {
arrowFunction: true,
const: true,
destructuring: true,
bigIntLiteral: true,
forOf: true,
dynamicImport: true,
module: true,
},
},
watchOptions: {
ignored: '/node_modules/',
},
optimization: {
minimize: false,
},
performance: {
// Disable bundle size warnings for bundles <2 MB
maxEntrypointSize: 2 * 1024 * 1024,
maxAssetSize: 2 * 1024 * 1024,
},
stats: {
env: true,
outputPath: true,
warnings: true,
errors: true,
errorDetails: true,
errorStack: true,
moduleTrace: true,
timings: true,
},
});