-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
584 lines (552 loc) · 18 KB
/
Copy pathwebpack.config.js
File metadata and controls
584 lines (552 loc) · 18 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* eslint-disable no-console */
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
//binhnt: Visualize size of webpack output files with an interactive zoomable treemap.
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
//binhnt: A webpack plugin to remove/clean your build folder(s).
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
//binhnt: Copies individual files or entire directories, which already exist, to the build directory.
const CopyPlugin = require('copy-webpack-plugin');
//binhnt: simplifies creation of HTML files to serve your webpack bundles
const HtmlWebpackPlugin = require('html-webpack-plugin');
//binhnt: This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
//binhnt: A Webpack plugin to optimize \ minimize CSS assets.
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
//binhnt: This plugin measures your webpack build speed, giving an output
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
//binhnt: This plugin uses terser to minify your JavaScript.
const TerserPlugin = require('terser-webpack-plugin');
//binhnt: A Webpack plugin for generating an asset manifest.
const ManifestPlugin = require('webpack-manifest-plugin');
//binhnt: Webpack plugin that runs TypeScript type checker on a separate process
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
//binhnt: Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.
const parsedArgs = require('yargs').argv;
//binhnt: Load proxy config
const getProxyConfig = require('./webpack.proxy-config');
const packageConfig = require('./package.json');
// input dir
const APP_DIR = path.resolve(__dirname, './');
// output dir
const BUILD_DIR = path.resolve(__dirname, './dist');
//binhnt add
const { createEmotionPlugin } = require('emotion-ts-plugin')
const {
mode = 'development',
devserverPort = 9000,
measure = false,
analyzeBundle = false,
analyzerPort = 8888,
nameChunks = false,
} = parsedArgs;
const isDevMode = mode !== 'production';
const isDevServer = process.argv[1].includes('webpack-dev-server');
const output = {
path: BUILD_DIR,
publicPath: '/static/assets/', // necessary for lazy-loaded chunks
};
if (isDevMode) {
output.filename = '[name].[hash:8].entry.js';
output.chunkFilename = '[name].[hash:8].chunk.js';
} else if (nameChunks) {
output.filename = '[name].[chunkhash].entry.js';
output.chunkFilename = '[name].[chunkhash].chunk.js';
} else {
output.filename = '[name].[chunkhash].entry.js';
output.chunkFilename = '[chunkhash].chunk.js';
}
const plugins = [
// creates a manifest.json mapping of name to hashed output used in template files
new ManifestPlugin({
publicPath: output.publicPath,
seed: { app: 'superset' },
// This enables us to include all relevant files for an entry
generate: (seed, files, entrypoints) => {
// Each entrypoint's chunk files in the format of
// {
// entry: {
// css: [],
// js: []
// }
// }
const entryFiles = {};
Object.entries(entrypoints).forEach(([entry, chunks]) => {
entryFiles[entry] = {
css: chunks
.filter(x => x.endsWith('.css'))
.map(x => path.join(output.publicPath, x)),
js: chunks
.filter(x => x.endsWith('.js'))
.map(x => path.join(output.publicPath, x)),
};
});
return {
...seed,
entrypoints: entryFiles,
};
},
// Also write maniafest.json to disk when running `npm run dev`.
// This is required for Flask to work.
writeToFileEmit: isDevMode && !isDevServer,
}),
// expose mode variable to other modules
new webpack.DefinePlugin({
'process.env.WEBPACK_MODE': JSON.stringify(mode),
}),
// runs type checking on a separate process to speed up the build
new ForkTsCheckerWebpackPlugin({
eslint: true,
checkSyntacticErrors: true,
memoryLimit: 4096,
}),
new CopyPlugin({
patterns: [
'package.json',
{ from: 'images', to: 'images' },
{ from: 'stylesheets', to: 'stylesheets' },
],
}),
// static pages
new HtmlWebpackPlugin({
template: './src/staticPages/404.html',
inject: true,
chunks: [],
filename: '404.html',
}),
new HtmlWebpackPlugin({
template: './src/staticPages/500.html',
inject: true,
chunks: [],
filename: '500.html',
}),
];
//binhnt: Add ProgressPlugin
if (!process.env.CI) {
plugins.push(new webpack.ProgressPlugin());
}
// clean up built assets if not from dev-server
if (!isDevServer) {
plugins.push(
new CleanWebpackPlugin({
// required because the build directory is outside the frontend directory:
dangerouslyAllowCleanPatternsOutsideProject: true,
}),
);
}
if (!isDevMode) {
// text loading (webpack 4+)
plugins.push(
new MiniCssExtractPlugin({
filename: '[name].[chunkhash].entry.css',
chunkFilename: '[name].[chunkhash].chunk.css',
}),
);
plugins.push(new OptimizeCSSAssetsPlugin());
}
const PREAMBLE = [path.join(APP_DIR, '/src/injections/preamble.ts')];
if (isDevMode) {
// A Superset webpage normally includes two JS bundles in dev, `theme.ts` and
// the main entrypoint. Only the main entry should have the dev server client,
// otherwise the websocket client will initialize twice, creating two sockets.
// Ref: https://github.com/gaearon/react-hot-loader/issues/141
PREAMBLE.unshift(
`webpack-dev-server/client?http://localhost:${devserverPort}`,
);
}
function addPreamble(entry) {
return PREAMBLE.concat([path.join(APP_DIR, entry)]);
}
//binhnt: Config babel
const babelLoader = {
loader: 'babel-loader',
options: {
cacheDirectory: true,
// disable gzip compression for cache files
// faster when there are millions of small files
cacheCompression: false,
plugins: ['emotion'],
presets: [
[
'@emotion/babel-preset-css-prop',
{
autoLabel: 'dev-only',
labelFormat: '[local]',
},
],
],
},
};
//binhnt: Main config
const config = {
node: {
fs: 'empty',
},
entry: {
theme: path.join(APP_DIR, '/src/injections/theme/index.ts'),
preamble: PREAMBLE,
addSlice: addPreamble('/src/injections/addSlice/index.tsx'),
dashboard: addPreamble('/src/injections/dashboard/index.jsx'),
sqllab: addPreamble('/src/injections/SqlLab/index.tsx'),
sqlsupport: addPreamble('/src/injections/SqlSupport/index.tsx'),
crudViews: addPreamble('/src/injections/views/index.tsx'),
menu: addPreamble('src/injections/menu/index.tsx'),
profile: addPreamble('/src/injections/profile/index.tsx'),
explore: addPreamble('/src/injections/explore/index.jsx'),
data_explore: addPreamble('/src/injections/DataExplore/index.jsx'),
showSavedQuery: [path.join(APP_DIR, '/src/injections/showSavedQuery/index.jsx')],
},
output,
stats: 'verbose',
performance: {
assetFilter(assetFilename) {
// don't throw size limit warning on geojson and font files
return !/\.(map|geojson|woff2)$/.test(assetFilename);
},
},
optimization: {
sideEffects: true,
splitChunks: {
chunks: 'all',
// increase minSize for devMode to 1000kb because of sourcemap
minSize: isDevMode ? 1000000 : 20000,
name: nameChunks,
automaticNameDelimiter: '-',
minChunks: 2,
cacheGroups: {
automaticNamePrefix: 'chunk',
// basic stable dependencies
vendors: {
priority: 50,
name: 'vendors',
test: new RegExp(
`/node_modules/(${[
'abortcontroller-polyfill',
'react',
'react-dom',
'prop-types',
'react-prop-types',
'prop-types-extra',
'redux',
'react-redux',
'react-hot-loader',
'react-select',
'react-sortable-hoc',
'react-virtualized',
'react-table',
'react-ace',
'@hot-loader.*',
'webpack.*',
'@?babel.*',
'lodash.*',
'antd',
'@ant-design.*',
'.*bootstrap',
'react-bootstrap-slider',
'moment',
'jquery',
'core-js.*',
'@emotion.*',
'd3',
'd3-(array|color|scale|interpolate|format|selection|collection|time|time-format)',
].join('|')})/`,
),
},
// bundle large libraries separately
mathjs: {
name: 'mathjs',
test: /\/node_modules\/mathjs\//,
priority: 30,
enforce: true,
},
// viz thumbnails are used in `addSlice` and `explore` page
thumbnail: {
name: 'thumbnail',
test: /thumbnail(Large)?\.png/i,
priority: 20,
enforce: true,
},
},
},
},
resolve: {
modules: [APP_DIR, 'node_modules'],
alias: {
'react-dom': '@hot-loader/react-dom',
// Force using absolute import path of some packages in the root node_modules,
// as they can be dependencies of other packages via `npm link`.
// Both `@emotion/core` and `@teko-data-ui/core` remember some globals within
// module after imported, which will not be available everywhere if two
// different copies of the same module are imported in different places.
'@emotion/core': path.resolve(APP_DIR, './node_modules/@emotion/core'),
'@teko-data-ui/core': path.resolve(
APP_DIR,
'./node_modules/@teko-data-ui/core',
),
'@teko-data-ui/chart-controls': path.resolve(
APP_DIR,
'./node_modules/@teko-data-ui/chart-controls',
),
},
extensions: ['.ts', '.tsx', '.js', '.jsx'],
symlinks: false,
},
context: APP_DIR, // to automatically find tsconfig.json
module: {
// Uglifying mapbox-gl results in undefined errors, see
// https://github.com/mapbox/mapbox-gl-js/issues/4359#issuecomment-288001933
noParse: /(mapbox-gl)\.js$/,
rules: [
{
test: /datatables\.net.*/,
loader: 'imports-loader?define=>false',
},
{
test: /\.tsx?$/,
exclude: [/\.test.tsx?$/],
use: [
'thread-loader',
babelLoader,
{
loader: 'ts-loader',
options: {
// transpile only in happyPack mode
// type checking is done via fork-ts-checker-webpack-plugin
happyPackMode: true,
transpileOnly: true,
// must override compiler options here, even though we have set
// the same options in `tsconfig.json`, because they may still
// be overriden by `tsconfig.json` in node_modules subdirectories.
getCustomTransformers: () => ({
before: [
createEmotionPlugin({
// <------------------- here
sourcemap: true,
autoLabel: true,
labelFormat: '[local]',
// if the jsxFactory is set, should we auto insert the import statement
autoInject: true,
// set for react@17 new jsx runtime
// only effect if `autoInject` is true
// set it in createEmotionPlugin options rather than in `tsconfig.json` will generate more optimized codes:
// import { jsx } from 'react/jsx-runtime' for files not using emotion
// import { jsx } from '@emotion/react/jsx-runtime' for files using emotion
jsxImportSource: '@emotion/react',
}),
],
}),
compilerOptions: {
esModuleInterop: false,
importHelpers: false,
module: 'esnext',
target: 'esnext',
},
},
},
],
},
{
test: /\.jsx?$/,
// include source code for plugins, but exclude node_modules and test files within them
exclude: [/teko-data-ui.*\/node_modules\//, /\.test.jsx?$/],
include: [
new RegExp(`${APP_DIR}/src`),
/teko-data-ui.*\/src/,
new RegExp(`${APP_DIR}/.storybook`),
],
use: [babelLoader],
},
{
test: /\.css$/,
include: [APP_DIR, /teko-data-ui.+\/src/],
use: [
isDevMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isDevMode,
},
},
],
},
{
test: /\.less$/,
include: APP_DIR,
use: [
isDevMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isDevMode,
},
},
{
loader: 'less-loader',
options: {
sourceMap: isDevMode,
javascriptEnabled: true,
},
},
],
},
/* binhnt add bootstrap 4.0 */
{
test: /\.(scss)$/,
use: [{
loader: 'style-loader', // inject CSS to page
}, {
loader: 'css-loader', // translates CSS into CommonJS modules
},
{
loader: 'sass-loader' // compiles Sass to CSS
}]
},
/* for css linking images (and viz plugin thumbnails) */
{
test: /\.png$/,
issuer: {
exclude: /\/src\/staticPages\//,
},
loader: 'url-loader',
options: {
limit: 10000,
name: '[name].[hash:8].[ext]',
},
},
{
test: /\.png$/,
issuer: {
test: /\/src\/staticPages\//,
},
loader: 'url-loader',
options: {
limit: 150000, // Convert images < 150kb to base64 strings
},
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
issuer: {
test: /\.(j|t)sx?$/,
},
use: ['@svgr/webpack'],
},
{
test: /\.(jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[hash:8].[ext]',
},
},
/* for font-awesome */
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff',
options: {
esModule: false,
},
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader',
options: {
esModule: false,
},
},
],
},
externals: {
cheerio: 'window',
'react/lib/ExecutionEnvironment': true,
'react/lib/ReactContext': true,
},
plugins,
devtool: false,
};
let proxyConfig = getProxyConfig();
if (isDevMode) {
config.devtool = 'eval-cheap-module-source-map';
config.devServer = {
before(app, server, compiler) {
// load proxy config when manifest updates
const hook = compiler.hooks.webpackManifestPluginAfterEmit;
hook.tap('ManifestPlugin', manifest => {
console.log("binhnt: start push ManifestPlugin by hook before start")
proxyConfig = getProxyConfig(manifest);
});
},
historyApiFallback: true,
hot: true,
injectClient: false,
injectHot: true,
inline: true,
stats: 'minimal',
overlay: true,
port: devserverPort,
// Only serves bundled files from webpack-dev-server
// and proxy everything else to teko-data backend
proxy: [
// functions are called for every request
() => proxyConfig,
],
// contentBase: path.join(process.cwd(), '../static/assets'),
contentBase: path.join(process.cwd(), './dist'),
};
// find all the symlinked plugins and use their source code for imports
let hasSymlink = false;
Object.entries(packageConfig.dependencies).forEach(([pkg, version]) => {
const srcPath = `./node_modules/${pkg}/src`;
if (/teko-data-ui/.test(pkg) && fs.existsSync(srcPath)) {
console.log(
`[Superset Plugin] Use symlink source for ${pkg} @ ${version}`,
);
// only allow exact match so imports like `@teko-data-ui/plugin-name/lib`
// and `@teko-data-ui/plugin-name/esm` can still work.
config.resolve.alias[`${pkg}$`] = `${pkg}/src`;
delete config.resolve.alias[pkg];
hasSymlink = true;
}
});
if (hasSymlink) {
console.log(''); // pure cosmetic new line
}
} else {
config.optimization.minimizer = [
new TerserPlugin({
cache: '.terser-plugin-cache/',
parallel: true,
extractComments: true,
}),
];
}
// Bundle analyzer is disabled by default
// Pass flag --analyzeBundle=true to enable
// e.g. npm run build -- --analyzeBundle=true
if (analyzeBundle) {
config.plugins.push(new BundleAnalyzerPlugin({ analyzerPort }));
}
// Speed measurement is disabled by default
// Pass flag --measure=true to enable
// e.g. npm run build -- --measure=true
const smp = new SpeedMeasurePlugin({
disable: !measure,
});
module.exports = smp.wrap(config);