UNPKG

4.41 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 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 index_1 = require("../index");
17const fs_url_resolver_1 = require("./fs-url-resolver");
18/**
19 * A URL resolver for very large codebases where source files map in an
20 * arbitrary but predetermined fashion onto URL space.
21 *
22 * It also separates the root directory – the root of all source code that's
23 * legal to load – from the package directory, which is how the user refers to
24 * files on the CLI or the IDE.
25 */
26class IndirectUrlResolver extends fs_url_resolver_1.FsUrlResolver {
27 /**
28 * @param rootPath All loadable source code must be a descendent of this
29 * directory. Should be the same as FsUrlLoader's rootPath.
30 * @param packagePath The base directory for package-relative paths. Usually
31 * the current working directory.
32 * @param indirectionMap Maps the runtime URL space to the paths for those
33 * files on the filesystem.
34 *
35 * The keys must be relative paths, like `paper-button/paper-button.html`.
36 * The filesystem paths must be be relative Fs paths from `rootPath` to
37 * the file on disk that corresponds to the runtime URL.
38 */
39 constructor(rootPath, packagePath, indirectionMap, protocol = 'https') {
40 super(packagePath);
41 this.protocol = protocol;
42 const rootResolver = new fs_url_resolver_1.FsUrlResolver(rootPath);
43 const urlspaceToFilesystem = new Map();
44 const filesystemToUrlspace = new Map();
45 for (const [u, fsPath] of indirectionMap) {
46 const url = u;
47 const fsUrl = rootResolver.resolve(this.brandAsPackageRelative(fsPath));
48 if (fsUrl === undefined) {
49 throw new Error(`Invalid fs path in indirection map: ${fsPath}`);
50 }
51 urlspaceToFilesystem.set(url, fsUrl);
52 filesystemToUrlspace.set(fsUrl, url);
53 }
54 this.runtimeUrlToResolvedUrl = urlspaceToFilesystem;
55 this.resolvedUrlToRuntimeUrl = filesystemToUrlspace;
56 }
57 resolve(firstHref, secondHref) {
58 const [baseUrl, unresolvedHref] = this.getBaseAndUnresolved(firstHref, secondHref);
59 // If we're just resolving a package-relative url, do the basic thing.
60 if (baseUrl === undefined) {
61 return super.resolve(this.brandAsPackageRelative(unresolvedHref));
62 }
63 const url = this.brandAsFileRelative(unresolvedHref);
64 const runtimeBaseUrl = this.resolvedUrlToRuntimeUrl.get(baseUrl);
65 if (runtimeBaseUrl === undefined) {
66 return super.resolve(baseUrl, url);
67 }
68 const runtimeUrl = this.runtimeResolve(url, runtimeBaseUrl);
69 const resolvedUrl = this.runtimeUrlToResolvedUrl.get(runtimeUrl);
70 if (resolvedUrl === undefined) {
71 return super.resolve(baseUrl, url);
72 }
73 return resolvedUrl;
74 }
75 runtimeResolve(url, runtimeBaseUrl) {
76 const resolved = this.simpleUrlResolve(this.brandAsResolved(runtimeBaseUrl), url, this.protocol);
77 return brandAsRuntimeUrl(resolved);
78 }
79 relative(fromOrTo, maybeTo, _kind) {
80 const [from, to] = (maybeTo !== undefined) ? [fromOrTo, maybeTo] :
81 [this.packageUrl, fromOrTo];
82 return this.relativeImpl(from, to);
83 }
84 relativeImpl(from, to) {
85 const fromWeb = this.resolvedUrlToRuntimeUrl.get(from);
86 const toWeb = this.resolvedUrlToRuntimeUrl.get(to);
87 if (fromWeb === undefined || toWeb === undefined) {
88 return this.simpleUrlRelative(from, to);
89 }
90 return this.simpleUrlRelative(this.brandAsResolved(fromWeb), this.brandAsResolved(toWeb));
91 }
92}
93exports.IndirectUrlResolver = IndirectUrlResolver;
94function brandAsRuntimeUrl(resolved) {
95 const str = resolved;
96 return str;
97}
98//# sourceMappingURL=indirect-url-resolver.js.map
\No newline at end of file