This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfile-map-url-loader.ts
57 lines (50 loc) · 1.88 KB
/
file-map-url-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import File = require('vinyl');
import {UrlLoader} from 'polymer-analyzer';
import {parseUrl} from 'polymer-analyzer/lib/core/utils';
import {getFileContents} from './streams';
/**
* This is a `UrlLoader` for use with a `polymer-analyzer` that reads files
* that have been gathered by a `BuildBundler` transform stream.
*/
export class FileMapUrlLoader implements UrlLoader {
files: Map<string, File>;
fallbackLoader?: UrlLoader;
constructor(files: Map<string, File>, fallbackLoader?: UrlLoader) {
this.files = files;
this.fallbackLoader = fallbackLoader;
}
// Return true if we can return load the given url.
canLoad(url: string): boolean {
return this.files.has(url) ||
this.fallbackLoader && this.fallbackLoader.canLoad(url);
}
// Try to load the file from the map. If not in the map, try to load
// from the fallback loader.
async load(url: string): Promise<string> {
const file = this.files.get(parseUrl(url).pathname)!;
if (file == null) {
if (this.fallbackLoader) {
if (this.fallbackLoader.canLoad(url)) {
return this.fallbackLoader.load(url);
}
throw new Error(
`${url} not present in file map and fallback loader can not load.`);
}
throw new Error(`${url} not present in file map and no fallback loader.`);
}
return getFileContents(file);
}
}