From 64ceffa102f8512e9d6a19501f97a44efe3056ce Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 10 Sep 2019 16:39:22 -0700 Subject: [PATCH 1/2] [optimize/bundleContext] do not include absolute urls or windows slashes --- .../ui/ui_bundles/ui_bundles_controller.js | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/legacy/ui/ui_bundles/ui_bundles_controller.js b/src/legacy/ui/ui_bundles/ui_bundles_controller.js index 5e9ae0abbb183..a34c40d29e302 100644 --- a/src/legacy/ui/ui_bundles/ui_bundles_controller.js +++ b/src/legacy/ui/ui_bundles/ui_bundles_controller.js @@ -17,7 +17,7 @@ * under the License. */ -import { resolve } from 'path'; +import { resolve, relative, isAbsolute } from 'path'; import { createHash } from 'crypto'; import { promisify } from 'util'; import { existsSync } from 'fs'; @@ -27,12 +27,13 @@ import { makeRe } from 'minimatch'; import mkdirp from 'mkdirp'; import jsonStableStringify from 'json-stable-stringify'; -import { IS_KIBANA_DISTRIBUTABLE } from '../../utils'; +import { IS_KIBANA_DISTRIBUTABLE, fromRoot } from '../../utils'; import { UiBundle } from './ui_bundle'; import { appEntryTemplate } from './app_entry_template'; const mkdirpAsync = promisify(mkdirp); +const REPO_ROOT = fromRoot(); function getWebpackAliases(pluginSpecs) { return pluginSpecs.reduce((aliases, spec) => { @@ -49,16 +50,26 @@ function getWebpackAliases(pluginSpecs) { }, {}); } -function sortAllArrays(input) { +// Recursively clone appExtensions, sorting array and normalizing absolute paths +function stableCloneAppExtensions(input) { if (Array.isArray(input)) { return input - .map(i => sortAllArrays(i)) + .map(i => stableCloneAppExtensions(i)) .sort((a, b) => typeof a === 'string' && typeof b === 'string' ? a.localeCompare(b) : 0); } - if (typeof input === 'object') { - return Object.entries(input) - .map(([key, value]) => [key, sortAllArrays(value)]); + if (input && typeof input === 'object') { + return Object.fromEntries( + Object.entries(input) + .map(([key, value]) => [key, stableCloneAppExtensions(value)]) + ); + } + + if (typeof input === 'string') { + if (isAbsolute(input)) { + input = `absolute:${relative(REPO_ROOT, input)}`; + } + return input.replace(/\\/g, '/'); } return input; @@ -75,7 +86,7 @@ export class UiBundlesController { sourceMaps: config.get('optimize.sourceMaps'), kbnVersion: config.get('pkg.version'), buildNum: config.get('pkg.buildNum'), - appExtensions: sortAllArrays(uiExports.appExtensions), + appExtensions: stableCloneAppExtensions(uiExports.appExtensions), }; this._filter = makeRe(config.get('optimize.bundleFilter') || '*', { From a02531c35b3231adabd7db65bfad7e72f9f6f4e9 Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 10 Sep 2019 16:55:55 -0700 Subject: [PATCH 2/2] simplify the stable clone fn, it doesn't need to be generic --- .../ui/ui_bundles/ui_bundles_controller.js | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/legacy/ui/ui_bundles/ui_bundles_controller.js b/src/legacy/ui/ui_bundles/ui_bundles_controller.js index a34c40d29e302..f0410f57c4578 100644 --- a/src/legacy/ui/ui_bundles/ui_bundles_controller.js +++ b/src/legacy/ui/ui_bundles/ui_bundles_controller.js @@ -51,28 +51,20 @@ function getWebpackAliases(pluginSpecs) { } // Recursively clone appExtensions, sorting array and normalizing absolute paths -function stableCloneAppExtensions(input) { - if (Array.isArray(input)) { - return input - .map(i => stableCloneAppExtensions(i)) - .sort((a, b) => typeof a === 'string' && typeof b === 'string' ? a.localeCompare(b) : 0); - } - - if (input && typeof input === 'object') { - return Object.fromEntries( - Object.entries(input) - .map(([key, value]) => [key, stableCloneAppExtensions(value)]) - ); - } - - if (typeof input === 'string') { - if (isAbsolute(input)) { - input = `absolute:${relative(REPO_ROOT, input)}`; - } - return input.replace(/\\/g, '/'); - } - - return input; +function stableCloneAppExtensions(appExtensions) { + return Object.fromEntries( + Object.entries(appExtensions).map(([extensionType, moduleIds]) => [ + extensionType, + moduleIds + .map(moduleId => { + if (isAbsolute(moduleId)) { + moduleId = `absolute:${relative(REPO_ROOT, moduleId)}`; + } + return moduleId.replace(/\\/g, '/'); + }) + .sort((a, b) => a.localeCompare(b)) + ]) + ); } export class UiBundlesController {