From 1088bd7f28a4586f4e8763ce34f741eace10fa31 Mon Sep 17 00:00:00 2001 From: farfromrefuge Date: Thu, 19 Aug 2021 17:46:19 +0200 Subject: [PATCH 1/2] remove duplicate plugins data This happens when multiple deps have another common dependencies. This fix has one direct consequences which makes app build faster. Before this native plugins where built multiple times. As an example for [this](https://github.com/nativescript-community/ui-material-components/tree/master/demo-vue) app using my material components. Before this commit : ```js pluginsData 37 [ '@nativescript-community/css-theme', '@nativescript-community/text', '@nativescript-community/ui-material-activityindicator', '@nativescript-community/ui-material-bottom-navigation', '@nativescript-community/ui-material-bottomnavigationbar', '@nativescript-community/ui-material-bottomsheet', '@nativescript-community/ui-material-button', '@nativescript-community/ui-material-cardview', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-dialogs', '@nativescript-community/ui-material-floatingactionbutton', '@nativescript-community/ui-material-progress', '@nativescript-community/ui-material-ripple', '@nativescript-community/ui-material-slider', '@nativescript-community/ui-material-snackbar', '@nativescript-community/ui-material-speeddial', '@nativescript-community/ui-material-tabs', '@nativescript-community/ui-material-textfield', '@nativescript-community/ui-material-textview', '@nativescript/core', '@nativescript/iqkeyboardmanager', '@nativescript/theme', 'nativescript-vue', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/text', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-textfield', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-core', '@nativescript-community/text', '@nativescript-community/ui-material-core', '@nativescript-community/text' ] ``` After this plugin: ```js pluginsData 23 [ '@nativescript-community/css-theme', '@nativescript-community/text', '@nativescript-community/ui-material-activityindicator', '@nativescript-community/ui-material-bottom-navigation', '@nativescript-community/ui-material-bottomnavigationbar', '@nativescript-community/ui-material-bottomsheet', '@nativescript-community/ui-material-button', '@nativescript-community/ui-material-cardview', '@nativescript-community/ui-material-core', '@nativescript-community/ui-material-dialogs', '@nativescript-community/ui-material-floatingactionbutton', '@nativescript-community/ui-material-progress', '@nativescript-community/ui-material-ripple', '@nativescript-community/ui-material-slider', '@nativescript-community/ui-material-snackbar', '@nativescript-community/ui-material-speeddial', '@nativescript-community/ui-material-tabs', '@nativescript-community/ui-material-textfield', '@nativescript-community/ui-material-textview', '@nativescript/core', '@nativescript/iqkeyboardmanager', '@nativescript/theme', 'nativescript-vue' ] ``` It means the native build of the app plugins got reduced by 60% --- lib/services/plugins-service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/services/plugins-service.ts b/lib/services/plugins-service.ts index da4958b0f4..b5e196b509 100644 --- a/lib/services/plugins-service.ts +++ b/lib/services/plugins-service.ts @@ -359,7 +359,9 @@ export class PluginsService implements IPluginsService { ); const pluginData = productionPlugins.map((plugin) => this.convertToPluginData(plugin, projectData.projectDir) - ); + ).filter(function(item, pos, self) { + return self.findIndex(p=>p.name === item.name) == pos; + }); return pluginData; } From 6f2beca0ccf1ce666b2e91ae0ac194b59eaf9948 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Mon, 23 Aug 2021 13:39:49 +0200 Subject: [PATCH 2/2] chore: add comment & use arrow-function --- lib/services/plugins-service.ts | 54 +++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/services/plugins-service.ts b/lib/services/plugins-service.ts index b5e196b509..89c5781263 100644 --- a/lib/services/plugins-service.ts +++ b/lib/services/plugins-service.ts @@ -283,22 +283,27 @@ export class PluginsService implements IPluginsService { _.keys(packageJsonContent.devDependencies) ); - const notInstalledDependencies = allDependencies.map(dep => { - try { - this.$logger.trace(`Checking if ${dep} is installed...`); - require.resolve(`${dep}/package.json`, { - paths: [projectData.projectDir] - }) - - // return false if the dependency is installed - we'll filter out boolean values - // and end up with an array of dep names that are not installed if we end up - // inside the catch block. - return false; - } catch(err) { - this.$logger.trace(`Error while checking if ${dep} is installed. Error is: `, err) - return dep; - } - }).filter(Boolean); + const notInstalledDependencies = allDependencies + .map((dep) => { + try { + this.$logger.trace(`Checking if ${dep} is installed...`); + require.resolve(`${dep}/package.json`, { + paths: [projectData.projectDir], + }); + + // return false if the dependency is installed - we'll filter out boolean values + // and end up with an array of dep names that are not installed if we end up + // inside the catch block. + return false; + } catch (err) { + this.$logger.trace( + `Error while checking if ${dep} is installed. Error is: `, + err + ); + return dep; + } + }) + .filter(Boolean); if (this.$options.force || notInstalledDependencies.length) { this.$logger.trace( @@ -342,7 +347,8 @@ export class PluginsService implements IPluginsService { dependencies = dependencies || this.$nodeModulesDependenciesBuilder.getProductionDependencies( - projectData.projectDir, projectData.ignoredDependencies + projectData.projectDir, + projectData.ignoredDependencies ); if (_.isEmpty(dependencies)) { @@ -357,12 +363,14 @@ export class PluginsService implements IPluginsService { projectData.projectDir, platform ); - const pluginData = productionPlugins.map((plugin) => - this.convertToPluginData(plugin, projectData.projectDir) - ).filter(function(item, pos, self) { - return self.findIndex(p=>p.name === item.name) == pos; - }); - return pluginData; + return productionPlugins + .map((plugin) => this.convertToPluginData(plugin, projectData.projectDir)) + .filter((item, idx, self) => { + // Filter out duplicates to speed up build times by not building the same dependency + // multiple times. One possible downside is that if there are different versions + // of the same native dependency only the first one in the array will be built + return self.findIndex((p) => p.name === item.name) === idx; + }); } public getDependenciesFromPackageJson(