Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
fix: optimize platform specific files resolver (#782)
Browse files Browse the repository at this point in the history
## What is the current behavior?
The platform specific files resolver for `.tns`, `.ios`, `.android` files is having performance issues on webpack and seems to break (make it run very slow like its for the first time) the incremental compilation. This is happening in big projects with many files in the `node_modules` directory, as the current implementation makes a file system call for each file.

## What is the new behavior?
The resolver now handles only the files that are considered to support platform specific names e.g:
* packages in `node_modules` with `nativescript`, `tns` or `ns` anywhere in the name
* only files with following extensions: `[".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"]`
  • Loading branch information
ventsislav-georgiev authored and SvetoslavTsenov committed Feb 1, 2019
1 parent ebce5f4 commit fb52c53
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions host/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import {
parse,
join,
} from "path";
import { parse, join } from "path";
import { statSync } from "fs";

export function getResolver(platforms: string[]) {
return function(path: string) {
export function getResolver(platforms: string[], explicitResolve: string[] = []) {
const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"];
const nsPackageFilters = [
'nativescript',
'tns',
'ns'
];

return function (path: string) {
const nmIndex = path.lastIndexOf('node_modules');

if (nmIndex !== -1) {
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;
}
}

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
}
}
Expand All @@ -34,6 +53,6 @@ function toSystemPath(path: string) {

const drive = path.match(/^\\(\w)\\(.*)$/);
return drive ?
`${drive[1]}:\\${drive[2]}`:
`${drive[1]}:\\${drive[2]}` :
path;
}

0 comments on commit fb52c53

Please sign in to comment.