-
Notifications
You must be signed in to change notification settings - Fork 233
/
webpack.config.js
257 lines (234 loc) · 9.3 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
253
254
255
256
257
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin').CleanWebpackPlugin;
const path = require('path');
const webpack = require('webpack');
const fs = require('fs');
const BUILD_DIR = 'dist/webapp';
const OUTPUT_PATH = path.resolve(__dirname, BUILD_DIR);
function getDevServerSettings(env) {
const QUERYBOOK_UPSTREAM = env && env.QUERYBOOK_UPSTREAM;
if (!QUERYBOOK_UPSTREAM) {
throw Error('Upstream API server is required for this to work');
}
const settings = {
contentBase: OUTPUT_PATH,
disableHostCheck: true,
hot: true,
historyApiFallback: {
index: '/build/index.html',
},
proxy: {
'/ds/*': {
target: QUERYBOOK_UPSTREAM,
changeOrigin: true,
secure: false,
headers: {
// can add custom headers here
// "X-name": "value"
},
pathRewrite: function (req) {},
bypass: function (req, res, proxyOptions) {},
},
'/-/socket.io/*': {
target: QUERYBOOK_UPSTREAM,
changeOrigin: true,
ws: true,
},
'/static/*': {
target: QUERYBOOK_UPSTREAM,
changeOrigin: true,
},
'/oauth2callback': {
target: QUERYBOOK_UPSTREAM,
changeOrigin: true,
},
},
publicPath: '/build/',
onListening: (server) => {
let firstTimeBuildComplete = true;
const port = server.listeningApp.address().port;
server.compiler.hooks.done.tap('done', () => {
if (!firstTimeBuildComplete) {
return;
}
firstTimeBuildComplete = false;
setImmediate(() => {
console.log('\033c');
console.log(`
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗██████╗ ██████╗ ██████╗ ██╗ ██╗
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝██╔══██╗██╔═══██╗██╔═══██╗██║ ██╔╝
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██████╔╝██║ ██║██║ ██║█████╔╝
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ██╔══██╗██║ ██║██║ ██║██╔═██╗
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ██████╔╝╚██████╔╝╚██████╔╝██║ ██╗
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝
- Website is served on http://localhost:${port}
- Run terminal inside container with: \`docker exec -it querybook_web bash\`
- Stop the containers with \`ctrl+c\` or run \`make bundled_off\`
`);
});
});
},
};
if (env && env.QUERYBOOK_COOKIE) {
for (const proxyPath in settings.proxy) {
settings.proxy[proxyPath]['headers'] = {
Cookie: env.QUERYBOOK_COOKIE,
};
}
}
return settings;
}
module.exports = (env, options) => {
const PROD = ((env && env.NODE_ENV) || options.mode) === 'production';
const mode = PROD ? 'production' : 'development';
const entry = {
react_app: './querybook/webapp/index.tsx',
};
const devTool = PROD
? {}
: {
// https://webpack.js.org/plugins/source-map-dev-tool-plugin/
devtool: false,
};
const watchOptions = PROD
? {}
: {
watchOptions: {
poll: 1000,
},
};
const customScriptPath = !!process.env.QUERYBOOK_PLUGIN
? path.resolve(
process.env.QUERYBOOK_PLUGIN,
'./webpage_plugin/custom_script.ts'
)
: null;
if (customScriptPath != null && fs.existsSync(customScriptPath)) {
entry.custom = customScriptPath;
}
const appName = process.env.QUERYBOOK_APPNAME || 'Querybook';
const devServer = env.WEBPACK_SERVE ? getDevServerSettings(env) : {};
return {
entry,
mode,
devServer,
output: {
filename: '[name].[fullhash].js',
path: OUTPUT_PATH,
publicPath: '/build/',
clean: true,
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: ['.ts', '.tsx', '.js', '.json', '.scss`'],
// emulate baseUrl + paths behavior in tsConfig until tsconfig path plugin is fixed
modules: [
path.resolve(__dirname, './querybook/webapp/'),
'node_modules',
],
alias: {
config: path.resolve(__dirname, './querybook/config/'),
},
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: 'babel-loader',
options: {
envName: mode,
},
},
exclude: [/[\\/]node_modules[\\/]/],
},
{
test: /\.(css|sass|scss)$/,
use: [
PROD ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
ident: 'postcss',
plugins: [
[
'postcss-preset-env',
{
// Options for Postcss-Present-env
},
],
],
},
},
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['node_modules'],
},
},
},
],
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
test: /\.tsx?$/,
enforce: 'pre',
loader: 'source-map-loader',
exclude: [/[\\/]node_modules[\\/]/],
},
{
test: /\.ya?ml$/,
include: path.resolve(__dirname, 'querybook/config'),
use: 'yaml-loader',
},
{
test: /\.md$/i,
type: 'asset/source',
},
],
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
},
},
...devTool,
...watchOptions,
plugins: [
new CleanWebpackPlugin(),
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(require('./package.json').version),
__APPNAME__: JSON.stringify(appName),
__ENVIRONMENT__: JSON.stringify(mode)
}),
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
}),
new HtmlWebpackPlugin({
title: appName,
template: './querybook/webapp/index.html',
chunks: (entry.custom ? ['custom'] : []).concat(['react_app']),
chunksSortMode: 'manual',
}),
new webpack.SourceMapDevToolPlugin({
filename: '[file].map[query]',
exclude: [/vendor/],
}),
!PROD &&
new ReactRefreshWebpackPlugin({
overlay: false,
}),
].filter(Boolean),
};
};