From 2bdccefb3d4229b3a03b090ecf5ad7cbb48973ad Mon Sep 17 00:00:00 2001 From: Ventsislav Georgiev Date: Tue, 29 Jan 2019 15:53:57 +0200 Subject: [PATCH 1/4] fix: optimize platform specific files resolver --- host/resolver.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/host/resolver.ts b/host/resolver.ts index 13e7cc04..d3d97662 100644 --- a/host/resolver.ts +++ b/host/resolver.ts @@ -5,19 +5,32 @@ import { import { statSync } from "fs"; export function getResolver(platforms: string[]) { - return function(path: string) { + const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml"]; + const nsPackageFilters = [ + 'nativescript-', + 'tns-' + ]; + + return function (path: string) { + if (path.indexOf('node_modules') !== -1 && nsPackageFilters.every(f => path.indexOf(f) === -1)) { + return path; + } + const { dir, name, ext } = parse(path); + if (platformSpecificExt.indexOf(ext) === -1) { + return path; + } + for (const platform of platforms) { const platformFileName = `${name}.${platform}${ext}`; const platformPath = toSystemPath(join(dir, platformFileName)); try { - const stat = statSync(platformPath); - if (stat && stat.isFile()) { + if (statSync(platformPath)) { return platformPath; } - } catch(_e) { + } catch (_e) { // continue checking the other platforms } } @@ -34,6 +47,6 @@ function toSystemPath(path: string) { const drive = path.match(/^\\(\w)\\(.*)$/); return drive ? - `${drive[1]}:\\${drive[2]}`: + `${drive[1]}:\\${drive[2]}` : path; } From 2a7f4c801192c677a6d7e0778b0197b6f7c5fbc9 Mon Sep 17 00:00:00 2001 From: Ventsislav Georgiev Date: Tue, 29 Jan 2019 16:35:23 +0200 Subject: [PATCH 2/4] fix: add .vue and .json files support to the resolver --- host/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/resolver.ts b/host/resolver.ts index d3d97662..974d0551 100644 --- a/host/resolver.ts +++ b/host/resolver.ts @@ -5,7 +5,7 @@ import { import { statSync } from "fs"; export function getResolver(platforms: string[]) { - const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml"]; + const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"]; const nsPackageFilters = [ 'nativescript-', 'tns-' From d436c66add80f37cd05ff383e3bc7aad5f4e4862 Mon Sep 17 00:00:00 2001 From: Ventsislav Georgiev Date: Wed, 30 Jan 2019 12:01:42 +0200 Subject: [PATCH 3/4] feat: improve check for ns plugins in resolver --- host/resolver.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/host/resolver.ts b/host/resolver.ts index 974d0551..440fd187 100644 --- a/host/resolver.ts +++ b/host/resolver.ts @@ -1,19 +1,23 @@ -import { - parse, - join, -} from "path"; +import { parse, join } from "path"; import { statSync } from "fs"; export function getResolver(platforms: string[]) { const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"]; const nsPackageFilters = [ - 'nativescript-', - 'tns-' + 'nativescript', + 'tns', + 'ns' ]; return function (path: string) { - if (path.indexOf('node_modules') !== -1 && nsPackageFilters.every(f => path.indexOf(f) === -1)) { - return path; + const nmIndex = path.lastIndexOf('node_modules'); + + if (nmIndex !== -1) { + const pathParts = path.substr(nmIndex + 'node_modules'.length).split(/[/\\\-_]/); + + if (pathParts.every(p => nsPackageFilters.every(f => f !== p))) { + return path; + } } const { dir, name, ext } = parse(path); From 22709b6cb16051b5c2f4d21d9dc86ac79ba4e1f7 Mon Sep 17 00:00:00 2001 From: Ventsislav Georgiev Date: Wed, 30 Jan 2019 12:27:20 +0200 Subject: [PATCH 4/4] feat: allow explicit package handling in resolver --- host/resolver.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/host/resolver.ts b/host/resolver.ts index 440fd187..30d8c7a0 100644 --- a/host/resolver.ts +++ b/host/resolver.ts @@ -1,7 +1,7 @@ import { parse, join } from "path"; import { statSync } from "fs"; -export function getResolver(platforms: string[]) { +export function getResolver(platforms: string[], explicitResolve: string[] = []) { const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"]; const nsPackageFilters = [ 'nativescript', @@ -13,9 +13,11 @@ export function getResolver(platforms: string[]) { const nmIndex = path.lastIndexOf('node_modules'); if (nmIndex !== -1) { - const pathParts = path.substr(nmIndex + 'node_modules'.length).split(/[/\\\-_]/); - - if (pathParts.every(p => nsPackageFilters.every(f => f !== p))) { + const subPath = path.substr(nmIndex + 'node_modules'.length).replace(/\\/g, '/'); + const shouldResolve = explicitResolve.length && explicitResolve.some(packageName => subPath.indexOf(packageName) !== -1); + const pathParts = subPath.split(/[/\-_]/); + + if (!shouldResolve && pathParts.every(p => nsPackageFilters.every(f => f !== p))) { return path; } }