1 | ;
|
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 | */
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | /**
|
17 | * CODE ADAPTED FROM THE "SLASH" LIBRARY BY SINDRE SORHUS
|
18 | * https://github.com/sindresorhus/slash
|
19 | *
|
20 | * ORIGINAL LICENSE:
|
21 | * The MIT License (MIT)
|
22 | *
|
23 | * Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)*
|
24 | *
|
25 | * Permission is hereby granted, free of charge, to any person obtaining a copy*
|
26 | * of this software and associated documentation files (the "Software"), to
|
27 | * deal*
|
28 | * in the Software without restriction, including without limitation the rights*
|
29 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell*
|
30 | * copies of the Software, and to permit persons to whom the Software is*
|
31 | * furnished to do so, subject to the following conditions:*
|
32 | *
|
33 | * The above copyright notice and this permission notice shall be included in*
|
34 | * all copies or substantial portions of the Software.*
|
35 | *
|
36 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*
|
37 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*
|
38 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE*
|
39 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*
|
40 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
41 | * FROM,*
|
42 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN*
|
43 | * THE SOFTWARE.
|
44 | */
|
45 | /**
|
46 | * This module consists of functions for transformations to filesystem and url
|
47 | * paths.
|
48 | * TODO(usergenic): We should consider migrating the responsibility of
|
49 | * path-related string transformation to a package like `upath`.
|
50 | * Please see: https://www.npmjs.com/package/upath
|
51 | */
|
52 | const path = require("path");
|
53 | /**
|
54 | * Returns a properly encoded URL representing the relative URL from the root
|
55 | * to the target. This function will throw an error if the target is outside
|
56 | * the root. We use this to map a file from the filesystem to the relative
|
57 | * URL that represents it in the build.
|
58 | */
|
59 | function urlFromPath(root, target) {
|
60 | const targetPosix = posixifyPath(target);
|
61 | const rootPosix = posixifyPath(root);
|
62 | const relativePath = path.posix.relative(rootPosix, targetPosix);
|
63 | // The startsWith(root) check is important on Windows because of the case
|
64 | // where paths have different drive letters. The startsWith('../') will
|
65 | // catch the general not-in-root case.
|
66 | if (!targetPosix.startsWith(posixifyPath(root)) ||
|
67 | relativePath.startsWith('../')) {
|
68 | throw new Error(`target path is not in root: ${target} (${root})`);
|
69 | }
|
70 | return encodeURI(relativePath);
|
71 | }
|
72 | exports.urlFromPath = urlFromPath;
|
73 | /**
|
74 | * Returns a filesystem path for the url, relative to the root.
|
75 | */
|
76 | function pathFromUrl(root,
|
77 | // TODO(usergenic): PackageRelativeUrl are not *necessarily* always just a
|
78 | // relative path from root. Maybe subclass as PackageRelativeUrlPath or
|
79 | // something if this function doesn't disappear after
|
80 | // https://github.com/Polymer/polymer-build/issues/324 is addressed.
|
81 | url) {
|
82 | return path.normalize(decodeURIComponent(path.posix.join(posixifyPath(root), path.posix.join('/', url))));
|
83 | }
|
84 | exports.pathFromUrl = pathFromUrl;
|
85 | /**
|
86 | * Returns a string where all Windows path separators are converted to forward
|
87 | * slashes.
|
88 | * NOTE(usergenic): We will generate only canonical Windows paths, but this
|
89 | * function is exported so that we can create a forward-slashed Windows root
|
90 | * path when dealing with the `sw-precache` library, which uses `glob` npm
|
91 | * module generates only forward-slash paths in building its `precacheConfig`
|
92 | * map.
|
93 | */
|
94 | function posixifyPath(filepath) {
|
95 | // We don't want to change backslashes to forward-slashes in the case where
|
96 | // we're already on posix environment, because they would be intentional in
|
97 | // that case (albeit weird.)
|
98 | if (path.sep === '\\') {
|
99 | filepath = filepath.replace(/\\/g, '/');
|
100 | }
|
101 | return filepath;
|
102 | }
|
103 | exports.posixifyPath = posixifyPath;
|
104 | //# sourceMappingURL=path-transformers.js.map |
\ | No newline at end of file |