-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.common.js
192 lines (181 loc) · 7.42 KB
/
webpack.config.common.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
// Webpack Plugins.
const { DefinePlugin, BannerPlugin, ProgressPlugin } = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
// This import is required for the CSV parser.
const Buffer = require('buffer/').Buffer // eslint-disable-line
// Path library.
const path = require('path')
// Webpack configuration.
const PACKAGE_JSON_PATH = path.resolve(__dirname, 'package.json')
const PACKAGE_JSON = require(PACKAGE_JSON_PATH) // The package.json file.
// License banner.
const fs = require('fs')
const { optimization } = require('./webpack.config.dev')
const LICENSE_BANNER = require('remove-markdown')(fs.readFileSync(path.resolve(__dirname, 'LICENSE.md'), 'utf8'))
// Module configuration.
const INLINE_LIMIT = 10000 // 10 kb limit for inlining asset data.
module.exports = env => {
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next])
return prev
}, {})
return {
resolve: {
roots: [__dirname, path.resolve(__dirname, 'src')],
alias: { react: path.resolve(__dirname, 'node_modules', 'react') },
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
fallback: {
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer/') // This is required for the CSV parser.
}
},
entry: './src/index.tsx',
output: {
publicPath: '/',
path: path.resolve(__dirname, 'dist'),
chunkFilename: '[name].[chunkhash].chunk.js',
filename: '[name].[fullhash].bundle.js',
clean: true
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/i,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
esmodules: true
}
}
],
'@babel/preset-typescript',
['@babel/preset-react', { runtime: 'automatic' }]
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-runtime',
'@babel/plugin-syntax-dynamic-import'
]
}
}
},
{ test: /\.css$/i, use: ['style-loader', 'css-loader'] },
{
test: /\.s[ac]ss$/i,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(png|gif|jpe?g|webp|svg)$/,
loader: 'image-webpack-loader',
// Specify "enforce: 'pre'" to apply the loader before url-loader/svg-url-loader and not duplicate it in rules with them.
enforce: 'pre'
},
{
test: /\.svg$/i,
use: [
{
loader: 'svg-url-loader',
options: {
name: '[name].[contenthash].[ext]',
limit: INLINE_LIMIT, // If size < INLINE_LIMIT, add escaped image URL to CSS.
outputPath: 'images' // If size > INLINE_LIMIT, move SVG file to public/images via file-loader.
}
}
]
},
{
test: /\.(png|gif|jpe?g|webp)(\?[a-z0-9]+)?$/i,
use: [
{
loader: 'url-loader',
options: {
name: '[name].[contenthash].[ext]',
limit: INLINE_LIMIT, // If size < INLINE_LIMIT, inline base64-encoding of the image.
outputPath: 'images' // If size > INLINE_LIMIT, move image to public/images via file-loader.
}
}
]
},
{
test: /\.(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/i,
use: [
{
loader: 'url-loader',
options: {
name: '[name].[contenthash].[ext]',
limit: INLINE_LIMIT, // If size < INLINE_LIMIT, inline base64-encoding of the font.
outputPath: 'fonts' // If size > INLINE_LIMIT, move font to public/fonts via file-loader.
}
}
]
}
]
},
plugins: [
new DefinePlugin({
RABIT_VERSION: JSON.stringify(PACKAGE_JSON.version)
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'public',
to: 'public',
globOptions: { ignore: ['index.html'] }
}
]
}),
new BannerPlugin(
LICENSE_BANNER +
'\nfullhash | [fullhash]' +
'\nchunkhash | [chunkhash]' +
'\nname | [name]' +
'\nbase | [base]' +
'\nquery | [query]' +
'\nfile | [file]'
),
new ProgressPlugin((percent, msg, ...args) =>
console.log(`${(percent * 100).toFixed(2).padEnd(7)}% | ${msg.padEnd(8)} | ${args.join(' :: ')}`)
),
new HtmlWebpackPlugin({
// The index.html file is used as a template here, which is why we ignore it in CopyWebpackPlugin.
template: './public/index.html',
// The output file name.
filename: 'index.html',
// Metadata to be passed to the template's placeholder strings.
meta: {
author: PACKAGE_JSON.author,
keywords: PACKAGE_JSON.keywords.join(','),
description: PACKAGE_JSON.description
}
}),
new DefinePlugin(envKeys)
],
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxSize: 244000
/*
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
enforce: true,
maxSize: 50000
}
}
*/
}
}
}
}