-
Notifications
You must be signed in to change notification settings - Fork 29
/
webpack.config.js
169 lines (164 loc) · 5.33 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
const webpack = require('webpack')
const path = require('path')
const CopyPlugin = require('copy-webpack-plugin')
const packageJson = require('./package.json')
const ESLintPlugin = require('eslint-webpack-plugin')
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const isDevelopment = process.env.NODE_ENV === 'development'
const isProduction = process.env.NODE_ENV === 'production'
const targetElectron = process.env.BUILD_TARGET === 'electron'
const alchemyApiKey = process.env.ALCHEMY_KEY
const walletConnectKey = process.env.WALLET_CONNECT_KEY
if (isProduction && (!alchemyApiKey || !walletConnectKey)) {
throw new Error('Alchemy and WalletConnect key env vars must be set')
}
console.log(`Building with webpack. isProduction:${isProduction}, targetElectron:${targetElectron}`)
const config = {
entry: './src/index.tsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
chunkFilename: 'bundle-[name].js',
},
optimization: {
splitChunks: {
minChunks: 3, // Prevents the ledger dynamic bundle from getting split up into separate vendors + local
minSize: 90000, // Prevent a fourth bundle with walletconnect + ledger common libs
enforceSizeThreshold: 100000,
},
},
externals: {
'node-hid': 'commonjs node-hid', // Exclude node-hid as it gets included in electron separately
ws: 'ws', // Exclude WS to work around walletconnect client bundling issue
},
target: targetElectron ? 'electron-renderer' : 'browserslist',
module: {
rules: [
// Run JS files through babel
{
test: /\.(js|jsx)$/,
use: [
{
loader: 'babel-loader',
options: { cacheDirectory: true },
},
],
exclude: /node_modules/,
},
// Run TS files through TS-loader then babel
{
test: /\.ts(x)?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: { cacheDirectory: true },
},
{
loader: 'ts-loader',
},
],
},
// Enable style loader (though most styles are from CSS-in-JS)
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 0,
modules: false,
},
},
],
exclude: /node_modules/,
},
// Inline fonts and svgs
{
test: /\.(woff|woff2|eot|ttf|otf|svg|)$/,
type: 'asset/inline',
},
],
},
resolve: {
// Set up file switching for certain features that differ on electron vs web
alias: {
'src/features/storage/storageProvider$': targetElectron
? 'src/features/storage/storageProvider-electron.ts'
: 'src/features/storage/storageProvider.ts',
'src/features/ledger/ledgerTransport$': targetElectron
? 'src/features/ledger/ledgerTransport-electron.ts'
: 'src/features/ledger/ledgerTransport.ts',
'src/app/update$': targetElectron ? 'src/app/update-electron.ts' : 'src/app/update.ts',
'src/app/deepLink$': targetElectron ? 'src/app/deepLink-electron.ts' : 'src/app/deepLink.ts',
},
extensions: ['.js', '.jsx', '.tsx', '.ts'],
modules: [
'./node_modules', // First check relative node_modules
path.resolve('./node_modules'), // Then check root node_modules
path.resolve('./'), // Finally check root dir (i.e. for src)
],
},
// Note about react fast refresh: I tried to enable this but it doesn't seem to work with webpack 5 yet.
plugins: [
new ESLintPlugin({
extensions: ['js', 'ts', 'jsx', 'tsx'],
emitError: true,
emitWarning: true,
failOnError: true,
failOnWarning: true,
}),
// Copy over static files
new CopyPlugin(
targetElectron
? {
patterns: [
{ from: './package-electron.json', to: 'package.json' },
{ from: './yarn-electron.lock', to: 'yarn.lock' },
{ from: './src/index.html', to: 'index.html' },
{ from: './electron/main.js', to: 'main.js' },
{ from: './static/*', to: 'static/[name][ext]' },
],
}
: {
patterns: [
{ from: './src/index.html', to: 'index.html' },
{ from: './netlify/*', to: '[name][ext]' },
{ from: './static/*', to: 'static/[name][ext]' },
],
}
),
// Inject some constants into the built code
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(packageJson.version),
__DEBUG__: isDevelopment,
__IS_ELECTRON__: targetElectron,
__ALCHEMY_KEY__: JSON.stringify(alchemyApiKey),
__WALLET_CONNECT_KEY__: JSON.stringify(walletConnectKey),
}),
// Bundle analyzer, don't leave enabled
// new BundleAnalyzerPlugin(),
],
devServer: {
historyApiFallback: true,
hot: true,
open: { target: '/', app: 'Google Chrome' },
},
// Show some extra info during build
stats: {
assets: true,
assetsSort: 'size',
nestedModules: true,
chunks: true,
chunkGroups: true,
chunkModules: true,
chunkOrigins: true,
modules: true,
modulesSort: 'size',
assetsSpace: 200,
modulesSpace: 200,
nestedModulesSpace: 50,
},
}
module.exports = config