This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
webpack.config.js
252 lines (241 loc) · 7.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const HtmlWebPackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const PreloadWebpackPlugin = require('preload-webpack-plugin')
const FaviconsWebpackPlugin = require('favicons-webpack-plugin')
const markdownIt = require('markdown-it')
const linkAttributes = require('markdown-it-link-attributes')
const path = require('path')
const dotenv = require('dotenv')
const loadConfig = require('./src/loadConfig')
const overrideEnvConfig = require('./src/overrideEnvConfig')
const TRADE_APP = { name: 'trade', title: 'Gnosis Protocol Exchange', filename: 'trade.html' }
const EXPLORER_APP = { name: 'explorer', title: 'Gnosis Protocol Explorer', filename: 'explorer.html' }
const SWAP_APP_V1 = { name: 'gp-v1', title: null, filename: 'index.html' }
const ALL_APPS = [TRADE_APP, EXPLORER_APP, SWAP_APP_V1]
// Setup env vars
dotenv.config()
const isProduction = process.env.NODE_ENV == 'production'
const baseUrl = isProduction ? '' : '/'
const config = overrideEnvConfig(loadConfig())
const { name: appTitle } = config
function getSelectedApps() {
const appName = process.env.APP
if (appName) {
const app = ALL_APPS.find((app) => appName === app.name)
if (!app) {
throw new Error(`Unknown App ${app}`)
}
return [
{
...app,
filename: 'index.html', // If we return only one app, the html web is "index.html"
},
]
} else {
return ALL_APPS
}
}
// Get selected apps: all apps by default
const apps = getSelectedApps()
console.log('apps', apps)
// Generate one entry point per app
const entryPoints = apps.reduce((acc, app) => {
const { name } = app
acc[name] = `./src/apps/${name}/index.tsx`
return acc
}, {})
// Generate one HTML per app (with their specific entry point)
const htmlPlugins = apps.map((app) => {
const { name, title, filename } = app
return new HtmlWebPackPlugin({
template: config.templatePath,
chunks: [name],
title: title || appTitle,
filename,
ipfsHack: isProduction,
minify: isProduction && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
})
})
module.exports = ({ stats = false } = {}) => ({
entry: entryPoints, // One entry points per app
devtool: isProduction ? 'source-map' : 'eval-source-map',
output: {
path: __dirname + '/dist',
chunkFilename: isProduction ? '[name].[contenthash].js' : '[name].js',
filename: isProduction ? '[name].[contenthash].js' : '[name].js',
publicPath: baseUrl,
},
module: {
rules: [
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
disable: !isProduction,
},
},
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { cacheDirectory: true },
},
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: { cacheDirectory: true },
},
{
loader: 'ts-loader',
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true,
},
},
],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(ttf|otf|eot|woff2?)(\?[a-z0-9]+)?$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
},
{
test: /\.md$/,
use: {
loader: 'frontmatter-markdown-loader',
options: {
mode: ['react-component'],
markdownIt: markdownIt({
html: true,
linkify: true,
breaks: true,
xhtmlOut: true,
}).use(linkAttributes, {
attrs: {
target: '_blank',
rel: 'noopener noreferrer',
},
}),
},
},
},
],
},
devServer: {
historyApiFallback: true,
},
resolve: {
alias: {
'react-dom': '@hot-loader/react-dom',
'bn.js': path.resolve(__dirname, 'node_modules/bn.js'),
'react-inspector': path.resolve(__dirname, 'node_modules/react-inspector'),
},
modules: [path.resolve(__dirname, 'custom'), path.resolve(__dirname, 'src'), 'node_modules'],
extensions: ['.ts', '.tsx', '.js'],
},
// drop unused deps
// https://www.amcharts.com/docs/v4/getting-started/integrations/using-webpack/#Large_file_sizes
externals: function (context, request, callback) {
if (/xlsx|canvg|pdfmake/.test(request)) {
return callback(null, 'commonjs ' + request)
}
callback()
},
plugins: [
...htmlPlugins, // one html page per app
new FaviconsWebpackPlugin({
logo: config.logoPath,
mode: 'webapp', // optional can be 'webapp' or 'light' - 'webapp' by default
devMode: 'webapp', // optional can be 'webapp' or 'light' - 'light' by default
favicons: {
appName: appTitle,
appDescription: appTitle,
developerName: appTitle,
developerURL: null, // prevent retrieving from the nearest package.json
background: '#dfe6ef',
themeColor: '#476481',
icons: {
coast: false,
yandex: false,
},
},
}),
new PreloadWebpackPlugin({
rel: 'prefetch',
include: 'allAssets',
fileBlacklist: [/\.map/, /runtime~.+\.js$/],
}),
isProduction && new InlineChunkHtmlPlugin(HtmlWebPackPlugin, [/runtime/]),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
BASE_URL: baseUrl,
// MOCK: Use mock or real API implementation
MOCK: 'false',
MOCK_WALLET: process.env.MOCK || 'false',
MOCK_TOKEN_LIST: process.env.MOCK || 'false',
MOCK_ERC20: process.env.MOCK || 'false',
MOCK_WETH: process.env.MOCK || 'false',
MOCK_DEPOSIT: process.env.MOCK || 'false',
MOCK_EXCHANGE: process.env.MOCK || 'false',
MOCK_WEB3: process.env.MOCK || 'false',
// AUTOCONNECT: only applies for mock implementation
AUTOCONNECT: 'true',
PRICE_ESTIMATOR_URL: process.env.PRICE_ESTIMATOR_URL || (isProduction && 'production') || 'develop',
APP_ID: null,
INFURA_ID: null,
WALLET_CONNECT_BRIDGE: null,
ETH_NODE_URL: null,
LIQUIDITY_TOKEN_LIST: null,
}),
new ForkTsCheckerWebpackPlugin({ silent: stats }),
// define inside one plugin instance
new webpack.DefinePlugin({
VERSION: JSON.stringify(require('./package.json').version),
DEX_JS_VERSION: JSON.stringify(require('@gnosis.pm/dex-js/package.json').version),
CONTRACT_VERSION: JSON.stringify(require('@gnosis.pm/dex-contracts/package.json').version),
CONFIG: JSON.stringify(config),
}),
].filter(Boolean),
optimization: {
moduleIds: 'hashed',
splitChunks: {
chunks: 'all',
minSize: 20000,
maxAsyncRequests: 10,
maxSize: 1000000,
},
runtimeChunk: true,
},
})