UNPKG

2.59 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 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const streams_1 = require("./streams");
25/**
26 * This is a `UrlLoader` for use with a `polymer-analyzer` that reads files
27 * that have been gathered by a `BuildBundler` transform stream.
28 */
29class FileMapUrlLoader {
30 constructor(files, fallbackLoader) {
31 this.files = files;
32 this.fallbackLoader = fallbackLoader;
33 }
34 // Return true if we can return load the given url.
35 canLoad(url) {
36 return !!(this.files.has(url) ||
37 this.fallbackLoader && this.fallbackLoader.canLoad(url));
38 }
39 // Try to load the file from the map. If not in the map, try to load
40 // from the fallback loader.
41 load(url) {
42 return __awaiter(this, void 0, void 0, function* () {
43 const file = this.files.get(url);
44 if (file == null) {
45 if (this.fallbackLoader) {
46 if (this.fallbackLoader.canLoad(url)) {
47 return this.fallbackLoader.load(url);
48 }
49 throw new Error(`${url} not present in file map and fallback loader can not load.`);
50 }
51 throw new Error(`${url} not present in file map and no fallback loader.`);
52 }
53 return streams_1.getFileContents(file);
54 });
55 }
56}
57exports.FileMapUrlLoader = FileMapUrlLoader;
58//# sourceMappingURL=file-map-url-loader.js.map
\No newline at end of file