UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const path = require("path");
17const url_1 = require("url");
18const utils_1 = require("../core/utils");
19/**
20 * Resolves the given URL to the concrete URL that a resource can
21 * be loaded from.
22 *
23 * This can be useful to resolve name to paths, such as resolving 'polymer' to
24 * '../polymer/polymer.html', or component paths, like '../polymer/polymer.html'
25 * to '/bower_components/polymer/polymer.html'.
26 */
27class UrlResolver {
28 getBaseAndUnresolved(url1, url2) {
29 return url2 === undefined ?
30 [undefined, this.brandAsPackageRelative(url1)] :
31 [this.brandAsResolved(url1), this.brandAsFileRelative(url2)];
32 }
33 simpleUrlResolve(baseUrl, url, defaultProtocol) {
34 return this.brandAsResolved(utils_1.resolveUrl(baseUrl, url, defaultProtocol));
35 }
36 simpleUrlRelative(from, to) {
37 const fromUrl = utils_1.parseUrl(from);
38 const toUrl = utils_1.parseUrl(to);
39 // Return the `to` as-is if there are conflicting components which
40 // prohibit calculating a relative form.
41 if (typeof toUrl.protocol === 'string' &&
42 fromUrl.protocol !== toUrl.protocol ||
43 typeof toUrl.slashes === 'boolean' &&
44 fromUrl.slashes !== toUrl.slashes ||
45 typeof toUrl.host === 'string' && fromUrl.host !== toUrl.host ||
46 typeof toUrl.auth === 'string' && fromUrl.auth !== toUrl.auth) {
47 return this.brandAsFileRelative(to);
48 }
49 let pathname;
50 const { search, hash } = toUrl;
51 if (fromUrl.pathname === toUrl.pathname) {
52 pathname = '';
53 }
54 else {
55 const fromDir = typeof fromUrl.pathname === 'string' ?
56 fromUrl.pathname.replace(/[^/]+$/, '') :
57 '';
58 const toDir = typeof toUrl.pathname === 'string' &&
59 typeof toUrl.pathname === 'string' ?
60 toUrl.pathname :
61 '';
62 // In a browserify environment, there isn't path.posix.
63 const pathlib = path.posix || path;
64 // Note, below, the _ character is appended to the `toDir` so that paths
65 // with trailing slash will retain the trailing slash in the result.
66 pathname = pathlib.relative(fromDir, toDir + '_').replace(/_$/, '');
67 }
68 return this.brandAsFileRelative(url_1.format({ pathname, search, hash }));
69 }
70 brandAsFileRelative(url) {
71 return url;
72 }
73 brandAsPackageRelative(url) {
74 return url;
75 }
76 brandAsResolved(url) {
77 return url;
78 }
79}
80exports.UrlResolver = UrlResolver;
81//# sourceMappingURL=url-resolver.js.map
\No newline at end of file