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

Commit fb52c53

Browse files
fix: optimize platform specific files resolver (#782)
## 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"]`
1 parent ebce5f4 commit fb52c53

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

Diff for: host/resolver.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1-
import {
2-
parse,
3-
join,
4-
} from "path";
1+
import { parse, join } from "path";
52
import { statSync } from "fs";
63

7-
export function getResolver(platforms: string[]) {
8-
return function(path: string) {
4+
export function getResolver(platforms: string[], explicitResolve: string[] = []) {
5+
const platformSpecificExt = [".ts", ".js", ".scss", ".less", ".css", ".html", ".xml", ".vue", ".json"];
6+
const nsPackageFilters = [
7+
'nativescript',
8+
'tns',
9+
'ns'
10+
];
11+
12+
return function (path: string) {
13+
const nmIndex = path.lastIndexOf('node_modules');
14+
15+
if (nmIndex !== -1) {
16+
const subPath = path.substr(nmIndex + 'node_modules'.length).replace(/\\/g, '/');
17+
const shouldResolve = explicitResolve.length && explicitResolve.some(packageName => subPath.indexOf(packageName) !== -1);
18+
const pathParts = subPath.split(/[/\-_]/);
19+
20+
if (!shouldResolve && pathParts.every(p => nsPackageFilters.every(f => f !== p))) {
21+
return path;
22+
}
23+
}
24+
925
const { dir, name, ext } = parse(path);
1026

27+
if (platformSpecificExt.indexOf(ext) === -1) {
28+
return path;
29+
}
30+
1131
for (const platform of platforms) {
1232
const platformFileName = `${name}.${platform}${ext}`;
1333
const platformPath = toSystemPath(join(dir, platformFileName));
1434

1535
try {
16-
const stat = statSync(platformPath);
17-
if (stat && stat.isFile()) {
36+
if (statSync(platformPath)) {
1837
return platformPath;
1938
}
20-
} catch(_e) {
39+
} catch (_e) {
2140
// continue checking the other platforms
2241
}
2342
}
@@ -34,6 +53,6 @@ function toSystemPath(path: string) {
3453

3554
const drive = path.match(/^\\(\w)\\(.*)$/);
3655
return drive ?
37-
`${drive[1]}:\\${drive[2]}`:
56+
`${drive[1]}:\\${drive[2]}` :
3857
path;
3958
}

0 commit comments

Comments
 (0)