UNPKG

4.3 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 pathlib = require("path");
17const path_1 = require("path");
18const pathIsInside = require("path-is-inside");
19const url_1 = require("url");
20const utils_1 = require("../core/utils");
21const fs_url_resolver_1 = require("./fs-url-resolver");
22/**
23 * Resolves a URL to a canonical URL within a package.
24 */
25class PackageUrlResolver extends fs_url_resolver_1.FsUrlResolver {
26 constructor(options = {}) {
27 super(options.packageDir, options.host, options.protocol);
28 this.componentDir = options.componentDir || 'bower_components/';
29 this.resolvedComponentDir =
30 pathlib.join(this.packageDir, this.componentDir);
31 }
32 modifyFsPath(path) {
33 // If the path points to a sibling directory, resolve it to the
34 // component directory
35 const parentOfPackageDir = pathlib.dirname(this.packageDir);
36 if (pathIsInside(path, parentOfPackageDir) &&
37 !pathIsInside(path, this.packageDir)) {
38 path = pathlib.join(this.packageDir, this.componentDir, path.substring(parentOfPackageDir.length));
39 }
40 return path;
41 }
42 relative(fromOrTo, maybeTo, _kind) {
43 const [from, to] = (maybeTo !== undefined) ? [fromOrTo, maybeTo] :
44 [this.packageUrl, fromOrTo];
45 const result = this.relativeImpl(from, to);
46 if (maybeTo === undefined) {
47 return this.brandAsPackageRelative(result);
48 }
49 else {
50 return result;
51 }
52 }
53 relativeImpl(from, to) {
54 const pathnameInComponentDir = this.pathnameForComponentDirUrl(to);
55 // If the URL we're going to is in the component directory, we need
56 // to correct for that when generating a relative URL...
57 if (pathnameInComponentDir !== undefined) {
58 // ... unless the URL we're coming from is *also* in the component
59 // directory
60 if (this.pathnameForComponentDirUrl(from) === undefined) {
61 // Ok, transform the path from looking like `/path/to/node_modules/foo`
62 // to look instead like `/path/foo` so we get a `../` relative url.
63 const componentDirPath = pathnameInComponentDir.slice(this.resolvedComponentDir.length);
64 const reresolved = this.simpleUrlResolve(this.packageUrl, ('../' + componentDirPath), this.protocol);
65 if (reresolved !== undefined) {
66 const reresolvedUrl = utils_1.parseUrl(reresolved);
67 const toUrl = utils_1.parseUrl(to);
68 reresolvedUrl.search = toUrl.search;
69 reresolvedUrl.hash = toUrl.hash;
70 to = this.brandAsResolved(url_1.format(reresolvedUrl));
71 }
72 }
73 }
74 return super.relative(from, to);
75 }
76 /**
77 * If the given URL is a file url inside our dependencies (e.g.
78 * bower_components) then return a resolved posix path to its file.
79 * Otherwise return undefined.
80 */
81 pathnameForComponentDirUrl(resolvedUrl) {
82 const url = utils_1.parseUrl(resolvedUrl);
83 if (this.shouldHandleAsFileUrl(url) && url.pathname) {
84 let pathname;
85 try {
86 pathname = path_1.posix.normalize(decodeURIComponent(url.pathname));
87 }
88 catch (_a) {
89 return undefined;
90 }
91 const path = this.filesystemPathForPathname(pathname);
92 if (path && pathIsInside(path, this.resolvedComponentDir)) {
93 return path;
94 }
95 }
96 return undefined;
97 }
98}
99exports.PackageUrlResolver = PackageUrlResolver;
100//# sourceMappingURL=package-url-resolver.js.map
\No newline at end of file