Skip to content

Check for .css files before .scss, .less, .sass files #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified nativescript-angular/bin/update-app-ng-deps
100644 → 100755
Empty file.
52 changes: 17 additions & 35 deletions nativescript-angular/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,20 +25,21 @@ 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 {
Expand All @@ -50,23 +51,4 @@ export class FileSystemResourceLoader extends ResourceLoader {
return url;
}
}

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;
}
}