Skip to content

Commit 2233948

Browse files
author
Fatme
authored
Merge pull request #3903 from NativeScript/fatme/externals
chore: provide external plugins to webpack in order to fix `tns preview --bundle` command on android devices
2 parents 943339f + 634c5eb commit 2233948

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

lib/definitions/preview-app-livesync.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare global {
2020

2121
interface IPreviewAppPluginsService {
2222
comparePluginsOnDevice(device: Device): Promise<void>;
23+
getExternalPlugins(device: Device): string[];
2324
}
2425

2526
interface IPreviewCommandHelper {

lib/services/livesync/playground/preview-app-livesync-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
4444
platform: device.platform,
4545
appFilesUpdaterOptions: data.appFilesUpdaterOptions,
4646
},
47+
externals: this.$previewAppPluginsService.getExternalPlugins(device),
4748
filesToSyncMap,
4849
  startSyncFilesTimeout: startSyncFilesTimeout.bind(this)
4950
}

lib/services/livesync/playground/preview-app-plugins-service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
2828
this.previewAppVersionWarnings[device.previewAppVersion].map(warning => this.$logger.warn(warning));
2929
}
3030

31+
public getExternalPlugins(device: Device): string[] {
32+
const devicePlugins = this.getDevicePlugins(device);
33+
const result = _.keys(devicePlugins)
34+
.filter(plugin => plugin.indexOf("nativescript") !== -1)
35+
// exclude angular and vue related dependencies as they do not contain
36+
// any native code. In this way, we will read them from the bundle
37+
// and improve the app startup time by not reading a lot of
38+
// files from the file system instead. Also, the core theme links
39+
// are custom and should be handled by us build time.
40+
.filter(plugin => !_.includes(["nativescript-angular", "nativescript-vue", "nativescript-intl", "nativescript-theme-core"], plugin));
41+
42+
result.push(...["tns-core-modules", "tns-core-modules-widgets"]);
43+
return result;
44+
}
45+
3146
private getDevicePlugins(device: Device): IStringDictionary {
3247
try {
3348
return JSON.parse(device.plugins);

test/services/playground/preview-app-livesync-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ function createTestInjector(options?: {
107107
injector.register("previewAppPluginsService", {
108108
comparePluginsOnDevice: async () => {
109109
isComparePluginsOnDeviceCalled = true;
110-
}
110+
},
111+
getExternalPlugins: () => <string[]>[]
111112
});
112113
injector.register("projectFilesManager", ProjectFilesManager);
113114
injector.register("previewAppLiveSyncService", PreviewAppLiveSyncService);

test/services/playground/preview-app-plugins-service.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,56 @@ describe("previewAppPluginsService", () => {
232232
});
233233
}
234234
});
235+
describe("getExternalPlugins", () => {
236+
const testCases = [
237+
{
238+
name: "should return default plugins(`tns-core-modules` and `tns-core-modules-widgets`) when no plugins are provided",
239+
plugins: {},
240+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
241+
},
242+
{
243+
name: "should exclude `nativescript-vue`",
244+
plugins: { "nativescript-vue": "1.2.3" },
245+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
246+
},
247+
{
248+
name: "should exclude `nativescript-intl`",
249+
plugins: { "nativescript-intl": "4.5.6" },
250+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
251+
},
252+
{
253+
name: "should exclude `nativescript-angular`",
254+
plugins: { "nativescript-angular": "7.8.9" },
255+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
256+
},
257+
{
258+
name: "should exclude `nativescript-theme-core`",
259+
plugins: { "nativescript-theme-core": "1.3.5" },
260+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
261+
},
262+
{
263+
name: "should return plugins that contain `nativescript` in their names",
264+
plugins: {
265+
"nativescript-facebook": "4.5.6"
266+
},
267+
expectedPlugins: ["nativescript-facebook", "tns-core-modules", "tns-core-modules-widgets"]
268+
},
269+
{
270+
name: "should not return plugins that do not contain `nativescript` in their names",
271+
plugins: {
272+
lodash: "4.5.6",
273+
xmlhttprequest: "1.2.3"
274+
},
275+
expectedPlugins: ["tns-core-modules", "tns-core-modules-widgets"]
276+
}
277+
];
278+
279+
_.each(testCases, testCase => {
280+
it(`${testCase.name}`, () => {
281+
const { previewAppPluginsService, device } = setup(testCase.plugins, testCase.plugins);
282+
const actualPlugins = previewAppPluginsService.getExternalPlugins(device);
283+
assert.deepEqual(actualPlugins, testCase.expectedPlugins);
284+
});
285+
});
286+
});
235287
});

0 commit comments

Comments
 (0)