This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
244 lines (237 loc) · 7.2 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
const path = require('path');
const fs = require('fs');
const { DefinePlugin } = require('webpack');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
// some plugins are conditionally-loaded as they are also conditionally used.
module.exports = (env, argv) => {
const isDev = argv.mode === 'development';
const isLiveReload = !!argv.liveReload;
let publicPath = '/';
if (env.PUBLIC_PATH) {
// if we have an array, it means we've probably overriden env in the CLI
// from a predefined env in a yarn/npm script
if (Array.isArray(env.PUBLIC_PATH)) {
// so we take the last one
publicPath = env.PUBLIC_PATH[env.PUBLIC_PATH.length - 1];
} else {
publicPath = env.PUBLIC_PATH;
}
}
const config = {
context: __dirname,
entry: [path.resolve(__dirname, 'src/index.tsx')],
output: {
path: path.resolve(__dirname, 'build'),
publicPath,
filename: 'app.[hash:6].js',
chunkFilename: '[name].[chunkhash:6].js',
},
devtool: (() => {
if (isLiveReload) return 'inline-sourcemap';
if (isDev) return 'eval-source-map';
// else, prod
return 'source-map';
})(),
resolve: {
extensions: ['.tsx', '.jsx', '.js', '.ts'],
alias: {
// Deopts here, because we're not on track with React's versions anymore
// react: path.resolve('./node_modules/react'),
// 'react-dom': path.resolve('./node_modules/react-dom'),
// 'react-router-dom': path.resolve('./node_modules/react-router-dom'),
},
symlinks: false,
},
// MODULE
module: {
// JavaScript and Typescript files
rules: [
{
test: /\.(js|jsx|tsx|ts)$/,
exclude: /node_modules\/((?!protvista-uniprot).*)/,
use: {
loader: 'babel-loader',
},
},
// Stylesheets
{
test: /\.(css|sass|scss)$/,
include: [
// We use realpathSync otherwise doesn't work with symlinks
fs.realpathSync(`${__dirname}/node_modules/litemol/dist/css`),
fs.realpathSync(
`${__dirname}/node_modules/@geneontology/ribbon/es`
),
fs.realpathSync(
`${__dirname}/node_modules/interaction-viewer/styles`
),
fs.realpathSync(`${__dirname}/node_modules/molstar/build/`),
path.resolve(__dirname, 'src'),
],
use: [
{
// loader: isLiveReload
// ? 'style-loader'
// : MiniCssExtractPlugin.loader,
// NOTE: deoptimising this because of issue with Google indexing
loader: 'style-loader',
},
{
loader: 'css-loader', // translates CSS into CommonJS
},
{
loader: 'sass-loader', // compiles Sass to CSS
},
],
},
// SVGs
{
test: /\.svg$/i,
issuer: /\.(t|j)sx?$/,
use: [
{
loader: '@svgr/webpack',
},
],
},
// Fonts
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
include: [path.resolve(__dirname, 'node_modules/litemol/dist/fonts')],
use: [
{
loader: 'file-loader',
options: {
name: '[name].[contenthash:6].[ext]',
outputPath: 'fonts/',
},
},
],
},
// SVGs from protvista-datatable
{
test: /\.svg$/,
include: [
path.resolve(__dirname, 'node_modules/protvista-datatable'),
path.resolve(__dirname, 'node_modules/protvista-uniprot'),
],
loader: 'svg-inline-loader',
},
{
test: /\.(jpe?g|png|gif|ico)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[contenthash:6].[ext]',
},
},
],
},
],
},
// END MODULE
stats: {
children: false,
},
// PLUGINS
plugins: [
!isLiveReload &&
new (require('clean-webpack-plugin').CleanWebpackPlugin)(),
new HtmlWebPackPlugin({
template: `${__dirname}/index.html`,
filename: 'index.html',
}),
!isDev &&
new HtmlWebPackPlugin({
template: `${__dirname}/404.html`,
filename: '404.html',
}),
new DefinePlugin({
BASE_URL: JSON.stringify(publicPath),
LIVE_RELOAD: JSON.stringify(isLiveReload),
}),
!isLiveReload &&
new (require('workbox-webpack-plugin').InjectManifest)({
swSrc: `${__dirname}/src/service-worker/service-worker.ts`,
// TODO: remove following line whenever we manage to reduce size of entrypoint
maximumFileSizeToCacheInBytes: 1024 * 1024 * 50,
dontCacheBustURLsMatching: /\.[\da-f]{6}\.[\w]{2,5}$/i,
// exclude from precaching because one browser will never need all fonts
// formats at the same time, will cache later whichever is actually used
exclude: [/fonts/],
}),
!isLiveReload &&
new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
analyzerMode: 'disabled',
generateStatsFile: true,
statsOptions: { source: false },
}),
!isLiveReload &&
new MiniCssExtractPlugin({
filename: '[name].[contenthash:6].css',
chunkFilename: '[id].[contenthash:6].css',
}),
!isDev &&
new CopyPlugin({
patterns: [{ from: 'sitemap.txt' }],
}),
].filter(Boolean),
// END PLUGINS
};
if (isLiveReload) {
config.devServer = {
contentBase: path.join(__dirname, 'build'),
compress: true,
host: 'localhost',
port: 0,
historyApiFallback: true,
};
}
if (!isDev) {
config.optimization = {
runtimeChunk: true,
// when updating webpack check this URL to adapt the different default
// https://webpack.js.org/plugins/split-chunks-plugin/#optimizationsplitchunks
splitChunks: {
chunks: 'async',
minSize: 30000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 6,
maxInitialRequests: 4,
automaticNameDelimiter: '~',
cacheGroups: {
franklin: {
test: /[\\/]node_modules[\\/]franklin-sites[\\/]/,
name: 'franklin',
chunks: 'all',
},
react: {
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
name: 'react',
chunks: 'all',
},
litemol: {
test: /[\\/]node_modules[\\/](litemol)[\\/]/,
name: 'litemol',
chunks: 'all',
},
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
name: 'default-vendors',
priority: -10,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
};
}
return config;
};