UNPKG

9.44 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright (c) 2017 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
15import * as path from 'path';
16import {Analyzer, FsUrlResolver, Import, PackageRelativeUrl, ResolvedUrl} from 'polymer-analyzer';
17import {buildDepsIndex} from 'polymer-bundler/lib/deps-index';
18import {ProjectConfig} from 'polymer-project-config';
19
20import File = require('vinyl');
21
22import {urlFromPath, LocalFsPath} from './path-transformers';
23import {FileMapUrlLoader} from './file-map-url-loader';
24import {AsyncTransformStream} from './streams';
25
26/**
27 * Push Manifest Types Definitions
28 * A push manifest is a JSON object representing relative application URL and
29 * the resources that should be pushed when those URLs are requested by the
30 * server. Below is a example of this data format:
31 *
32 * {
33 * "index.html": { // PushManifestEntryCollection
34 * "/css/app.css": { // PushManifestEntry
35 * "type": "style", // ResourceType
36 * "weight": 1
37 * },
38 * ...
39 * },
40 * "page.html": {
41 * "/css/page.css": {
42 * "type": "style",
43 * "weight": 1
44 * },
45 * ...
46 * }
47 * }
48 *
49 * NOTE(fks) 04-05-2017: Only weight=1 is supported by browsers at the moment.
50 * When support is added, we can add automatic weighting and support multiple
51 * numbers.
52 */
53export type ResourceType = 'document'|'script'|'style'|'image'|'font';
54export interface PushManifestEntry {
55 type?: ResourceType;
56 weight?: 1;
57}
58export interface PushManifestEntryCollection {
59 [dependencyAbsoluteUrl: string]: PushManifestEntry;
60}
61export interface PushManifest {
62 [requestAbsoluteUrl: string]: PushManifestEntryCollection;
63}
64
65/**
66 * A mapping of file extensions and their default resource type.
67 */
68const extensionToTypeMapping = new Map<string, ResourceType>([
69 ['.css', 'style'],
70 ['.gif', 'image'],
71 ['.html', 'document'],
72 ['.png', 'image'],
73 ['.jpg', 'image'],
74 ['.js', 'script'],
75 ['.json', 'script'],
76 ['.svg', 'image'],
77 ['.webp', 'image'],
78 ['.woff', 'font'],
79 ['.woff2', 'font'],
80]);
81
82/**
83 * Get the default resource type for a file based on its extension.
84 */
85function getResourceTypeFromUrl(url: string): ResourceType|undefined {
86 return extensionToTypeMapping.get(path.extname(url));
87}
88
89/**
90 * Get the resource type for an import, handling special import types and
91 * falling back to getResourceTypeFromUrl() if the resource type can't be
92 * detected directly from importFeature.
93 */
94function getResourceTypeFromImport(importFeature: Import): ResourceType|
95 undefined {
96 const importKinds = importFeature.kinds;
97 if (importKinds.has('css-import') || importKinds.has('html-style')) {
98 return 'style';
99 }
100 if (importKinds.has('html-import')) {
101 return 'document';
102 }
103 if (importKinds.has('html-script')) {
104 return 'script';
105 }
106 // @NOTE(fks) 04-07-2017: A js-import can actually import multiple types of
107 // resources, so we can't guarentee that it's a script and should instead rely
108 // on the default file-extension mapping.
109 return getResourceTypeFromUrl(importFeature.url);
110}
111
112/**
113 * Create a PushManifestEntry from an analyzer Import.
114 */
115function createPushEntryFromImport(importFeature: Import): PushManifestEntry {
116 return {
117 type: getResourceTypeFromImport(importFeature),
118 weight: 1,
119 };
120}
121
122/**
123 * Analyze the given URL and resolve with a collection of push manifest entries
124 * to be added to the overall push manifest.
125 */
126async function generatePushManifestEntryForUrl(
127 analyzer: Analyzer,
128 url: ResolvedUrl): Promise<PushManifestEntryCollection> {
129 const analysis = await analyzer.analyze([url]);
130 const result = analysis.getDocument(url);
131
132 if (result.successful === false) {
133 const message = result.error && result.error.message || 'unknown';
134 throw new Error(`Unable to get document ${url}: ${message}`);
135 }
136
137 const analyzedDocument = result.value;
138 const rawImports = [...analyzedDocument.getFeatures({
139 kind: 'import',
140 externalPackages: true,
141 imported: true,
142 noLazyImports: true,
143 })];
144 const importsToPush = rawImports.filter(
145 (i) => !(i.type === 'html-import' && i.lazy) &&
146 !(i.kinds.has('html-script-back-reference')));
147 const pushManifestEntries: PushManifestEntryCollection = {};
148
149 for (const analyzedImport of importsToPush) {
150 // TODO This import URL does not respect the document's base tag.
151 // Probably an issue more generally with all URLs analyzed out of
152 // documents, but base tags are somewhat rare.
153 const analyzedImportUrl = analyzedImport.url;
154 const relativeImportUrl = analyzer.urlResolver.relative(analyzedImportUrl);
155 const analyzedImportEntry = pushManifestEntries[relativeImportUrl];
156 if (!analyzedImportEntry) {
157 pushManifestEntries[relativeImportUrl] =
158 createPushEntryFromImport(analyzedImport);
159 }
160 }
161
162 return pushManifestEntries;
163}
164
165
166/**
167 * A stream that reads in files from an application to generate an HTTP2/Push
168 * manifest that gets injected into the stream.
169 */
170export class AddPushManifest extends AsyncTransformStream<File, File> {
171 files: Map<ResolvedUrl, File>;
172 outPath: LocalFsPath;
173 private config: ProjectConfig;
174 private analyzer: Analyzer;
175 private basePath: PackageRelativeUrl;
176
177 constructor(
178 config: ProjectConfig, outPath?: LocalFsPath,
179 basePath?: PackageRelativeUrl) {
180 super({objectMode: true});
181 this.files = new Map();
182 this.config = config;
183 this.analyzer = new Analyzer({
184 urlLoader: new FileMapUrlLoader(this.files),
185 urlResolver: new FsUrlResolver(config.root),
186 });
187 this.outPath =
188 path.join(this.config.root, outPath || 'push-manifest.json') as
189 LocalFsPath;
190 this.basePath = (basePath || '') as PackageRelativeUrl;
191 }
192
193 protected async *
194 _transformIter(files: AsyncIterable<File>): AsyncIterable<File> {
195 for await (const file of files) {
196 this.files.set(
197 this.analyzer.resolveUrl(urlFromPath(
198 this.config.root as LocalFsPath, file.path as LocalFsPath))!,
199 file);
200 yield file;
201 }
202
203 // Generate a push manifest, and propagate any errors up.
204 const pushManifest = await this.generatePushManifest();
205 const pushManifestContents = JSON.stringify(pushManifest, undefined, ' ');
206 // Push the new push manifest into the stream.
207 yield new File({
208 path: this.outPath,
209 contents: Buffer.from(pushManifestContents),
210 });
211 }
212
213 async generatePushManifest(): Promise<PushManifest> {
214 // Bundler's buildDepsIndex code generates an index with all fragments and
215 // all lazy-imports encountered are the keys, so we'll use that function to
216 // produce the set of all fragments to generate push-manifest entries for.
217 const depsIndex = await buildDepsIndex(
218 this.config.allFragments.map(
219 (path) => this.analyzer.resolveUrl(urlFromPath(
220 this.config.root as LocalFsPath, path as LocalFsPath))!),
221 this.analyzer);
222 // Don't include bundler's fake "sub-bundle" URLs (e.g.
223 // "foo.html>external#1>bar.js").
224 const allFragments =
225 new Set([...depsIndex.keys()].filter((url) => !url.includes('>')));
226
227 // If an app-shell exists, use that as our main push URL because it has a
228 // reliable URL. Otherwise, support the single entrypoint URL.
229 const mainPushEntrypointUrl = this.analyzer.resolveUrl(urlFromPath(
230 this.config.root as LocalFsPath,
231 this.config.shell as LocalFsPath ||
232 this.config.entrypoint as LocalFsPath))!;
233 allFragments.add(mainPushEntrypointUrl);
234
235 // Generate the dependencies to push for each fragment.
236 const pushManifest: PushManifest = {};
237 for (const fragment of allFragments) {
238 const absoluteFragmentUrl =
239 '/' + this.analyzer.urlResolver.relative(fragment);
240 pushManifest[absoluteFragmentUrl] =
241 await generatePushManifestEntryForUrl(this.analyzer, fragment);
242 }
243
244 // The URLs we got may be absolute or relative depending on how they were
245 // declared in the source. This will normalize them to relative by stripping
246 // any leading slash.
247 //
248 // TODO Decide whether they should really be relative or absolute. Relative
249 // was chosen here only because most links were already relative so it was
250 // a smaller change, but
251 // https://github.com/GoogleChrome/http2-push-manifest actually shows
252 // relative for the keys and absolute for the values.
253 const normalize = (p: string) =>
254 path.posix.join(this.basePath, p).replace(/^\/+/, '');
255
256 const normalized: PushManifest = {};
257 for (const source of Object.keys(pushManifest)) {
258 const targets: PushManifestEntryCollection = {};
259 for (const target of Object.keys(pushManifest[source])) {
260 targets[normalize(target)] = pushManifest[source][target];
261 }
262 normalized[normalize(source)] = targets;
263 }
264 return normalized;
265 }
266}