-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
base_optimizer.js
435 lines (387 loc) · 13.2 KB
/
base_optimizer.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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
*/
import { writeFile } from 'fs';
import Boom from 'boom';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
import webpack from 'webpack';
import Stats from 'webpack/lib/Stats';
import webpackMerge from 'webpack-merge';
import { defaults } from 'lodash';
import { IS_KIBANA_DISTRIBUTABLE, fromRoot } from '../utils';
import { PUBLIC_PATH_PLACEHOLDER } from './public_path_placeholder';
const POSTCSS_CONFIG_PATH = require.resolve('./postcss.config');
const BABEL_PRESET_PATH = require.resolve('@kbn/babel-preset/webpack_preset');
const BABEL_EXCLUDE_RE = [
/[\/\\](webpackShims|node_modules|bower_components)[\/\\]/,
];
export default class BaseOptimizer {
constructor(opts) {
this.uiBundles = opts.uiBundles;
this.profile = opts.profile || false;
switch (opts.sourceMaps) {
case true:
this.sourceMaps = 'source-map';
break;
case 'fast':
this.sourceMaps = 'cheap-module-eval-source-map';
break;
default:
this.sourceMaps = opts.sourceMaps || false;
break;
}
this.unsafeCache = opts.unsafeCache || false;
if (typeof this.unsafeCache === 'string') {
this.unsafeCache = [
new RegExp(this.unsafeCache.slice(1, -1))
];
}
}
async initCompiler() {
if (this.compiler) return this.compiler;
const compilerConfig = this.getConfig();
this.compiler = webpack(compilerConfig);
this.compiler.plugin('done', stats => {
if (!this.profile) return;
const path = this.uiBundles.resolvePath('stats.json');
const content = JSON.stringify(stats.toJson());
writeFile(path, content, function (err) {
if (err) throw err;
});
});
return this.compiler;
}
getConfig() {
function getStyleLoaders(preProcessors = [], postProcessors = []) {
return ExtractTextPlugin.extract({
fallback: {
loader: 'style-loader'
},
use: [
...postProcessors,
{
loader: 'css-loader',
options: {
// importLoaders needs to know the number of loaders that follow this one,
// so we add 1 (for the postcss-loader) to the length of the preProcessors
// array that we merge into this array
importLoaders: 1 + preProcessors.length,
},
},
{
loader: 'postcss-loader',
options: {
config: {
path: POSTCSS_CONFIG_PATH,
},
},
},
...preProcessors,
],
});
}
const nodeModulesPath = fromRoot('node_modules');
/**
* Adds a cache loader if we're running in dev mode. The reason we're not adding
* the cache-loader when running in production mode is that it creates cache
* files in optimize/.cache that are not necessary for distributable versions
* of Kibana and just make compressing and extracting it more difficult.
*/
const maybeAddCacheLoader = (cacheName, loaders) => {
if (IS_KIBANA_DISTRIBUTABLE) {
return loaders;
}
return [
{
loader: 'cache-loader',
options: {
cacheDirectory: this.uiBundles.getCacheDirectory(cacheName)
}
},
...loaders
];
};
/**
* Creates the selection rules for a loader that will only pass for
* source files that are eligible for automatic transpilation.
*/
const createSourceFileResourceSelector = (test) => {
return [
{
test,
exclude: BABEL_EXCLUDE_RE.concat(this.uiBundles.getWebpackNoParseRules()),
},
{
test,
include: /[\/\\]node_modules[\/\\]x-pack[\/\\]/,
exclude: /[\/\\]node_modules[\/\\]x-pack[\/\\]node_modules[\/\\]/,
}
];
};
const commonConfig = {
node: { fs: 'empty' },
context: fromRoot('.'),
entry: this.uiBundles.toWebpackEntries(),
devtool: this.sourceMaps,
profile: this.profile || false,
output: {
path: this.uiBundles.getWorkingDir(),
filename: '[name].bundle.js',
sourceMapFilename: '[file].map',
publicPath: PUBLIC_PATH_PLACEHOLDER,
devtoolModuleFilenameTemplate: '[absolute-resource-path]'
},
plugins: [
new ExtractTextPlugin('[name].style.css', {
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: 'commons.bundle.js',
minChunks: 2,
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.bundle.js',
// only combine node_modules from Kibana
minChunks: module => module.context && module.context.indexOf(nodeModulesPath) !== -1
}),
new webpack.NoEmitOnErrorsPlugin(),
// replace imports for `uiExports/*` modules with a synthetic module
// created by create_ui_exports_module.js
new webpack.NormalModuleReplacementPlugin(/^uiExports\//, (resource) => {
// the map of uiExport types to module ids
const extensions = this.uiBundles.getAppExtensions();
// everything following the first / in the request is
// treated as a type of appExtension
const type = resource.request.slice(resource.request.indexOf('/') + 1);
resource.request = [
// the "val-loader" is used to execute create_ui_exports_module
// and use its return value as the source for the module in the
// bundle. This allows us to bypass writing to the file system
require.resolve('val-loader'),
'!',
require.resolve('./create_ui_exports_module'),
'?',
// this JSON is parsed by create_ui_exports_module and determines
// what require() calls it will execute within the bundle
JSON.stringify({ type, modules: extensions[type] || [] })
].join('');
}),
...this.uiBundles.getWebpackPluginProviders()
.map(provider => provider(webpack)),
],
module: {
rules: [
{
test: /\.less$/,
use: getStyleLoaders(
['less-loader'],
maybeAddCacheLoader('less', [])
),
},
{
test: /\.css$/,
use: getStyleLoaders(),
},
{
test: /\.(html|tmpl)$/,
loader: 'raw-loader'
},
{
test: /\.png$/,
loader: 'url-loader'
},
{
test: /\.(woff|woff2|ttf|eot|svg|ico)(\?|$)/,
loader: 'file-loader'
},
{
resource: createSourceFileResourceSelector(/\.js$/),
use: maybeAddCacheLoader('babel', [
{
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
BABEL_PRESET_PATH,
],
},
}
]),
},
...this.uiBundles.getPostLoaders().map(loader => ({
enforce: 'post',
...loader
})),
],
noParse: this.uiBundles.getWebpackNoParseRules(),
},
resolve: {
extensions: ['.js', '.json'],
mainFields: ['browser', 'browserify', 'main'],
modules: [
'webpackShims',
fromRoot('webpackShims'),
'node_modules',
fromRoot('node_modules'),
],
alias: this.uiBundles.getAliases(),
unsafeCache: this.unsafeCache,
},
};
// when running from the distributable define an environment variable we can use
// to exclude chunks of code, modules, etc.
const isDistributableConfig = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
'IS_KIBANA_DISTRIBUTABLE': `"true"`
}
}),
]
};
// when running from source transpile TypeScript automatically
const getSourceConfig = () => {
// dev/typescript is deleted from the distributable, so only require it if we actually need the source config
const { Project } = require('../dev/typescript');
const browserProject = new Project(fromRoot('tsconfig.browser.json'));
return {
module: {
rules: [
{
resource: createSourceFileResourceSelector(/\.tsx?$/),
use: maybeAddCacheLoader('typescript', [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
onlyCompileBundledFiles: true,
configFile: fromRoot('tsconfig.json'),
compilerOptions: {
...browserProject.config.compilerOptions,
sourceMap: Boolean(this.sourceMaps),
}
}
}
]),
}
]
},
stats: {
// when typescript doesn't do a full type check, as we have the ts-loader
// configured here, it does not have enough information to determine
// whether an imported name is a type or not, so when the name is then
// exported, typescript has no choice but to emit the export. Fortunately,
// the extraneous export should not be harmful, so we just suppress these warnings
// https://github.com/TypeStrong/ts-loader#transpileonly-boolean-defaultfalse
warningsFilter: /export .* was not found in/
},
resolve: {
extensions: ['.ts', '.tsx'],
},
};
};
// We need to add react-addons (and a few other bits) for enzyme to work.
// https://github.com/airbnb/enzyme/blob/master/docs/guides/webpack.md
const supportEnzymeConfig = {
externals: {
'mocha': 'mocha',
'react/lib/ExecutionEnvironment': true,
'react/addons': true,
'react/lib/ReactContext': true,
}
};
const watchingConfig = {
plugins: [
new webpack.WatchIgnorePlugin([
// When our bundle entry files are fresh they cause webpack
// to think they might have changed since the watcher was
// initialized, which triggers a second compilation on startup.
// Since we can't reliably update these files anyway, we can
// just ignore them in the watcher and prevent the extra compilation
/bundles[\/\\].+\.entry\.js/,
])
]
};
// in production we set the process.env.NODE_ENV and uglify our bundles
const productionConfig = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': '"production"'
}
}),
new UglifyJsPlugin({
parallel: true,
sourceMap: false,
uglifyOptions: {
compress: {
// the following is required for dead-code the removal
// check in React DevTools
unused: true,
dead_code: true,
conditionals: true,
evaluate: true,
comparisons: false,
sequences: false,
properties: false,
drop_debugger: false,
booleans: false,
loops: false,
toplevel: false,
top_retain: false,
hoist_funs: false,
if_return: false,
join_vars: false,
collapse_vars: false,
reduce_vars: false,
warnings: false,
negate_iife: false,
keep_fnames: true,
keep_infinity: true,
side_effects: false
},
mangle: false
}
}),
]
};
return webpackMerge(
commonConfig,
IS_KIBANA_DISTRIBUTABLE
? isDistributableConfig
: getSourceConfig(),
this.uiBundles.isDevMode()
? webpackMerge(watchingConfig, supportEnzymeConfig)
: productionConfig
);
}
failedStatsToError(stats) {
const details = stats.toString(defaults(
{ colors: true },
Stats.presetToOptions('minimal')
));
return Boom.internal(
`Optimizations failure.\n${details.split('\n').join('\n ')}\n`,
stats.toJson(Stats.presetToOptions('detailed'))
);
}
}