Skip to content

refactor: resource loader #968

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 3 commits into from
Aug 30, 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
18 changes: 9 additions & 9 deletions nativescript-angular/file-system/ns-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { knownFolders, Folder, File } from "tns-core-modules/file-system";

@Injectable()
export class NSFileSystem {
public currentApp(): Folder {
return knownFolders.currentApp();
}
public currentApp(): Folder {
return knownFolders.currentApp();
}

public fileFromPath(path: string): File {
return File.fromPath(path);
}
public fileFromPath(path: string): File {
return File.fromPath(path);
}

public fileExists(path: string): boolean {
return File.exists(path);
}
public fileExists(path: string): boolean {
return File.exists(path);
}
}
51 changes: 32 additions & 19 deletions nativescript-angular/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ export class FileSystemResourceLoader extends ResourceLoader {
return templateFile.readText();
}

resolveRelativeUrls(url: string): string {
resolve(url: string): string {
const normalizedUrl = this.resolveRelativeUrls(url);

if (this.fs.fileExists(normalizedUrl)) {
return normalizedUrl;
}

const { candidates: fallbackCandidates, resource: fallbackResource } =
this.fallbackResolve(normalizedUrl);

if (fallbackResource) {
return fallbackResource;
}

throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`);
}

private resolveRelativeUrls(url: string): string {
// Angular assembles absolute URLs and prefixes them with //
if (url.indexOf("/") !== 0) {
// Resolve relative URLs based on the app root.
Expand All @@ -34,26 +51,22 @@ export class FileSystemResourceLoader extends ResourceLoader {
}
}

resolve(url: string) {
const normalizedUrl = this.resolveRelativeUrls(url);
private fallbackResolve(url: string):
({ resource: string, candidates: string[] }) {

if (this.fs.fileExists(normalizedUrl)) {
return normalizedUrl;
}
const candidates = extensionsFallbacks
.filter(([extension]) => url.endsWith(extension))
.map(([extension, fallback]) =>
this.replaceExtension(url, extension, fallback));

const fallbackCandidates = [];
extensionsFallbacks.forEach(([extension, fallback]) => {
if (normalizedUrl.endsWith(extension)) {
fallbackCandidates.push(normalizedUrl.substr(0, normalizedUrl.length - extension.length) + fallback);
}
});

for (let i = 0; i < fallbackCandidates.length; i++) {
if (this.fs.fileExists(fallbackCandidates[i])) {
return fallbackCandidates[i];
}
}
const resource = candidates.find(candidate => this.fs.fileExists(candidate));

throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`);
return { candidates, resource };
}

private replaceExtension(fileName: string, oldExtension: string, newExtension: string): string {
const baseName = fileName.substr(0, fileName.length - oldExtension.length);
return baseName + newExtension;
}
}

6 changes: 5 additions & 1 deletion tests/app/tests/xhr-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NSFileSystemMock {
}

public fileExists(path: string): boolean {
// mycomponent.html aways exists
// mycomponent.html always exists
// mycomponent.css is the other file
return path.indexOf("mycomponent.html") >= 0 || path === "/app/dir/mycomponent.css";
}
Expand Down Expand Up @@ -65,4 +65,8 @@ describe("XHR name resolution", () => {
"/app/dir/mycomponent.css",
resourceLoader.resolve("mycomponent.less"));
});

it("throws for non-existing file that has no fallbacks", () => {
assert.throws(() => resourceLoader.resolve("does-not-exist.css"));
});
});