aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers
diff options
context:
space:
mode:
authorGrégoire Aubert <gregoire.aubert@sonarsource.com>2019-05-23 13:59:45 +0200
committerSonarTech <sonartech@sonarsource.com>2019-05-24 20:21:09 +0200
commitd98ea4bead105ce8f79b2072f45f050d5da37d1e (patch)
tree1416df0086c9159ae7015995f539eefd9d87df23 /server/sonar-web/src/main/js/helpers
parentbcd1bf53c45459ec754270a0c6408e3d68144442 (diff)
downloadsonarqube-d98ea4bead105ce8f79b2072f45f050d5da37d1e.tar.gz
sonarqube-d98ea4bead105ce8f79b2072f45f050d5da37d1e.zip
SONAR-12109 Fix initial vendor bundle size, and re-activate bundle size checking on CI
Diffstat (limited to 'server/sonar-web/src/main/js/helpers')
-rw-r--r--server/sonar-web/src/main/js/helpers/__tests__/extensions-test.ts42
-rw-r--r--server/sonar-web/src/main/js/helpers/__tests__/extensionsHandler-test.ts49
-rw-r--r--server/sonar-web/src/main/js/helpers/extensions.ts37
-rw-r--r--server/sonar-web/src/main/js/helpers/extensionsHandler.ts49
4 files changed, 114 insertions, 63 deletions
diff --git a/server/sonar-web/src/main/js/helpers/__tests__/extensions-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/extensions-test.ts
index 72ece2f775b..182fa0e54d6 100644
--- a/server/sonar-web/src/main/js/helpers/__tests__/extensions-test.ts
+++ b/server/sonar-web/src/main/js/helpers/__tests__/extensions-test.ts
@@ -17,14 +17,8 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import {
- getExtensionFromCache,
- getWebAnalyticsPageHandlerFromCache,
- getExtensionStart,
- installExtensionsHandler,
- installScript,
- installWebAnalyticsHandler
-} from '../extensions';
+import { getExtensionStart, installScript } from '../extensions';
+import { installExtensionsHandler } from '../extensionsHandler';
import exposeLibraries from '../../app/components/extensions/exposeLibraries';
jest.mock('../../app/components/extensions/exposeLibraries', () => ({
@@ -35,30 +29,6 @@ beforeEach(() => {
jest.clearAllMocks();
});
-describe('installExtensionsHandler & extensions.getExtensionFromCache', () => {
- it('should register the global "registerExtension" function and retrieve extension', () => {
- expect((window as any).registerExtension).toBeUndefined();
- installExtensionsHandler();
- expect((window as any).registerExtension).toEqual(expect.any(Function));
-
- const start = jest.fn();
- (window as any).registerExtension('foo', start);
- expect(getExtensionFromCache('foo')).toBe(start);
- });
-});
-
-describe('setWebAnalyticsPageChangeHandler & getWebAnalyticsPageHandlerFromCache', () => {
- it('should register the global "setWebAnalyticsPageChangeHandler" function and retrieve analytics extension', () => {
- expect((window as any).setWebAnalyticsPageChangeHandler).toBeUndefined();
- installWebAnalyticsHandler();
- expect((window as any).setWebAnalyticsPageChangeHandler).toEqual(expect.any(Function));
-
- const pageChange = jest.fn();
- (window as any).setWebAnalyticsPageChangeHandler(pageChange);
- expect(getWebAnalyticsPageHandlerFromCache()).toBe(pageChange);
- });
-});
-
describe('installScript', () => {
it('should add the given script in the dom', () => {
installScript('custom_script.js');
@@ -67,17 +37,19 @@ describe('installScript', () => {
});
describe('getExtensionStart', () => {
- it('should install the extension in the to dom', () => {
+ it('should install the extension in the to dom', async () => {
const start = jest.fn();
const scriptTag = document.createElement('script');
document.createElement = jest.fn().mockReturnValue(scriptTag);
installExtensionsHandler();
const result = getExtensionStart('bar');
- (window as any).registerExtension('bar', start);
- (scriptTag.onload as Function)();
+ await new Promise(setImmediate);
expect(exposeLibraries).toBeCalled();
+
+ (window as any).registerExtension('bar', start);
+ (scriptTag.onload as Function)();
return expect(result).resolves.toBe(start);
});
diff --git a/server/sonar-web/src/main/js/helpers/__tests__/extensionsHandler-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/extensionsHandler-test.ts
new file mode 100644
index 00000000000..a2a23379e32
--- /dev/null
+++ b/server/sonar-web/src/main/js/helpers/__tests__/extensionsHandler-test.ts
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+import {
+ getExtensionFromCache,
+ getWebAnalyticsPageHandlerFromCache,
+ installExtensionsHandler,
+ installWebAnalyticsHandler
+} from '../extensionsHandler';
+
+describe('installExtensionsHandler & extensions.getExtensionFromCache', () => {
+ it('should register the global "registerExtension" function and retrieve extension', () => {
+ expect((window as any).registerExtension).toBeUndefined();
+ installExtensionsHandler();
+ expect((window as any).registerExtension).toEqual(expect.any(Function));
+
+ const start = jest.fn();
+ (window as any).registerExtension('foo', start);
+ expect(getExtensionFromCache('foo')).toBe(start);
+ });
+});
+
+describe('setWebAnalyticsPageChangeHandler & getWebAnalyticsPageHandlerFromCache', () => {
+ it('should register the global "setWebAnalyticsPageChangeHandler" function and retrieve analytics extension', () => {
+ expect((window as any).setWebAnalyticsPageChangeHandler).toBeUndefined();
+ installWebAnalyticsHandler();
+ expect((window as any).setWebAnalyticsPageChangeHandler).toEqual(expect.any(Function));
+
+ const pageChange = jest.fn();
+ (window as any).setWebAnalyticsPageChangeHandler(pageChange);
+ expect(getWebAnalyticsPageHandlerFromCache()).toBe(pageChange);
+ });
+});
diff --git a/server/sonar-web/src/main/js/helpers/extensions.ts b/server/sonar-web/src/main/js/helpers/extensions.ts
index 66a56cf3ed0..6d40d843625 100644
--- a/server/sonar-web/src/main/js/helpers/extensions.ts
+++ b/server/sonar-web/src/main/js/helpers/extensions.ts
@@ -18,34 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { getBaseUrl } from './urls';
-import exposeLibraries from '../app/components/extensions/exposeLibraries';
+import { getExtensionFromCache } from './extensionsHandler';
-const WEB_ANALYTICS_EXTENSION = 'sq-web-analytics';
-const extensions: T.Dict<Function> = {};
-
-function registerExtension(key: string, start: Function) {
- extensions[key] = start;
-}
-
-function setWebAnalyticsPageChangeHandler(pageHandler: (pathname: string) => void) {
- registerExtension(WEB_ANALYTICS_EXTENSION, pageHandler);
-}
-
-export function installExtensionsHandler() {
- (window as any).registerExtension = registerExtension;
-}
-
-export function installWebAnalyticsHandler() {
- (window as any).setWebAnalyticsPageChangeHandler = setWebAnalyticsPageChangeHandler;
-}
-
-export function getExtensionFromCache(key: string): Function | undefined {
- return extensions[key];
-}
-
-export function getWebAnalyticsPageHandlerFromCache(): Function | undefined {
- return extensions[WEB_ANALYTICS_EXTENSION];
-}
+let librariesExposed = false;
export function installScript(url: string, target: 'body' | 'head' = 'body'): Promise<any> {
return new Promise(resolve => {
@@ -62,7 +37,13 @@ export async function getExtensionStart(key: string) {
return Promise.resolve(fromCache);
}
- exposeLibraries();
+ if (!librariesExposed) {
+ // Async import allows to reduce initial vendor bundle size
+ const exposeLibraries = (await import('../app/components/extensions/exposeLibraries')).default;
+ exposeLibraries();
+ librariesExposed = true;
+ }
+
await installScript(`/static/${key}.js`);
const start = getExtensionFromCache(key);
diff --git a/server/sonar-web/src/main/js/helpers/extensionsHandler.ts b/server/sonar-web/src/main/js/helpers/extensionsHandler.ts
new file mode 100644
index 00000000000..e414b2fdbbd
--- /dev/null
+++ b/server/sonar-web/src/main/js/helpers/extensionsHandler.ts
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+// Do not import dependencies in this helper, to keep initial bundle load as small as possible
+
+const WEB_ANALYTICS_EXTENSION = 'sq-web-analytics';
+
+const extensions: T.Dict<Function> = {};
+
+function registerExtension(key: string, start: Function) {
+ extensions[key] = start;
+}
+
+function setWebAnalyticsPageChangeHandler(pageHandler: (pathname: string) => void) {
+ registerExtension(WEB_ANALYTICS_EXTENSION, pageHandler);
+}
+
+export function installExtensionsHandler() {
+ (window as any).registerExtension = registerExtension;
+}
+
+export function installWebAnalyticsHandler() {
+ (window as any).setWebAnalyticsPageChangeHandler = setWebAnalyticsPageChangeHandler;
+}
+
+export function getExtensionFromCache(key: string): Function | undefined {
+ return extensions[key];
+}
+
+export function getWebAnalyticsPageHandlerFromCache(): Function | undefined {
+ return extensions[WEB_ANALYTICS_EXTENSION];
+}