From 8fc0fcacc01cc3f5138f7df8cdba35952c523eec Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 22 Jun 2018 14:44:36 -0700 Subject: [PATCH] [optimizer] allow prod optimizations when running source (#20174) --- src/optimize/base_optimizer.js | 105 ++++++++++++--------- src/ui/ui_bundles/ui_bundles_controller.js | 4 + 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/src/optimize/base_optimizer.js b/src/optimize/base_optimizer.js index 71193cf75c57b..9026925099c63 100644 --- a/src/optimize/base_optimizer.js +++ b/src/optimize/base_optimizer.js @@ -286,58 +286,59 @@ export default class BaseOptimizer { }, }; - if (!IS_KIBANA_DISTRIBUTABLE) { - return webpackMerge(commonConfig, { - module: { - rules: [ - { - resource: createSourceFileResourceSelector(/\.tsx?$/), - use: maybeAddCacheLoader('typescript', [ - { - loader: 'ts-loader', - options: { - transpileOnly: true, - experimentalWatchApi: true, - onlyCompileBundledFiles: true, - compilerOptions: { - sourceMap: Boolean(this.sourceMaps), - target: 'es5', - module: 'esnext', - } + // we transpile typescript in the optimizer unless we are running the distributable + const transpileTsConfig = { + module: { + rules: [ + { + resource: createSourceFileResourceSelector(/\.tsx?$/), + use: maybeAddCacheLoader('typescript', [ + { + loader: 'ts-loader', + options: { + transpileOnly: true, + experimentalWatchApi: true, + onlyCompileBundledFiles: true, + compilerOptions: { + sourceMap: Boolean(this.sourceMaps), + target: 'es5', + module: 'esnext', } } - ]), - } - ] - }, + } + ]), + } + ] + }, - 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/ - }, + 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'], - }, + resolve: { + extensions: ['.ts', '.tsx'], + }, + }; - // In the test env we need to add react-addons (and a few other bits) for the - // enzyme tests to work. - // https://github.com/airbnb/enzyme/blob/master/docs/guides/webpack.md - externals: { - 'mocha': 'mocha', - 'react/lib/ExecutionEnvironment': true, - 'react/addons': true, - 'react/lib/ReactContext': true, - } - }); - } + // 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, + } + }; - return webpackMerge(commonConfig, { + // in production we set the process.env.NODE_ENV and uglify our bundles + const productionConfig = { plugins: [ new webpack.DefinePlugin({ 'process.env': { @@ -352,7 +353,17 @@ export default class BaseOptimizer { mangle: false }), ] - }); + }; + + return webpackMerge( + commonConfig, + IS_KIBANA_DISTRIBUTABLE + ? {} + : transpileTsConfig, + this.uiBundles.isDevMode() + ? supportEnzymeConfig + : productionConfig + ); } failedStatsToError(stats) { diff --git a/src/ui/ui_bundles/ui_bundles_controller.js b/src/ui/ui_bundles/ui_bundles_controller.js index 5eb2eb1d582c4..3a4b1385da4af 100644 --- a/src/ui/ui_bundles/ui_bundles_controller.js +++ b/src/ui/ui_bundles/ui_bundles_controller.js @@ -100,6 +100,10 @@ export class UiBundlesController { } } + isDevMode() { + return this._env === 'development'; + } + getWebpackPluginProviders() { return this._webpackPluginProviders || []; }