This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
webpack.config.js
497 lines (453 loc) · 15.7 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
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
/**
* Inspired by https://github.com/topheman/react-es6-redux
*/
/* eslint-disable max-len */
/* eslint-disable */
const path = require('path');
const log = require('npmlog');
const nodeSass = require("node-sass");
log.level = 'silly';
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const myLocalIp = require('my-local-ip');
const common = require('./common');
const AssetsPlugin = require('assets-webpack-plugin');
const plugins = [];
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const BANNER = common.getBanner();
const BANNER_HTML = common.getBannerHtml();
const root = __dirname;
const MODE_DEV_SERVER = process.argv[1].indexOf('webpack-dev-server') > -1 ? true : false;
const MODE_LOCAL_SERVER = !!process.env.MODE_LOCAL_SERVER;
log.info('webpack', 'Launched in ' + (MODE_DEV_SERVER ? 'dev-server' : 'build') + ' mode');
/** environment setup */
const BUILD_DIR = '';
const DIST_DIR = process.env.DIST_DIR || 'dist';// relative to BUILD_DIR
const NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';
const USE_SPA = (process.env.USE_SPA ? process.env.USE_SPA : 0);
const DEVTOOLS = process.env.DEVTOOLS ? JSON.parse(process.env.DEVTOOLS) : null;// can be useful in case you have web devtools (null by default to differentiate from true or false)
const ANALYZE = process.env.ANALYZE ? JSON.parse(process.env.ANALYZE) : false;
// optimize in production by default - otherwize, override with OPTIMIZE=false flag (if not optimized, sourcemaps will be generated)
const OPTIMIZE = process.env.OPTIMIZE ? JSON.parse(process.env.OPTIMIZE) : NODE_ENV === 'production';
const LINTER = process.env.LINTER ? JSON.parse(process.env.LINTER) : true;
const FAIL_ON_ERROR = process.env.FAIL_ON_ERROR ? JSON.parse(process.env.FAIL_ON_ERROR) : !MODE_DEV_SERVER;// disabled on dev-server mode, enabled in build mode
const STATS = process.env.STATS ? JSON.parse(process.env.STATS) : false; // to output a stats.json file (from webpack at build - useful for debuging)
const LOCALHOST = process.env.LOCALHOST ? JSON.parse(process.env.LOCALHOST) : true;
const ASSETS_LIMIT = typeof process.env.ASSETS_LIMIT !== 'undefined' ? parseInt(process.env.ASSETS_LIMIT, 10) : 5000;// limit bellow the assets will be inlines
const hash = ''; // (NODE_ENV === 'production' && DEVTOOLS ? '-devtools' : '') + (NODE_ENV === 'production' ? '-[hash]' : '');
const TEST = false;
const NOPFN = path.join(__dirname, "js/nop.js");
const NOPOBJECT = path.join(__dirname, "js/nop_object.js");
const bbopJS = path.resolve(__dirname, 'gen/bbop.min.js');
// const bbopJS = path.resolve(__dirname, 'node_modules/bbop/bbop.js');
const mode = NODE_ENV;
const monarchAccess = path.join(__dirname, 'ui/monarchAccess/index.js');
const monarchNGPrelude = path.join(__dirname, 'ui/style/_prelude-ng.scss');
/** integrity checks */
if (/^\w+/.test(DIST_DIR) === false || /\/$/.test(DIST_DIR) === true) { // @todo make a better regexp that accept valid unicode leading chars
log.error('webpack', `DIST_DIR should not contain trailing slashes nor invalid leading chars - you passed "${DIST_DIR}"`);
process.exit(1);
}
log.info('webpack', `${NODE_ENV.toUpperCase()} mode`);
if (USE_SPA) {
log.info('webpack', 'USE_SPA active');
}
if (DEVTOOLS) {
log.info('webpack', 'DEVTOOLS active');
}
if (!OPTIMIZE) {
log.info('webpack', 'SOURCEMAPS activated');
}
if (FAIL_ON_ERROR) {
log.info('webpack', 'NoErrorsPlugin disabled, build will fail on error');
}
/** plugins setup */
if (!FAIL_ON_ERROR) {
plugins.push(new webpack.NoEmitOnErrorsPlugin());
}
plugins.push(new AssetsPlugin());
plugins.push(new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
}));
if (USE_SPA) {
plugins.push(new VueLoaderPlugin());
}
if (USE_SPA) {
plugins.push(new HtmlWebpackPlugin({
title: 'Monarch',
template: 'ui/index.ejs',
filename: 'index.html',
inject: MODE_DEV_SERVER, // inject scripts in dev-server mode - in build mode, use the template tags
MODE_DEV_SERVER: MODE_DEV_SERVER,
MODE_LOCAL_SERVER: MODE_LOCAL_SERVER,
DEVTOOLS: DEVTOOLS,
BANNER_HTML: BANNER_HTML,
USE_SPA: USE_SPA
}));
plugins.push(new HtmlWebpackPlugin({
title: 'Monarch',
template: 'ui/index.ejs',
filename: '404.html',
inject: MODE_DEV_SERVER, // inject scripts in dev-server mode - in build mode, use the template tags
MODE_DEV_SERVER: MODE_DEV_SERVER,
MODE_LOCAL_SERVER: MODE_LOCAL_SERVER,
DEVTOOLS: DEVTOOLS,
BANNER_HTML: BANNER_HTML,
USE_SPA: USE_SPA
}));
}
// extract css into one main.css file
const extractSass = new ExtractTextPlugin({
filename: `[name]${hash}.bundle.css`,
disable: MODE_DEV_SERVER || TEST,
allChunks: true
});
plugins.push(extractSass);
plugins.push(new webpack.BannerPlugin(BANNER));
plugins.push(new webpack.HotModuleReplacementPlugin());
plugins.push(new webpack.NamedModulesPlugin()); // HMR shows correct file names in console on update
plugins.push(new webpack.DefinePlugin({
// Lots of library source code (like React) are based on process.env.NODE_ENV
// (all development related code is wrapped inside a conditional that can be dropped if equal to "production"
// this way you get your own react.min.js build)
'process.env': {
NODE_ENV: JSON.stringify(NODE_ENV),
DEVTOOLS: DEVTOOLS, // You can rely on this var in your code to enable specific features only related to development (that are not related to NODE_ENV)
LINTER: LINTER, // You can choose to log a warning in dev if the linter is disabled
USE_SPA: USE_SPA
}
}));
if (ANALYZE) {
plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static'
}));
}
if (NODE_ENV !== 'production') {
// to keep compatibility with old loaders - debug: true was previously on config
plugins.push(new webpack.LoaderOptionsPlugin({
debug: true
}));
}
if (MODE_DEV_SERVER) {
// webpack-dev-server mode
if(LOCALHOST) {
log.info('webpack', 'Check http://localhost:8081');
}
else {
log.info('webpack', 'Check http://' + myLocalIp() + ':8081');
}
// https://github.com/1337programming/webpack-browser-plugin
// const WebpackBrowserPlugin = require('webpack-browser-plugin');
// plugins.push(
// new WebpackBrowserPlugin({
// // browser: 'Safari' // 'Firefox'
// })
// );
}
else {
// build mode
log.info('webpackbuild', `rootdir: ${root}`);
if (STATS) {
// write infos about the build (to retrieve the hash) https://webpack.github.io/docs/long-term-caching.html#get-filenames-from-stats
plugins.push(function () {
this.plugin('done', function(stats) {
require('fs').writeFileSync(
path.join(__dirname, BUILD_DIR, DIST_DIR, 'stats.json'),
JSON.stringify(stats.toJson()));
});
});
}
}
/** preloaders */
const preLoaders = [];
if (LINTER && !ANALYZE) {
log.info('webpack', 'LINTER ENABLED');
preLoaders.push({
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
enforce: 'pre',
options: {
configFile: path.join(__dirname, 'wbs.eslintrc')
}
});
}
else {
log.info('webpack', 'LINTER DISABLED');
}
/** webpack config */
const config = {
mode: mode,
amd: {
jQuery: true
},
bail: FAIL_ON_ERROR,
entry: {
'app': './js/index.js'
},
output: {
publicPath: MODE_DEV_SERVER ? '/' : '/dist/',
filename: `[name]${hash}.bundle.js`,
chunkFilename: `[id]${hash}.chunk.js`,
path: path.resolve(__dirname, BUILD_DIR, DIST_DIR)
},
cache: true,
devtool: OPTIMIZE ? false : 'sourcemap',
module: {
noParse: [
bbopJS,
path.resolve(__dirname, 'node_modules/bootstrap/dist/js/bootstrap.min.js'),
path.resolve(__dirname, 'node_modules/underscore/underscore-min.js'),
path.resolve(__dirname, 'node_modules/d3/d3.min.js'),
path.resolve(__dirname, 'node_modules/jquery/dist/jquery.min.js'),
// path.resolve(__dirname, 'node_modules/phenogrid/dist/phenogrid-bundle.js'),
path.resolve(__dirname, 'gen/phenogrid.min.js'),
],
rules: [
...preLoaders,
{
test: /\.vue$/,
loader: 'vue-loader',
include: [
path.resolve('ui')
],
options: {
esModule: true,
loaders: {
scss: [ 'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
// data: '@import "variables";',
includePaths: [
path.resolve(__dirname, 'node_modules/bootstrap/scss/bootstrap'),
path.resolve(__dirname, 'node_modules/bootstrap/scss'),
path.resolve(__dirname, 'node_modules/font-awesome/scss'),
]
},
},
]
}
},
},
{
test: /\.js$/,
// exclude: /node_modules/,
include: [
path.resolve('ui'),
path.resolve('js'),
path.resolve('node_modules/q'),
path.resolve('node_modules/webpack-dev-server/client')],
loader: 'babel-loader',
// options: {
// presets: [path.resolve('node_modules/babel-preset-env')],
// plugins: ["fast-async", "syntax-dynamic-import"]
// }
},
{
test: /\.css$/,
use: extractSass.extract({
use: [{
loader: 'css-loader',
query: JSON.stringify({
sourceMap: true
})
}],
// use style-loader in development
fallback: 'style-loader'
})
},
{
test: /\.scss$/,
use: extractSass.extract({
use: [
{
loader: 'css-loader',
query: JSON.stringify({
sourceMap: true
})
},
{
loader: 'sass-loader',
options: {
includePaths: [
// path.join(__dirname, 'node_modules'),
path.resolve(__dirname, 'node_modules/font-awesome/scss'),
path.join(__dirname, 'node_modules/bootstrap/scss'),
// path.join(__dirname, 'node_modules/bootstrap-sass/assets/stylesheets/bootstrap')
],
sourceMap: true,
// importer: [
// // // url will be the string passed to @import
// // // prev is the file where the import was encountered
// (url, prev) => {
// // console.log('###importer', url, prev);
// if (url.indexOf('bootstrap/') === 0) {
// return {
// file: path.join(__dirname, 'node_modules/bootstrap-sass/assets/stylesheets/' + url)
// };
// }
// else if (url.indexOf('font-awesome') === 0) {
// return {
// file: path.join(__dirname, 'node_modules/font-awesome/scss/' + url)
// };
// }
// else {
// return nodeSass.types.Null();
// }
// }
// ]
}
}],
// use style-loader in development
fallback: 'style-loader'
})
},
{
test: /\.(svg|woff|woff2|ttf|eot)$/,
loader: 'file-loader'
},
{ test: /\.(png|jpg|jpeg|gif)$/, loader: 'url-loader?limit=' + ASSETS_LIMIT + '&name=[hash].[ext]' },
]
},
plugins: plugins,
node: {
console: true,
fs: 'empty',
net: 'empty',
tls: 'empty'
},
resolve: {
modules: ['node_modules'],
alias: {
'bbop': (USE_SPA ? NOPOBJECT : bbopJS),
'monarchSearchResults' : path.join(__dirname, 'js/search_results.js'), // (USE_SPA ? NOPFN : path.join(__dirname, 'js/search_results.js')),
'jquery': path.join(__dirname, 'node_modules/jquery/dist/jquery.min.js'),
'jquery-ui': path.join(__dirname, 'node_modules/jquery-ui/'),
'd3': path.join(__dirname, 'node_modules/d3/d3.min.js'),
'monarchNGMain': (USE_SPA ? '../ui/main.js' : NOPFN),
'monarchSCSS': (USE_SPA ? '../ui/style/monarch-ng.scss' : '../css/monarch.scss'),
'monarchAccess': monarchAccess,
'monarchNGPrelude': monarchNGPrelude,
'monarchHomeCSS': (USE_SPA ? '../css/empty.css' : '../css/monarch-home.css'),
'bootstrap$': path.join(__dirname, 'node_modules/bootstrap/'),
'bootstrapSCSS$': path.join(__dirname, 'node_modules/bootstrap/scss/bootstrap.scss'),
'vue$': 'vue/dist/vue.min.js', // 'vue/dist/vue.esm.js',
'vue-good-table$': 'vue-good-table/dist/vue-good-table.min.js',
'ringo/httpclient': NOPFN,
'phenogrid': path.join(__dirname, 'gen/phenogrid.min.js'),
// 'phenogrid': path.join(__dirname, 'node_modules/phenogrid/js/phenogrid.js')
// 'phenogrid': path.join(__dirname, 'node_modules/phenogrid/dist/phenogrid-bundle.js')
}
}
};
if (USE_SPA) {
// config.entry.spa = './ui/main.js';
// config.entry.spastyle = './ui/style/main.scss';
config.resolve.extensions = ['.js', '.vue', '.json'];
// config.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
config.resolve.alias['@'] = path.resolve('ui');
}
if (MODE_DEV_SERVER) {
config.devServer = {
clientLogLevel: 'info', // 'warning',
historyApiFallback: true,
noInfo: false,
hot: true,
inline: true,
contentBase: false,
compress: true,
host: LOCALHOST ? 'localhost' : myLocalIp(),
open: true,
overlay: { warnings: !OPTIMIZE, errors: true },
publicPath: '/',
port: 8081,
quiet: false,
watchOptions: {
poll: false,
},
headers: {
"Access-Control-Allow-Origin": "http://localhost:8080",
"Access-Control-Allow-Credentials": "true"
}
// clientLogLevel: 'warning',
// host: LOCALHOST ? 'localhost' : myLocalIp(),
// watchContentBase: true,
// hot: false,
// hotOnly: false,
// inline: true,
// contentBase: './',
// historyApiFallback: true,
// headers: {
// "Access-Control-Allow-Origin": "http://localhost:8080",
// "Access-Control-Allow-Credentials": "true"
// }
};
config.devServer.proxy = {
'*.json': {
target: 'http://localhost:8080'
},
'/status': {
target: 'http://localhost:8080'
},
'/admin/introspect': {
target: 'http://localhost:8080'
},
'/simsearch/phenotype': {
target: 'http://localhost:8080'
},
'/robots.txt': {
target: 'http://localhost:8080'
},
'/score': {
target: 'http://localhost:8080'
},
'/compare': {
target: 'http://localhost:8080'
},
'/autocomplete': {
target: 'http://localhost:8080'
},
'/searchapi': {
target: 'http://localhost:8080'
},
'/node_modules/phenogrid/': {
target: 'http://localhost:8080'
},
};
if (!USE_SPA) {
config.devServer.proxy['/'] = {
target: 'http://localhost:8080'
};
}
else {
config.devServer.proxy['/image'] = {
target: 'http://localhost:8080'
};
if (MODE_LOCAL_SERVER) {
console.log('MODE_LOCAL_SERVER means devServer proxy disabled');
}
else {
config.devServer.proxy['/**/*.json'] = {
target: 'http://localhost:8080'
};
config.devServer.proxy['/legacy'] = {
target: 'http://localhost:8080',
};
config.devServer.proxy['/'] = {
target: 'http://localhost:8080',
bypass: function(req, res, proxyOptions) {
return '/index.html';
}
};
}
}
}
console.log('config.output', config.output);
module.exports = config;