-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
395 lines (372 loc) · 12.2 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
/* *****************************************************************************
* Caleydo - Visualization for Molecular Biology - http://caleydo.org
* Copyright (c) The Caleydo Team. All rights reserved.
* Licensed under the new BSD license, available at http://caleydo.org/license
**************************************************************************** */
const {libraryAliases, libraryExternals, modules, entries, ignores, type, registry, vendor} = require('./.yo-rc.json')['generator-phovea'];
const resolve = require('path').resolve;
const pkg = require('./package.json');
const webpack = require('webpack');
const fs = require('fs');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const buildInfo = require('./buildInfo.js');
const now = new Date();
const prefix = (n) => n < 10 ? ('0' + n) : n.toString();
const buildId = `${now.getUTCFullYear()}${prefix(now.getUTCMonth() + 1)}${prefix(now.getUTCDate())}-${prefix(now.getUTCHours())}${prefix(now.getUTCMinutes())}${prefix(now.getUTCSeconds())}`;
pkg.version = pkg.version.replace('SNAPSHOT', buildId);
const year = (new Date()).getFullYear();
const banner = '/*! ' + (pkg.title || pkg.name) + ' - v' + pkg.version + ' - ' + year + '\n' +
(pkg.homepage ? '* ' + pkg.homepage + '\n' : '') +
'* Copyright (c) ' + year + ' ' + pkg.author.name + ';' +
' Licensed ' + pkg.license + '*/\n';
const preCompilerFlags = {flags: (registry || {}).flags || {}};
const includeFeature = registry ? (extension, id) => {
const exclude = registry.exclude || [];
const include = registry.include || [];
if (!exclude && !include) {
return true;
}
const test = (f) => Array.isArray(f) ? extension.match(f[0]) && (id || '').match(f[1]) : extension.match(f);
return include.every(test) && !exclude.some(test);
} : () => true;
const tsLoader = [
{
loader: 'awesome-typescript-loader'
}
];
const tsLoaderDev = [
{loader: 'cache-loader'},
{
loader: 'thread-loader',
options: {
// there should be 1 cpu for the fork-ts-checker-webpack-plugin
workers: require('os').cpus().length - 1
}
},
{
loader: 'ts-loader',
options: {
happyPackMode: true, // IMPORTANT! use happyPackMode mode to speed-up compilation and reduce errors reported to webpack,
compilerOptions: {
target: 'es6',
jsx: 'react',
jsxFactory: 'h',
}
}
}
];
// list of loaders and their mappings
const webpackloaders = [
{test: /\.scss$/, use: 'style-loader!css-loader!sass-loader'},
{test: /\.css$/, use: 'style-loader!css-loader'},
{test: /\.tsx?$/, use: tsLoader},
{
test: /phovea(_registry)?\.js$/, use: [{
loader: 'ifdef-loader',
options: Object.assign({include: includeFeature}, preCompilerFlags)
}]
},
{test: /\.json$/, use: 'json-loader'},
{
test: /\.(png|jpg|gif|webp)$/,
loader: 'url-loader',
options: {
limit: 10000 // inline <= 10kb
}
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader',
options: {
limit: 10000, // inline <= 10kb
mimetype: 'application/font-woff'
}
},
{
test: /\.svg(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader',
options: {
limit: 10000, // inline <= 10kb
mimetype: 'image/svg+xml'
}
},
{test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader'}
];
/**
* tests whether the given phovea module name is matching the requested file and if so convert it to an external lookup
* depending on the loading type
*/
function testPhoveaModule(moduleName, request) {
if (!(new RegExp('^' + moduleName + '/src.*')).test(request)) {
return false;
}
const subModule = request.match(/.*\/src\/?(.*)/)[1];
// skip empty modules = root
const path = subModule === '' ? [moduleName] : [moduleName, subModule];
// phovea_<name> ... phovea.name
const rootPath = /phovea_.*/.test(moduleName) ? ['phovea', moduleName.slice(7)].concat(path.slice(1)) : path;
return {
root: rootPath,
commonjs2: path,
commonjs: path,
amd: request + (subModule === '' ? '/main' : '')
};
}
function testPhoveaModules(modules) {
return (context, request, callback) => {
for (let i = 0; i < modules.length; ++i) {
const r = testPhoveaModule(modules[i], request);
if (r) {
return callback(null, r);
}
}
callback();
};
}
// use workspace registry file if available
const isWorkspaceContext = fs.existsSync(resolve(__dirname, '..', 'phovea_registry.js'));
const registryFile = isWorkspaceContext ? '../phovea_registry.js' : './phovea_registry.js';
const actMetaData = `file-loader?name=phoveaMetaData.json!${buildInfo.metaDataTmpFile(pkg)}`;
const actBuildInfoFile = `file-loader?name=buildInfo.json!${buildInfo.tmpFile()}`;
/**
* inject the registry to be included
* @param entry
* @returns {*}
*/
function injectRegistry(entry) {
const extraFiles = [registryFile, actBuildInfoFile, actMetaData];
// build also the registry
if (typeof entry === 'string') {
return extraFiles.concat(entry);
}
const transformed = {};
Object.keys(entry).forEach((eentry) => {
transformed[eentry] = extraFiles.concat(entry[eentry]);
});
return transformed;
}
/**
* generate a webpack configuration
*/
function generateWebpack(options) {
let base = {
entry: injectRegistry(options.entries),
output: {
path: resolve(__dirname, 'build'),
filename: (options.name || (pkg.name + (options.bundle ? '_bundle' : ''))) + (options.min && !options.nosuffix ? '.min' : '') + '.js',
chunkFilename: '[chunkhash].js',
publicPath: '' // no public path = relative
},
resolve: {
// add `.ts` and `.tsx` as a resolvable extension.
extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js'],
alias: Object.assign({}, options.libs || {}),
symlinks: false,
// fallback to the directory above if they are siblings just in the workspace context
modules: isWorkspaceContext ? [
resolve(__dirname, '../'),
'node_modules'
] : ['node_modules']
},
plugins: [
// define magic constants that are replaced
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(options.isProduction ? 'production' : 'development'),
__VERSION__: JSON.stringify(pkg.version),
__LICENSE__: JSON.stringify(pkg.license),
__BUILD_ID__: buildId,
__DEBUG__: options.isDev || options.isTest,
__TEST__: options.isTest,
__PRODUCTION__: options.isProduction,
__APP_CONTEXT__: JSON.stringify('/')
})
// rest depends on type
],
externals: [],
module: {
loaders: webpackloaders.slice()
},
devServer: {
proxy: {
'/api/*': {
target: 'http://localhost:9000',
secure: false,
ws: true
},
'/login': {
target: 'http://localhost:9000',
secure: false
},
'/logout': {
target: 'http://localhost:9000',
secure: false
},
'/loggedinas': {
target: 'http://localhost:9000',
secure: false
}
},
contentBase: resolve(__dirname, 'build'),
watchOptions: {
aggregateTimeout: 500,
ignored: /node_modules/
}
},
watchOptions: {
aggregateTimeout: 500,
ignored: /node_modules/
}
};
if (options.isProduction) {
base.plugins.unshift(new webpack.BannerPlugin({
banner: banner,
raw: true
}));
base.plugins.push(new webpack.optimize.MinChunkSizePlugin({
minChunkSize: 10000 // at least 10.000 characters
}));
// base.plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
} else if (options.isDev) {
// switch to def settings
base.module.loaders.find((d) => d.use === tsLoader).use = tsLoaderDev;
base.plugins.push(new ForkTsCheckerWebpackPlugin({checkSyntacticErrors: true, tsconfig: './tsconfig_dev.json'}));
}
if (options.library) {
let libName = /phovea_.*/.test(pkg.name) ? ['phovea', pkg.name.slice(7)] : pkg.name;
// generate a library, i.e. output the last entry element
// create library name
if (options.moduleBundle) {
libName = 'phovea';
}
base.output.library = libName;
base.output.libraryTarget = 'umd';
base.output.umdNamedDefine = false; // anonymous require module
}
if (!options.bundle) {
// if we don't bundle don't include external libraries and other phovea modules
base.externals.push(...(options.externals || Object.keys(options.libs || {})));
// ignore all phovea modules
if (options.modules) {
base.externals.push(testPhoveaModules(options.modules));
}
// ignore extra modules
(options.ignore || []).forEach(function (d) {
base.module.loaders.push({test: new RegExp(d), loader: 'null-loader'}); // use null loader
});
// ingore phovea module registry calls
(options.modules || []).forEach(function (m) {
base.module.loaders.push({
test: new RegExp('.*[\\\\/]' + m + '[\\\\/]phovea_registry.js'),
loader: 'null-loader'
}); // use null loader
});
}
if (!options.bundle || options.isApp) {
// extract the included css file to own file
const p = new ExtractTextPlugin({
filename: (options.isApp || options.moduleBundle ? 'style' : pkg.name) + (options.min && !options.nosuffix ? '.min' : '') + '.css',
allChunks: true // there seems to be a bug in dynamically loaded chunk styles are not loaded, workaround: extract all styles from all chunks
});
base.plugins.push(p);
base.module.loaders[0] = {
test: /\.scss$/,
loader: p.extract(['css-loader', 'sass-loader'])
};
base.module.loaders[1] = {
test: /\.css$/,
loader: p.extract(['css-loader'])
};
}
if (options.isApp) {
// create manifest
// base.plugins.push(new webpack.optimize.AppCachePlugin());
}
if (options.commons) {
// build a commons plugin
base.plugins.push(new webpack.optimize.CommonsChunkPlugin({
// The order of this array matters
name: 'common',
filename: 'common.js',
minChunks: 2
}));
}
if (options.vendor) {
(Array.isArray(options.vendor) ? options.vendor : [options.vendor]).forEach((reg) => {
base.plugins.push(new webpack.optimize.CommonsChunkPlugin({
async: true,
children: true,
deepChildren: true,
minChunks: (module, count) => new RegExp(reg, 'i').test(module.resource) && count >= 2
}));
});
}
if (options.min) {
// use a minifier
base.plugins.push(
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.optimize.UglifyJsPlugin());
} else {
// generate source maps
base.devtool = 'inline-source-map';
}
return base;
}
function generateWebpackConfig(env) {
const isTest = env === 'test';
const isProduction = env === 'prod';
const isDev = !isProduction && !isTest;
const base = {
entries: entries,
libs: libraryAliases,
externals: libraryExternals,
modules: modules,
vendor: vendor,
ignore: ignores,
isProduction: isProduction,
isDev: isDev,
isTest: isTest
};
if (isTest) {
return generateWebpack(Object.assign({}, base, {
bundle: true
}));
}
if (type.startsWith('app')) {
base.isApp = true;
base.bundle = true; // bundle everything together
base.name = '[name]'; // multiple entries case
base.commons = true; // extract commons module
} else if (type === 'bundle') {
base.library = true; // expose as library
base.moduleBundle = true; // expose as library 'phovea'
base.name = pkg.name; // to avoid adding _bundle
base.bundle = true;
} else { // type === 'lib'
base.library = true;
}
// single generation
if (isDev) {
return generateWebpack(base);
}
if (type.startsWith('app')) { // isProduction app
return generateWebpack(Object.assign({}, base, {
min: true,
nosuffix: true
}));
}
// isProduction
return [
// plain
generateWebpack(base),
// minified
generateWebpack(Object.assign({}, base, {
min: true
}))
];
}
module.exports = generateWebpackConfig;
module.exports.generateWebpack = generateWebpack;