From c675301a871d8efc825cdddc90bd97a18af0321c Mon Sep 17 00:00:00 2001 From: Tyler Smalley Date: Thu, 21 May 2020 09:43:31 -0700 Subject: [PATCH] [build] Removes commonjs transforms (#66506) Signed-off-by: Tyler Smalley Co-authored-by: spalger Co-authored-by: Elastic Machine --- package.json | 1 + packages/kbn-babel-preset/package.json | 2 - packages/kbn-babel-preset/webpack_preset.js | 2 - packages/kbn-i18n/src/angular/index.ts | 7 ++- packages/kbn-i18n/src/react/index.tsx | 4 +- packages/kbn-interpreter/.babelrc | 1 + packages/kbn-interpreter/package.json | 1 + .../basic_optimization.test.ts.snap | 10 ++-- .../basic_optimization.test.ts | 56 ++++++++++++------- .../core_plugins/timelion/public/app.js | 5 +- src/optimize/base_optimizer.js | 10 +++- src/optimize/create_ui_exports_module.js | 4 +- src/plugins/apm_oss/server/index.ts | 2 + .../public/application/angular/context_app.js | 4 +- .../create_static_index_pattern.ts | 2 +- yarn.lock | 2 +- 16 files changed, 70 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index 634720d6d3a2..b13de672028c 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,7 @@ }, "dependencies": { "@babel/core": "^7.9.0", + "@babel/plugin-transform-modules-commonjs": "^7.9.0", "@babel/register": "^7.9.0", "@elastic/apm-rum": "^5.1.1", "@elastic/charts": "19.2.0", diff --git a/packages/kbn-babel-preset/package.json b/packages/kbn-babel-preset/package.json index ba0c2489cdbc..ab0d875430d0 100644 --- a/packages/kbn-babel-preset/package.json +++ b/packages/kbn-babel-preset/package.json @@ -7,8 +7,6 @@ "@babel/plugin-proposal-class-properties": "^7.8.3", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3", "@babel/plugin-proposal-optional-chaining": "^7.9.0", - "@babel/plugin-syntax-dynamic-import": "^7.8.3", - "@babel/plugin-transform-modules-commonjs": "^7.9.0", "@babel/preset-env": "^7.9.0", "@babel/preset-react": "^7.9.1", "@babel/preset-typescript": "^7.9.0", diff --git a/packages/kbn-babel-preset/webpack_preset.js b/packages/kbn-babel-preset/webpack_preset.js index 2c1129f275bf..97462a579e3c 100644 --- a/packages/kbn-babel-preset/webpack_preset.js +++ b/packages/kbn-babel-preset/webpack_preset.js @@ -33,8 +33,6 @@ module.exports = () => { require('./common_preset'), ], plugins: [ - require.resolve('@babel/plugin-transform-modules-commonjs'), - require.resolve('@babel/plugin-syntax-dynamic-import'), [ require.resolve('babel-plugin-styled-components'), { diff --git a/packages/kbn-i18n/src/angular/index.ts b/packages/kbn-i18n/src/angular/index.ts index 8f1e315cab38..ef30f59d60c2 100644 --- a/packages/kbn-i18n/src/angular/index.ts +++ b/packages/kbn-i18n/src/angular/index.ts @@ -17,6 +17,11 @@ * under the License. */ -export { I18nProvider, I18nServiceType } from './provider'; +export { I18nProvider } from './provider'; + export { i18nFilter } from './filter'; export { i18nDirective } from './directive'; + +// re-export types: https://github.com/babel/babel-loader/issues/603 +import { I18nServiceType as _I18nServiceType } from './provider'; +export type I18nServiceType = _I18nServiceType; diff --git a/packages/kbn-i18n/src/react/index.tsx b/packages/kbn-i18n/src/react/index.tsx index b3c6bbe7befb..ff30934aad6d 100644 --- a/packages/kbn-i18n/src/react/index.tsx +++ b/packages/kbn-i18n/src/react/index.tsx @@ -17,9 +17,11 @@ * under the License. */ +import { InjectedIntl as _InjectedIntl } from 'react-intl'; +export type InjectedIntl = _InjectedIntl; + export { intlShape, - InjectedIntl, FormattedDate, FormattedTime, FormattedRelative, diff --git a/packages/kbn-interpreter/.babelrc b/packages/kbn-interpreter/.babelrc index 875cbcde9d0e..309b3d5b3233 100644 --- a/packages/kbn-interpreter/.babelrc +++ b/packages/kbn-interpreter/.babelrc @@ -1,6 +1,7 @@ { "presets": ["@kbn/babel-preset/webpack_preset"], "plugins": [ + "@babel/plugin-transform-modules-commonjs", ["@babel/plugin-transform-runtime", { "regenerator": true }] diff --git a/packages/kbn-interpreter/package.json b/packages/kbn-interpreter/package.json index dee35043211f..5528511e2f56 100644 --- a/packages/kbn-interpreter/package.json +++ b/packages/kbn-interpreter/package.json @@ -18,6 +18,7 @@ "devDependencies": { "@babel/cli": "^7.8.4", "@babel/core": "^7.9.0", + "@babel/plugin-transform-modules-commonjs": "^7.9.0", "@babel/plugin-transform-runtime": "^7.9.0", "@kbn/babel-preset": "1.0.0", "@kbn/dev-utils": "1.0.0", diff --git a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap index fe0f75c05c64..8bf5eb72523f 100644 --- a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap +++ b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap @@ -1,7 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`builds expected bundles, saves bundle counts to metadata: 1 async bundle 1`] = `"(window[\\"foo_bundle_jsonpfunction\\"]=window[\\"foo_bundle_jsonpfunction\\"]||[]).push([[1],{3:function(module,exports,__webpack_require__){\\"use strict\\";Object.defineProperty(exports,\\"__esModule\\",{value:true});exports.foo=foo;function foo(){}}}]);"`; - exports[`builds expected bundles, saves bundle counts to metadata: OptimizerConfig 1`] = ` OptimizerConfig { "bundles": Array [ @@ -31,7 +29,7 @@ OptimizerConfig { }, ], "cache": true, - "dist": true, + "dist": false, "inspectWorkers": false, "maxWorkerCount": 1, "plugins": Array [ @@ -57,6 +55,8 @@ OptimizerConfig { } `; -exports[`builds expected bundles, saves bundle counts to metadata: bar bundle 1`] = `"var __kbnBundles__=typeof __kbnBundles__===\\"object\\"?__kbnBundles__:{};__kbnBundles__[\\"plugin/bar\\"]=function(modules){function webpackJsonpCallback(data){var chunkIds=data[0];var moreModules=data[1];var moduleId,chunkId,i=0,resolves=[];for(;i { await del(TMP_DIR); await cpy('**/*', MOCK_REPO_DIR, { @@ -52,23 +65,11 @@ it('builds expected bundles, saves bundle counts to metadata', async () => { repoRoot: MOCK_REPO_DIR, pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')], maxWorkerCount: 1, - dist: true, + dist: false, }); expect(config).toMatchSnapshot('OptimizerConfig'); - const log = new ToolingLog({ - level: 'error', - writeTo: { - write(chunk) { - if (chunk.endsWith('\n')) { - chunk = chunk.slice(0, -1); - } - // eslint-disable-next-line no-console - console.error(chunk); - }, - }, - }); const msgs = await runOptimizer(config) .pipe(logOptimizerState(log, config), toArray()) .toPromise(); @@ -125,13 +126,6 @@ it('builds expected bundles, saves bundle counts to metadata', async () => { ); assert('produce zero unexpected states', otherStates.length === 0, otherStates); - expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle'); - expectFileMatchesSnapshotWithCompression( - 'plugins/foo/target/public/1.plugin.js', - '1 async bundle' - ); - expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle'); - const foo = config.bundles.find(b => b.id === 'foo')!; expect(foo).toBeTruthy(); foo.cache.refresh(); @@ -174,7 +168,7 @@ it('uses cache on second run and exist cleanly', async () => { repoRoot: MOCK_REPO_DIR, pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')], maxWorkerCount: 1, - dist: true, + dist: false, }); const msgs = await runOptimizer(config) @@ -200,6 +194,26 @@ it('uses cache on second run and exist cleanly', async () => { `); }); +it('prepares assets for distribution', async () => { + const config = OptimizerConfig.create({ + repoRoot: MOCK_REPO_DIR, + pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')], + maxWorkerCount: 1, + dist: true, + }); + + await runOptimizer(config) + .pipe(logOptimizerState(log, config), toArray()) + .toPromise(); + + expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle'); + expectFileMatchesSnapshotWithCompression( + 'plugins/foo/target/public/1.plugin.js', + '1 async bundle' + ); + expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle'); +}); + /** * Verifies that the file matches the expected output and has matching compressed variants. */ diff --git a/src/legacy/core_plugins/timelion/public/app.js b/src/legacy/core_plugins/timelion/public/app.js index 80ffa440e728..6c1dc81dc341 100644 --- a/src/legacy/core_plugins/timelion/public/app.js +++ b/src/legacy/core_plugins/timelion/public/app.js @@ -23,6 +23,7 @@ import 'angular-sanitize'; import { i18n } from '@kbn/i18n'; +import routes from 'ui/routes'; import { capabilities } from 'ui/capabilities'; import { docTitle } from 'ui/doc_title'; import { fatalError, toastNotifications } from 'ui/notify'; @@ -62,9 +63,9 @@ document.title = 'Timelion - Kibana'; const app = require('ui/modules').get('apps/timelion', ['i18n', 'ngSanitize']); -require('ui/routes').enable(); +routes.enable(); -require('ui/routes').when('/:id?', { +routes.when('/:id?', { template: rootTemplate, reloadOnSearch: false, k7Breadcrumbs: ($injector, $route) => diff --git a/src/optimize/base_optimizer.js b/src/optimize/base_optimizer.js index a94f251c292f..9d3a834b455d 100644 --- a/src/optimize/base_optimizer.js +++ b/src/optimize/base_optimizer.js @@ -49,6 +49,10 @@ const STATS_WARNINGS_FILTER = new RegExp( ); const IS_CODE_COVERAGE = !!process.env.CODE_COVERAGE; +const LEGACY_PRESETS = { + plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')], +}; + function recursiveIssuer(m) { if (m.issuer) { return recursiveIssuer(m.issuer); @@ -123,7 +127,7 @@ export default class BaseOptimizer { } warmupThreadLoaderPool() { - const baseModules = ['babel-loader', BABEL_PRESET_PATH]; + const baseModules = ['babel-loader', BABEL_PRESET_PATH, LEGACY_PRESETS]; threadLoader.warmup( // pool options, like passed to loader options @@ -522,6 +526,8 @@ export default class BaseOptimizer { } getPresets() { - return IS_CODE_COVERAGE ? [ISTANBUL_PRESET_PATH, BABEL_PRESET_PATH] : [BABEL_PRESET_PATH]; + return IS_CODE_COVERAGE + ? [ISTANBUL_PRESET_PATH, BABEL_PRESET_PATH, LEGACY_PRESETS] + : [BABEL_PRESET_PATH, LEGACY_PRESETS]; } } diff --git a/src/optimize/create_ui_exports_module.js b/src/optimize/create_ui_exports_module.js index 265f68c4fd7f..2ab19d71e1f1 100644 --- a/src/optimize/create_ui_exports_module.js +++ b/src/optimize/create_ui_exports_module.js @@ -22,7 +22,7 @@ function normalizePath(path) { return path.replace(/[\\\/]+/g, '/'); } -export default function() { +module.exports = function() { if (!module.id.includes('?')) { throw new Error('create_ui_exports_module loaded without JSON args in module.id'); } @@ -37,4 +37,4 @@ export default function() { return { code: `${comment}\n${requires}\n`, }; -} +}; diff --git a/src/plugins/apm_oss/server/index.ts b/src/plugins/apm_oss/server/index.ts index d2e7321f39e7..8a7e97e4ebc5 100644 --- a/src/plugins/apm_oss/server/index.ts +++ b/src/plugins/apm_oss/server/index.ts @@ -44,6 +44,8 @@ export { APMOSSPluginSetup } from './plugin'; export { apmIndexPattern }; +export { APM_STATIC_INDEX_PATTERN_ID } from '../common/index_pattern_constants'; + export { createNodeAgentInstructions, createDjangoAgentInstructions, diff --git a/src/plugins/discover/public/application/angular/context_app.js b/src/plugins/discover/public/application/angular/context_app.js index e79689d3e26e..acb94bf97d21 100644 --- a/src/plugins/discover/public/application/angular/context_app.js +++ b/src/plugins/discover/public/application/angular/context_app.js @@ -36,9 +36,7 @@ import { } from './context/query'; import { callAfterBindingsWorkaround } from './context/helpers/call_after_bindings_workaround'; -const module = getAngularModule(); - -module.directive('contextApp', function ContextApp() { +getAngularModule().directive('contextApp', function ContextApp() { return { bindToController: true, controller: callAfterBindingsWorkaround(ContextAppController), diff --git a/x-pack/plugins/apm/server/lib/index_pattern/create_static_index_pattern.ts b/x-pack/plugins/apm/server/lib/index_pattern/create_static_index_pattern.ts index 2e9087b23840..acb2bffeda75 100644 --- a/x-pack/plugins/apm/server/lib/index_pattern/create_static_index_pattern.ts +++ b/x-pack/plugins/apm/server/lib/index_pattern/create_static_index_pattern.ts @@ -5,7 +5,7 @@ */ import { SavedObjectsErrorHelpers } from '../../../../../../src/core/server'; import { apmIndexPattern } from '../../../../../../src/plugins/apm_oss/server'; -import { APM_STATIC_INDEX_PATTERN_ID } from '../../../../../../src/plugins/apm_oss/public'; +import { APM_STATIC_INDEX_PATTERN_ID } from '../../../../../../src/plugins/apm_oss/server'; import { hasHistoricalAgentData } from '../services/get_services/has_historical_agent_data'; import { Setup } from '../helpers/setup_request'; import { APMRequestHandlerContext } from '../../routes/typings'; diff --git a/yarn.lock b/yarn.lock index 278649a971a9..b14a8458a109 100644 --- a/yarn.lock +++ b/yarn.lock @@ -468,7 +468,7 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" -"@babel/plugin-syntax-dynamic-import@^7.2.0", "@babel/plugin-syntax-dynamic-import@^7.8.0", "@babel/plugin-syntax-dynamic-import@^7.8.3": +"@babel/plugin-syntax-dynamic-import@^7.2.0", "@babel/plugin-syntax-dynamic-import@^7.8.0": version "7.8.3" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.8.3.tgz#62bf98b2da3cd21d626154fc96ee5b3cb68eacb3" integrity sha512-5gdGbFon+PszYzqs83S3E5mpi7/y/8M9eC90MRTZfduQOYW76ig6SOSPNe41IG5LoP3FGBn2N0RjVDSQiS94kQ==