Skip to content

Commit c2f1fe9

Browse files
committed
Support "relative" URLs in component decorators.
We get resolved absolute paths from Angular prefixed by a "//"
1 parent df63d10 commit c2f1fe9

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/nativescript-angular/xhr.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ import {path, knownFolders, File} from "file-system";
22
import {XHR} from "@angular/compiler/src/xhr";
33

44
export class FileSystemXHR extends XHR {
5-
get(url: string): Promise<string> {
6-
let appDir = knownFolders.currentApp().path;
7-
let templatePath = path.join(appDir, url);
5+
resolve(url: string, baseUrl: string): string {
6+
//Angular assembles absolute URL's and prefixes them with //
7+
if (url.indexOf("//") !== 0) {
8+
//Resolve relative URL's based on the app root.
9+
return path.join(baseUrl, url);
10+
} else {
11+
return url;
12+
}
13+
}
814

9-
if (!File.exists(templatePath)) {
10-
throw new Error(`File ${url} does not exist.`);
11-
}
12-
let templateFile = File.fromPath(templatePath);
13-
return templateFile.readText();
14-
}
15+
get(url: string): Promise<string> {
16+
const appDir = knownFolders.currentApp().path;
17+
const templatePath = this.resolve(url, appDir);
18+
19+
if (!File.exists(templatePath)) {
20+
throw new Error(`File ${templatePath} does not exist. Resolved from: ${url}.`);
21+
}
22+
let templateFile = File.fromPath(templatePath);
23+
return templateFile.readText();
24+
}
1525
}

tests/app/tests/xhr-paths.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//make sure you import mocha-config before @angular/core
2+
import {assert} from "./test-config";
3+
import {FileSystemXHR} from "nativescript-angular/xhr";
4+
5+
describe("XHR name resolution", () => {
6+
it("resolves relative paths from app root", () => {
7+
const xhr = new FileSystemXHR();
8+
assert.strictEqual("/app/dir/mydir/mycomponent.html", xhr.resolve("mydir/mycomponent.html", "/app/dir"))
9+
});
10+
11+
it("resolves absolute paths as is", () => {
12+
const xhr = new FileSystemXHR();
13+
assert.strictEqual("//app/mydir/mycomponent.html", xhr.resolve("//app/mydir/mycomponent.html", "/app/dir"))
14+
});
15+
})

0 commit comments

Comments
 (0)