-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.babel.js
193 lines (181 loc) · 5.34 KB
/
webpack.config.babel.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
import 'dotenv/config'
import path from 'path'
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
import webpack from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import { GitRevisionPlugin } from 'git-revision-webpack-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import UglifyJsPlugin from 'uglifyjs-webpack-plugin'
import { uuidv4 } from './core/uuid'
import * as ProcessUtils from './core/processUtils'
const { buildReport } = ProcessUtils.ENV
const config = {
mode: process.env.NODE_ENV || 'development',
path: path.resolve(__dirname, 'dist'),
}
const isDevelopment = process.env.NODE_ENV !== 'production'
const gitRevisionPlugin = config.mode === 'production' ? null : new GitRevisionPlugin()
// Remove mini-css-extract-plugin log spam
// See: https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/97
class CleanUpStatsPlugin {
// eslint-disable-next-line class-methods-use-this
shouldPickStatChild(child) {
return child.name.indexOf('mini-css-extract-plugin') !== 0
}
apply(compiler) {
compiler.hooks.done.tap('CleanUpStatsPlugin', (stats) => {
const { children } = stats.compilation
if (Array.isArray(children)) {
// eslint-disable-next-line no-param-reassign
stats.compilation.children = children.filter((child) => this.shouldPickStatChild(child))
}
})
}
}
// ==== init plugins
const plugins = [
...(gitRevisionPlugin ? [gitRevisionPlugin] : []),
new MiniCssExtractPlugin({
filename: 'styles-[fullhash].css',
ignoreOrder: true,
}),
new HtmlWebpackPlugin({
template: './web-resources/index.html',
}),
new webpack.DefinePlugin({
__BUST__: JSON.stringify(uuidv4()),
process: {
env: {
NODE_ENV: JSON.stringify(ProcessUtils.ENV.nodeEnv),
RSTUDIO_DOWNLOAD_SERVER_URL: JSON.stringify(process.env.RSTUDIO_DOWNLOAD_SERVER_URL),
APPLICATION_VERSION: gitRevisionPlugin
? JSON.stringify(gitRevisionPlugin.version())
: JSON.stringify(process.env.APP_VERSION),
RECAPTCHA_ENABLED: process.env.RECAPTCHA_ENABLED,
RECAPTCHA_SITE_KEY: JSON.stringify(process.env.RECAPTCHA_SITE_KEY),
EXPERIMENTAL_FEATURES: process.env.EXPERIMENTAL_FEATURES,
},
},
}),
new CleanUpStatsPlugin(),
]
if (isDevelopment) {
plugins.push(new webpack.HotModuleReplacementPlugin())
plugins.push(new ReactRefreshWebpackPlugin())
}
if (buildReport) {
plugins.push(new BundleAnalyzerPlugin())
}
// ====== webpack config
const webPackConfig = {
entry: ['./webapp/Main.js'],
mode: ProcessUtils.ENV.nodeEnv,
devtool: 'source-map',
resolve: {
extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx', '.scss', '.sass', '.css'],
alias: {
'@common': path.resolve(__dirname, 'common/'),
'@core': path.resolve(__dirname, 'core/'),
'@server': path.resolve(__dirname, 'server/'),
'@webapp': path.resolve(__dirname, 'webapp/'),
'@test': path.resolve(__dirname, 'test/'),
},
},
output: {
filename: 'bundle-[fullhash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
devServer: {
hot: true,
devMiddleware: {
index: false, // specify to enable root proxying
},
proxy: [
{
// Proxy all server-served routes:
context: ['/img', '/api', '/auth', '/socket.io', 'sockjs-node'],
target: 'http://localhost:9090',
},
{
context: ['/socket.io', 'sockjs-node'],
target: 'ws://localhost:9090',
ws: true,
},
// Proxy root to server to mirror the server routes (goes to /app/home currently)
{
context: '/',
target: 'http://localhost:9090',
// eslint-disable-next-line consistent-return
bypass(req) {
return req.path !== '/' ? '/index.html' : undefined
},
},
],
compress: false,
port: 9000,
historyApiFallback: true,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
plugins: isDevelopment ? [require.resolve('react-refresh/babel')] : [],
},
},
],
},
{
test: /\.(sass|scss|css)$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
url: {
filter: (url) => {
// Don't handle /img/ urls
return !url.includes('/img/')
},
},
import: {
filter: (url) => {
return true
},
},
},
},
'sass-loader',
],
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader',
},
],
},
plugins,
optimization: {
minimizer: [
new UglifyJsPlugin({
parallel: true,
uglifyOptions: {
compress: true,
output: { comments: false },
},
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin({}),
],
},
}
export default webPackConfig