From 5b14d95ed5ce92f96f3b646b9a03e678fab3cbef Mon Sep 17 00:00:00 2001 From: Tyler Smalley Date: Wed, 13 May 2020 17:38:18 -0700 Subject: [PATCH 1/4] [build] Removes commonjs transforms Signed-off-by: Tyler Smalley --- 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 | 60 ++++++++++++------- .../core_plugins/timelion/public/app.js | 5 +- src/optimize/base_optimizer.js | 10 +++- src/optimize/create_ui_exports_module.js | 4 +- .../public/application/angular/context_app.js | 4 +- yarn.lock | 2 +- 14 files changed, 72 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 72a63ec34d2c7..a5eee01112d6d 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 1a2f6941c2020..c8186b95d7817 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 2c1129f275bfe..97462a579e3c4 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 8f1e315cab389..ef30f59d60c25 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 b3c6bbe7befbb..ff30934aad6d1 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 875cbcde9d0e1..309b3d5b3233d 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 dee35043211f7..5528511e2f566 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 fe0f75c05c646..8bf5eb72523ff 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, { @@ -41,6 +43,19 @@ beforeAll(async () => { parents: true, deep: true, }); + + 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); + }, + }, + }); }); afterAll(async () => { @@ -52,23 +67,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 +128,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(); @@ -195,11 +191,35 @@ it('uses cache on second run and exist cleanly', async () => { "initializing", "initializing", "initialized", + "initialized", + "running", + "running", + "running", "success", ] `); }); +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 80ffa440e7285..6c1dc81dc341b 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 a94f251c292f9..9d3a834b455db 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 265f68c4fd7fa..2ab19d71e1f1c 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/discover/public/application/angular/context_app.js b/src/plugins/discover/public/application/angular/context_app.js index e79689d3e26e3..acb94bf97d21f 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/yarn.lock b/yarn.lock index a18f89cd480f8..1a6309148ff7f 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== From b4b9869bb93d74cb47dd64b157487684d166f654 Mon Sep 17 00:00:00 2001 From: spalger Date: Wed, 20 May 2020 13:29:49 -0700 Subject: [PATCH 2/4] avoid separating definition and assignment --- .../basic_optimization.test.ts | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts b/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts index 470fac3dbd5be..fa44aab1a38ef 100644 --- a/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts +++ b/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts @@ -34,7 +34,18 @@ const MOCK_REPO_DIR = Path.resolve(TMP_DIR, 'mock_repo'); expect.addSnapshotSerializer(createAbsolutePathSerializer(REPO_ROOT)); -let log: ToolingLog; +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); + }, + }, +}); beforeAll(async () => { await del(TMP_DIR); @@ -43,19 +54,6 @@ beforeAll(async () => { parents: true, deep: true, }); - - 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); - }, - }, - }); }); afterAll(async () => { From 776facd6952c1f53b3c78eeb440e5bc146d83afe Mon Sep 17 00:00:00 2001 From: spalger Date: Wed, 20 May 2020 13:30:20 -0700 Subject: [PATCH 3/4] run with dist:false so the test properly verified the cache is used --- .../src/integration_tests/basic_optimization.test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts b/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts index fa44aab1a38ef..ff9addbb3172e 100644 --- a/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts +++ b/packages/kbn-optimizer/src/integration_tests/basic_optimization.test.ts @@ -168,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) @@ -189,10 +189,6 @@ it('uses cache on second run and exist cleanly', async () => { "initializing", "initializing", "initialized", - "initialized", - "running", - "running", - "running", "success", ] `); From 54273b7a26c37bbd29fd8f363b50d706e859e7a8 Mon Sep 17 00:00:00 2001 From: Tyler Smalley Date: Wed, 20 May 2020 22:28:05 -0700 Subject: [PATCH 4/4] Updates APM import to not import across boundaries Introduced in #66432 and I have a more thorough PR to prevent imports in server from public in #67149 Signed-off-by: Tyler Smalley --- src/plugins/apm_oss/server/index.ts | 2 ++ .../apm/server/lib/index_pattern/create_static_index_pattern.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/apm_oss/server/index.ts b/src/plugins/apm_oss/server/index.ts index d2e7321f39e7b..8a7e97e4ebc5c 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/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 2e9087b238406..acb2bffeda758 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';