forked from aces/Loris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
392 lines (363 loc) · 11.1 KB
/
webpack.config.ts
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
import cp from 'child_process';
import fs from 'fs';
import path from 'path';
import webpack, {DefinePlugin, IgnorePlugin} from 'webpack';
import CopyPlugin from 'copy-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
// Build mode (development or production)
const isDev = process.env.NODE_ENV === 'development';
// Target module to build (if there is one)
const target = process.env.target;
// A record mapping each LORIS module to its entry points
const lorisModules: Record<string, string[]> = {
media: ['CandidateMediaWidget', 'mediaIndex'],
issue_tracker: ['issueTrackerIndex', 'index', 'CandidateIssuesWidget'],
login: ['loginIndex'],
publication: ['publicationIndex', 'viewProjectIndex'],
document_repository: ['docIndex', 'editFormIndex'],
candidate_parameters: ['CandidateParameters', 'ConsentWidget'],
configuration: [
'CohortRelations',
'configuration_helper',
'DiagnosisEvolution',
],
conflict_resolver: ['conflict_resolver', 'CandidateConflictsWidget'],
battery_manager: ['batteryManagerIndex'],
bvl_feedback: ['react.behavioural_feedback_panel'],
behavioural_qc: ['behaviouralQCIndex'],
create_timepoint: ['createTimepointIndex'],
candidate_list: ['openProfileForm', 'candidateListIndex'],
datadict: ['dataDictIndex'],
dataquery: ['index'],
data_release: ['dataReleaseIndex'],
dictionary: ['dataDictIndex'],
dqt: [
'components/expansionpanels',
'components/searchabledropdown',
'components/stepper',
'react.app',
'react.fieldselector',
'react.filterBuilder',
'react.navigationStepper',
'react.notice',
'react.savedqueries',
'react.sidebar',
'react.tabs',
],
dicom_archive: ['dicom_archive'],
genomic_browser: ['genomicBrowserIndex'],
electrophysiology_browser: [
'electrophysiologyBrowserIndex',
],
electrophysiology_uploader: [
'ElectrophysiologyUploader',
'UploadForm',
'UploadViewer',
],
imaging_browser: [
'ImagePanel',
'imagingBrowserIndex',
'CandidateScanQCSummaryWidget',
],
instrument_builder: ['react.instrument_builder', 'react.questions'],
instrument_manager: ['instrumentManagerIndex'],
survey_accounts: ['surveyAccountsIndex'],
mri_violations: ['mriViolationsIndex'],
user_accounts: ['userAccountsIndex'],
examiner: ['examinerIndex'],
help_editor: ['help_editor', 'helpEditorForm'],
brainbrowser: ['Brainbrowser'],
imaging_uploader: ['index'],
acknowledgements: ['acknowledgementsIndex'],
new_profile: ['NewProfileIndex'],
module_manager: ['modulemanager'],
imaging_qc: ['imagingQCIndex'],
server_processes_manager: ['server_processes_managerIndex'],
statistics: ['WidgetIndex'],
instruments: ['CandidateInstrumentList', 'ControlpanelDeleteInstrumentData'],
candidate_profile: ['CandidateInfo'],
schedule_module: ['scheduleIndex'],
api_docs: ['swagger-ui_custom'],
dashboard: ['welcome'],
};
/*
* ------------------------------------------------------------
* Check if useEEGBrowserVisualizationComponents is set to TRUE
* If not, protoc compiled file chunk.proto may not exist.
* Deactivate compilation of the EEGBrowserVisualization files
* to avoid import errors and optimize performance
*/
let EEGVisEnabled: boolean | string = false;
if ('EEG_VIS_ENABLED' in process.env) {
EEGVisEnabled = process.env.EEG_VIS_ENABLED ?? false;
} else {
const getConfig = cp.spawnSync('php', [
'tools/get_config.php',
'useEEGBrowserVisualizationComponents',
], {});
try {
EEGVisEnabled = JSON.parse(getConfig.stdout.toString());
} catch (e) {
console.warn(
'\x1b[33m',
'WARNING: Unable to fetch DB config',
'useEEGBrowserVisualizationComponents',
'\x1b[0m',
);
}
}
const optimization = {
minimizer: [
(compiler: webpack.Compiler) => {
new TerserPlugin({
parallel: true,
terserOptions: {
compress: false,
ecma: 2015,
mangle: false,
},
extractComments: false,
}).apply(compiler);
},
],
};
const resolve: webpack.ResolveOptions = {
alias: {
jsx: path.resolve(__dirname, './jsx'),
jslib: path.resolve(__dirname, './jslib'),
Breadcrumbs: path.resolve(__dirname, './jsx/Breadcrumbs'),
DataTable: path.resolve(__dirname, './jsx/DataTable'),
Filter: path.resolve(__dirname, './jsx/Filter'),
FilterableDataTable: path.resolve(__dirname, './jsx/FilterableDataTable'),
FilterForm: path.resolve(__dirname, './jsx/FilterForm'),
Loader: path.resolve(__dirname, './jsx/Loader'),
Modal: path.resolve(__dirname, './jsx/Modal'),
MultiSelectDropdown: path.resolve(__dirname, './jsx/MultiSelectDropdown'),
PaginationLinks: path.resolve(__dirname, './jsx/PaginationLinks'),
Panel: path.resolve(__dirname, './jsx/Panel'),
ProgressBar: path.resolve(__dirname, './jsx/ProgressBar'),
StaticDataTable: path.resolve(__dirname, './jsx/StaticDataTable'),
Tabs: path.resolve(__dirname, './jsx/Tabs'),
TriggerableModal: path.resolve(__dirname, './jsx/TriggerableModal'),
Card: path.resolve(__dirname, './jsx/Card'),
Help: path.resolve(__dirname, './jsx/Help'),
},
extensions: ['*', '.js', '.jsx', '.json', '.ts', '.tsx'],
fallback: {
fs: false,
path: false,
},
};
const module: webpack.ModuleOptions = {
rules: [
{
test: /\.(jsx?|tsx?)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader?cacheDirectory',
},
],
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
],
},
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {onlyCompileBundledFiles: true},
},
],
},
],
};
const plugins: webpack.WebpackPluginInstance[] = [];
plugins.push(new CopyPlugin({
patterns: [
{
from: `node_modules/react/umd/${
isDev ? 'react.development.js' : 'react.production.min.js'
}`,
to: 'htdocs/vendor/js/react',
force: true,
},
{
from: `node_modules/react-dom/umd/${
isDev ? 'react-dom.development.js' : 'react-dom.production.min.js'
}`,
to: 'htdocs/vendor/js/react',
force: true,
},
],
}));
plugins.push(new DefinePlugin({
EEG_VIS_ENABLED: EEGVisEnabled,
}));
if (EEGVisEnabled !== 'true' && EEGVisEnabled !== '1' ) {
plugins.push(
new IgnorePlugin({
resourceRegExp: /react-series-data-viewer/,
})
);
}
/**
* Add the project-specific modules and entry points to the record of main
* LORIS modules.
*/
function addProjectModules(
modules: Record<string, string[]>,
): Record<string, string[]> {
if (!fs.existsSync('./project/webpack-project.config.js')) {
return modules;
}
const projectModules: Record<string, string[]>
// eslint-disable-next-line @typescript-eslint/no-var-requires
= require('./project/webpack-project.config.js');
// Copy the record of LORIS modules
const allModules: Record<string, string[]> = {};
for (const [moduleName, moduleEntryPoints] of
Object.entries(projectModules)
) {
allModules[moduleName] = [...moduleEntryPoints];
}
// Add project-specific modules and overrides to the record of modules
for (const [moduleName, moduleEntryPoints] of
Object.entries(projectModules)
) {
if (moduleName in allModules) {
allModules[moduleName].push(...moduleEntryPoints);
} else {
allModules[moduleName] = moduleEntryPoints;
}
}
return allModules;
}
/**
* Filter the record of LORIS modules to contain only the target module if a
* target is defined, or return the record unchanged if no target is defined.
*/
function filterTargetModules(
modules: Record<string, string[]>,
): Record<string, string[]> {
// If there is no target module, do not filter the modules
if (!target) {
// eslint-disable-next-line no-console
console.log('Building all modules');
return modules;
}
// Exit if the target module is not found in the list of modules
if (!(target in modules)) {
console.error(`Target module \''${target}'\' not found`);
process.exit(1);
}
// Return a record containing only the target module files
// eslint-disable-next-line no-console
console.log(`Building module \'${target}\'`);
return {
[target]: modules[target],
};
}
/**
* Get the Webpack entries of a given module, with each entry being mapped to
* its name.
*/
function getModuleEntries(
moduleName: string,
moduleEntryPoints: string[],
): Record<string, webpack.EntryOptions>[] {
// Check if a project override exists for the module.
const basePath = fs.existsSync(`./project/modules/${moduleName}`)
? `./project/modules/${moduleName}/`
: `./modules/${moduleName}/`;
return moduleEntryPoints.map((moduleEntryPoint) => ({
[moduleName + '/' + moduleEntryPoint]: {
import: basePath + 'jsx/' + moduleEntryPoint,
filename: basePath + 'js/' + moduleEntryPoint + '.js',
library: {
name: ['lorisjs', moduleName, moduleEntryPoint],
type: 'window',
},
},
}));
}
/**
* Get the Webpack entries of the LORIS modules to build, with each entry being
* mapped to its name.
*/
function getModulesEntries(): Record<string, webpack.EntryOptions> {
const allModules = addProjectModules(lorisModules);
const targetModules = filterTargetModules(allModules);
const moduleEntries = Object.entries(targetModules)
.map(([moduleName, moduleEntryPoints]) =>
getModuleEntries(moduleName, moduleEntryPoints)
)
.flat();
return Object.assign({}, ...moduleEntries);
}
const configs: webpack.Configuration[] = [];
configs.push({
entry: {
PaginationLinks: './jsx/PaginationLinks.js',
StaticDataTable: './jsx/StaticDataTable.js',
MultiSelectDropdown: './jsx/MultiSelectDropdown.js',
Breadcrumbs: './jsx/Breadcrumbs.js',
CSSGrid: './jsx/CSSGrid.js',
Help: './jsx/Help.js',
...getModulesEntries(),
},
output: {
path: __dirname,
filename: './htdocs/js/components/[name].js',
library: ['lorisjs', '[name]'],
libraryTarget: 'window',
},
externals: {'react': 'React', 'react-dom': 'ReactDOM'},
devtool: 'source-map',
plugins,
optimization,
resolve,
module,
stats: 'errors-warnings',
});
// HACK: For some reason, the electrophysiology session view only compiles if
// it uses a separate (although possibly identical) configuration.
if (!target || target === 'electrophysiology_browser') {
configs.push({
entry: {
electrophysiology_browser: {
import: './modules/electrophysiology_browser/'
+ 'jsx/electrophysiologySessionView',
filename: './modules/electrophysiology_browser/'
+ 'js/electrophysiologySessionView.js',
library: {
name: [
'lorisjs',
'electrophysiology_browser',
'electrophysiologySessionView',
],
type: 'window',
},
},
},
output: {
path: __dirname,
filename: './htdocs/js/components/[name].js',
library: ['lorisjs', '[name]'],
libraryTarget: 'window',
},
externals: {'react': 'React', 'react-dom': 'ReactDOM'},
devtool: 'source-map',
plugins,
optimization,
resolve,
module,
stats: 'errors-warnings',
});
}
export default configs;