UNPKG

5.48 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14/// <reference types="node" />
15import { Analyzer, PackageRelativeUrl, ResolvedUrl, UrlLoader, Warning } from 'polymer-analyzer';
16import { ProjectConfig } from 'polymer-project-config';
17import { LocalFsPath } from './path-transformers';
18import File = require('vinyl');
19export interface DocumentDeps {
20 imports: PackageRelativeUrl[];
21 scripts: PackageRelativeUrl[];
22 styles: PackageRelativeUrl[];
23}
24export interface DepsIndex {
25 depsToFragments: Map<PackageRelativeUrl, PackageRelativeUrl[]>;
26 fragmentToDeps: Map<PackageRelativeUrl, PackageRelativeUrl[]>;
27 fragmentToFullDeps: Map<PackageRelativeUrl, DocumentDeps>;
28}
29export declare class BuildAnalyzer {
30 /** If null is given, we do not log warnings. */
31 private readonly streamToWarnTo;
32 config: ProjectConfig;
33 loader: StreamLoader;
34 analyzer: Analyzer;
35 started: boolean;
36 sourceFilesLoaded: boolean;
37 private _sourcesStream;
38 private _sourcesProcessingStream;
39 private _dependenciesStream;
40 private _dependenciesProcessingStream;
41 private _warningsFilter;
42 files: Map<PackageRelativeUrl, File>;
43 warnings: Set<Warning>;
44 allFragmentsToAnalyze: Set<LocalFsPath>;
45 analyzeDependencies: Promise<DepsIndex>;
46 _dependencyAnalysis: DepsIndex;
47 _resolveDependencyAnalysis: (index: DepsIndex) => void;
48 constructor(config: ProjectConfig,
49 /** If null is given, we do not log warnings. */
50 streamToWarnTo?: (NodeJS.WriteStream | null));
51 /**
52 * Start analysis by setting up the sources and dependencies analysis
53 * pipelines and starting the source stream. Files will not be loaded from
54 * disk until this is called. Can be called multiple times but will only run
55 * set up once.
56 */
57 startAnalysis(): void;
58 /**
59 * Return _dependenciesOutputStream, which will contain fully loaded file
60 * objects for each dependency after analysis.
61 */
62 dependencies(): NodeJS.ReadableStream;
63 /**
64 * Return _sourcesOutputStream, which will contain fully loaded file
65 * objects for each source after analysis.
66 */
67 sources(): NodeJS.ReadableStream;
68 /**
69 * Resolve a file in our loader so that the analyzer can read it.
70 */
71 resolveFile(file: File): void;
72 /**
73 * Analyze a file to find additional dependencies to load. Currently we only
74 * get dependencies for application fragments. When all fragments are
75 * analyzed, we call _done() to signal that analysis is complete.
76 */
77 analyzeFile(file: File): Promise<void>;
78 /**
79 * Perform some checks once we know that `_sourcesStream` is done loading.
80 */
81 private onSourcesStreamComplete;
82 /**
83 * Helper function for emitting a general analysis error onto both file
84 * streams.
85 */
86 private emitAnalysisError;
87 /**
88 * Called when analysis is complete and there are no more files to analyze.
89 * Checks for serious errors before resolving its dependency analysis and
90 * ending the dependency stream (which it controls).
91 */
92 private _done;
93 getFile(filepath: LocalFsPath): File | undefined;
94 getFileByUrl(url: PackageRelativeUrl): File | undefined;
95 /**
96 * A side-channel to add files to the loader that did not come through the
97 * stream transformation. This is for generated files, like
98 * shared-bundle.html. This should probably be refactored so that the files
99 * can be injected into the stream.
100 */
101 addFile(file: File): void;
102 printWarnings(): void;
103 private countWarningsByType;
104 /**
105 * Attempts to retreive document-order transitive dependencies for `url`.
106 */
107 _getDependencies(url: ResolvedUrl): Promise<DocumentDeps>;
108 _addDependencies(filePath: LocalFsPath, deps: DocumentDeps): void;
109 /**
110 * Check that the source stream has not already completed loading by the
111 * time
112 * this file was analyzed.
113 */
114 sourcePathAnalyzed(filePath: LocalFsPath): void;
115 /**
116 * Push the given filepath into the dependencies stream for loading.
117 * Each dependency is only pushed through once to avoid duplicates.
118 */
119 dependencyPathAnalyzed(filePath: LocalFsPath): void;
120}
121export declare type ResolveFileCallback = (a: string) => void;
122export declare type RejectFileCallback = (err: Error) => void;
123export declare type DeferredFileCallbacks = {
124 resolve: ResolveFileCallback;
125 reject: RejectFileCallback;
126};
127export declare class StreamLoader implements UrlLoader {
128 config: ProjectConfig;
129 private _buildAnalyzer;
130 deferredFiles: Map<LocalFsPath, DeferredFileCallbacks>;
131 constructor(buildAnalyzer: BuildAnalyzer);
132 hasDeferredFile(filePath: LocalFsPath): boolean;
133 hasDeferredFiles(): boolean;
134 resolveDeferredFile(filePath: LocalFsPath, file: File): void;
135 rejectDeferredFile(filePath: LocalFsPath, err: Error): void;
136 canLoad(url: ResolvedUrl): boolean;
137 load(url: ResolvedUrl): Promise<string>;
138}