From 3e05b18b972a1bdb0a0854dced2ed29f846017b5 Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 17 Sep 2018 15:55:30 +0300 Subject: [PATCH 1/2] chore: provide external plugins to webpack in order to fix `tns preview --bundle` command on android devices --- lib/definitions/preview-app-livesync.d.ts | 1 + .../preview-app-livesync-service.ts | 1 + .../playground/preview-app-plugins-service.ts | 15 ++++++ .../playground/preview-app-plugins-service.ts | 52 +++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/lib/definitions/preview-app-livesync.d.ts b/lib/definitions/preview-app-livesync.d.ts index a725e8d160..bf8717add4 100644 --- a/lib/definitions/preview-app-livesync.d.ts +++ b/lib/definitions/preview-app-livesync.d.ts @@ -20,6 +20,7 @@ declare global { interface IPreviewAppPluginsService { comparePluginsOnDevice(device: Device): Promise; + getExternalPlugins(device: Device): string[]; } interface IPreviewCommandHelper { diff --git a/lib/services/livesync/playground/preview-app-livesync-service.ts b/lib/services/livesync/playground/preview-app-livesync-service.ts index 0d47b8089c..c8317693c4 100644 --- a/lib/services/livesync/playground/preview-app-livesync-service.ts +++ b/lib/services/livesync/playground/preview-app-livesync-service.ts @@ -44,6 +44,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService { platform: device.platform, appFilesUpdaterOptions: data.appFilesUpdaterOptions, }, + externals: this.$previewAppPluginsService.getExternalPlugins(device), filesToSyncMap,   startSyncFilesTimeout: startSyncFilesTimeout.bind(this) } diff --git a/lib/services/livesync/playground/preview-app-plugins-service.ts b/lib/services/livesync/playground/preview-app-plugins-service.ts index 7541ea04f8..9a486ceefe 100644 --- a/lib/services/livesync/playground/preview-app-plugins-service.ts +++ b/lib/services/livesync/playground/preview-app-plugins-service.ts @@ -28,6 +28,21 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService { this.previewAppVersionWarnings[device.previewAppVersion].map(warning => this.$logger.warn(warning)); } + public getExternalPlugins(device: Device): string[] { + const devicePlugins = this.getDevicePlugins(device); + const result = _.keys(devicePlugins) + .filter(plugin => plugin.indexOf("nativescript") !== -1) + // exclude angular and vue related dependencies as they do not contain + // any native code. In this way, we will read them from the bundle + // and improve the app startup time by not reading a lot of + // files from the file system instead. Also, the core theme links + // are custom and should be handled by us build time. + .filter(plugin => !_.includes(["nativescript-angular", "nativescript-vue", "nativescript-intl", "nativescript-theme-core"], plugin)); + + result.push(...["tns-core-modules", "tns-core-modules-widgets"]); + return result; + } + private getDevicePlugins(device: Device): IStringDictionary { try { return JSON.parse(device.plugins); diff --git a/test/services/playground/preview-app-plugins-service.ts b/test/services/playground/preview-app-plugins-service.ts index d3a772239b..093769c4b3 100644 --- a/test/services/playground/preview-app-plugins-service.ts +++ b/test/services/playground/preview-app-plugins-service.ts @@ -232,4 +232,56 @@ describe("previewAppPluginsService", () => { }); } }); + describe("getExternalPlugins", () => { + const testCases = [ + { + name: "should return default plugins(`tns-core-modules` and `tns-core-modules-widgets`) when no plugins are provided", + plugins: {}, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should exclude `nativescript-vue`", + plugins: { "nativescript-vue": "1.2.3" }, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should exclude `nativescript-intl`", + plugins: { "nativescript-intl": "4.5.6" }, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should exclude `nativescript-angular`", + plugins: { "nativescript-angular": "7.8.9" }, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should exclude `nativescript-theme-core`", + plugins: { "nativescript-theme-core": "1.3.5" }, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should return plugins that contain `nativescript` in their names", + plugins: { + "nativescript-facebook": "4.5.6" + }, + expectedPlugins: ["nativescript-facebook", "tns-core-modules", "tns-core-modules-widgets"] + }, + { + name: "should not return plugins that do not contain `nativescript` in their names", + plugins: { + lodash: "4.5.6", + xmlhttprequest: "1.2.3" + }, + expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"] + } + ]; + + _.each(testCases, testCase => { + it(`${testCase.name}`, () => { + const { previewAppPluginsService, device } = setup(testCase.plugins, testCase.plugins); + const actualPlugins = previewAppPluginsService.getExternalPlugins(device); + assert.deepEqual(actualPlugins, testCase.expectedPlugins); + }); + }); + }); }); From 634c5eb199f50187a96cd11c41d87d315a96e5df Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 17 Sep 2018 16:17:39 +0300 Subject: [PATCH 2/2] Fix unit tests --- test/services/playground/preview-app-livesync-service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/services/playground/preview-app-livesync-service.ts b/test/services/playground/preview-app-livesync-service.ts index 9301d71a36..a4f6838943 100644 --- a/test/services/playground/preview-app-livesync-service.ts +++ b/test/services/playground/preview-app-livesync-service.ts @@ -107,7 +107,8 @@ function createTestInjector(options?: { injector.register("previewAppPluginsService", { comparePluginsOnDevice: async () => { isComparePluginsOnDeviceCalled = true; - } + }, + getExternalPlugins: () => [] }); injector.register("projectFilesManager", ProjectFilesManager); injector.register("previewAppLiveSyncService", PreviewAppLiveSyncService);