From e1ffc501a22f4b3d058861aa86946985abe7681c Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Tue, 5 Dec 2017 13:05:29 +0200 Subject: [PATCH 1/2] Check for .css files before .scss, .less, .sass files --- nativescript-angular/bin/update-app-ng-deps | 0 nativescript-angular/resource-loader.ts | 46 +++++++-------------- 2 files changed, 16 insertions(+), 30 deletions(-) mode change 100644 => 100755 nativescript-angular/bin/update-app-ng-deps diff --git a/nativescript-angular/bin/update-app-ng-deps b/nativescript-angular/bin/update-app-ng-deps old mode 100644 new mode 100755 diff --git a/nativescript-angular/resource-loader.ts b/nativescript-angular/resource-loader.ts index 43a5091bc..0e5466199 100644 --- a/nativescript-angular/resource-loader.ts +++ b/nativescript-angular/resource-loader.ts @@ -4,11 +4,11 @@ import { path } from "tns-core-modules/file-system"; import { NSFileSystem } from "./file-system/ns-file-system"; -const extensionsFallbacks = [ - [".scss", ".css"], - [".sass", ".css"], - [".less", ".css"] -]; +const sourceExtensionsMap = { + ".scss": ".css", + ".sass": ".css", + ".less": ".css" +} @Injectable() export class FileSystemResourceLoader extends ResourceLoader { @@ -25,20 +25,20 @@ export class FileSystemResourceLoader extends ResourceLoader { } resolve(url: string): string { - const normalizedUrl = this.resolveRelativeUrls(url); - - if (this.fs.fileExists(normalizedUrl)) { - return normalizedUrl; + const normalizedSourceUrl = this.resolveRelativeUrls(url); + const normalizedCompiledFileUrl = normalizedSourceUrl.replace(/\.\w+$/, ext => sourceExtensionsMap[ext] || ext); + if (normalizedCompiledFileUrl != normalizedSourceUrl && this.fs.fileExists(normalizedCompiledFileUrl)) { + return normalizedCompiledFileUrl; } - - const { candidates: fallbackCandidates, resource: fallbackResource } = - this.fallbackResolve(normalizedUrl); - - if (fallbackResource) { - return fallbackResource; + if (this.fs.fileExists(normalizedSourceUrl)) { + return normalizedSourceUrl; } - throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`); + if (normalizedCompiledFileUrl === normalizedSourceUrl) { + throw new Error(`Could not resolve ${url}. Looked for: ${normalizedSourceUrl}.`); + } else { + throw new Error(`Could not resolve ${url}. Looked for: ${normalizedCompiledFileUrl}, ${normalizedSourceUrl}.`); + } } private resolveRelativeUrls(url: string): string { @@ -51,22 +51,8 @@ export class FileSystemResourceLoader extends ResourceLoader { } } - private fallbackResolve(url: string): - ({ resource: string, candidates: string[] }) { - - const candidates = extensionsFallbacks - .filter(([extension]) => url.endsWith(extension)) - .map(([extension, fallback]) => - this.replaceExtension(url, extension, fallback)); - - const resource = candidates.find(candidate => this.fs.fileExists(candidate)); - - return { candidates, resource }; - } - private replaceExtension(fileName: string, oldExtension: string, newExtension: string): string { const baseName = fileName.substr(0, fileName.length - oldExtension.length); return baseName + newExtension; } } - From fde2d54f0d0c40b152f57ed7df820066ee707431 Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Tue, 5 Dec 2017 14:42:31 +0200 Subject: [PATCH 2/2] Fix tslint errors --- nativescript-angular/resource-loader.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/nativescript-angular/resource-loader.ts b/nativescript-angular/resource-loader.ts index 0e5466199..f9f5ca9a5 100644 --- a/nativescript-angular/resource-loader.ts +++ b/nativescript-angular/resource-loader.ts @@ -8,7 +8,7 @@ const sourceExtensionsMap = { ".scss": ".css", ".sass": ".css", ".less": ".css" -} +}; @Injectable() export class FileSystemResourceLoader extends ResourceLoader { @@ -27,7 +27,7 @@ export class FileSystemResourceLoader extends ResourceLoader { resolve(url: string): string { const normalizedSourceUrl = this.resolveRelativeUrls(url); const normalizedCompiledFileUrl = normalizedSourceUrl.replace(/\.\w+$/, ext => sourceExtensionsMap[ext] || ext); - if (normalizedCompiledFileUrl != normalizedSourceUrl && this.fs.fileExists(normalizedCompiledFileUrl)) { + if (normalizedCompiledFileUrl !== normalizedSourceUrl && this.fs.fileExists(normalizedCompiledFileUrl)) { return normalizedCompiledFileUrl; } if (this.fs.fileExists(normalizedSourceUrl)) { @@ -37,7 +37,8 @@ export class FileSystemResourceLoader extends ResourceLoader { if (normalizedCompiledFileUrl === normalizedSourceUrl) { throw new Error(`Could not resolve ${url}. Looked for: ${normalizedSourceUrl}.`); } else { - throw new Error(`Could not resolve ${url}. Looked for: ${normalizedCompiledFileUrl}, ${normalizedSourceUrl}.`); + throw new Error(`Could not resolve ${url}.` + + `Looked for: ${normalizedCompiledFileUrl}, ${normalizedSourceUrl}.`); } } @@ -50,9 +51,4 @@ export class FileSystemResourceLoader extends ResourceLoader { return url; } } - - private replaceExtension(fileName: string, oldExtension: string, newExtension: string): string { - const baseName = fileName.substr(0, fileName.length - oldExtension.length); - return baseName + newExtension; - } }