UNPKG

8.13 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 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 __asyncValues = (this && this.__asyncValues) || function (o) {
16 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17 var m = o[Symbol.asyncIterator], i;
18 return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
19 function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
20 function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
21};
22var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
23var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
24 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25 var g = generator.apply(thisArg, _arguments || []), i, q = [];
26 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
27 function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
28 function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
29 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
30 function fulfill(value) { resume("next", value); }
31 function reject(value) { resume("throw", value); }
32 function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
33};
34Object.defineProperty(exports, "__esModule", { value: true });
35const dom5 = require("dom5/lib/index-next");
36const parse5 = require("parse5");
37const path = require("path");
38const polymer_analyzer_1 = require("polymer-analyzer");
39const File = require("vinyl");
40const path_transformers_1 = require("./path-transformers");
41const file_map_url_loader_1 = require("./file-map-url-loader");
42const streams_1 = require("./streams");
43/**
44 * A stream that modifies HTML files to include prefetch links for all of the
45 * file's transitive dependencies.
46 */
47class AddPrefetchLinks extends streams_1.AsyncTransformStream {
48 constructor(config) {
49 super({ objectMode: true });
50 this.files = new Map();
51 this._config = config;
52 this._analyzer =
53 new polymer_analyzer_1.Analyzer({ urlLoader: new file_map_url_loader_1.FileMapUrlLoader(this.files) });
54 }
55 _transformIter(files) {
56 return __asyncGenerator(this, arguments, function* _transformIter_1() {
57 var e_1, _a;
58 const htmlFileUrls = [];
59 try {
60 // Map all files; pass-through all non-HTML files.
61 for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield __await(files_1.next()), !files_1_1.done;) {
62 const file = files_1_1.value;
63 const fileUrl = this._analyzer.resolveUrl(path_transformers_1.urlFromPath(this._config.root, file.path));
64 this.files.set(fileUrl, file);
65 if (path.extname(file.path) !== '.html') {
66 yield yield __await(file);
67 }
68 else {
69 htmlFileUrls.push(fileUrl);
70 }
71 }
72 }
73 catch (e_1_1) { e_1 = { error: e_1_1 }; }
74 finally {
75 try {
76 if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield __await(_a.call(files_1));
77 }
78 finally { if (e_1) throw e_1.error; }
79 }
80 // Analyze each HTML file and add prefetch links.
81 const analysis = yield __await(this._analyzer.analyze(htmlFileUrls));
82 for (const documentUrl of htmlFileUrls) {
83 const result = analysis.getDocument(documentUrl);
84 if (result.successful === false) {
85 const message = result.error && result.error.message;
86 console.warn(`Unable to get document ${documentUrl}: ${message}`);
87 continue;
88 }
89 const document = result.value;
90 const allDependencyUrls = [...document.getFeatures({
91 kind: 'import',
92 externalPackages: true,
93 imported: true,
94 noLazyImports: true
95 })].filter((d) => d.document !== undefined && !d.lazy)
96 .map((d) => d.document.url);
97 const directDependencyUrls = [...document.getFeatures({
98 kind: 'import',
99 externalPackages: true,
100 imported: false,
101 noLazyImports: true
102 })].filter((d) => d.document !== undefined && !d.lazy)
103 .map((d) => d.document.url);
104 const onlyTransitiveDependencyUrls = allDependencyUrls.filter((d) => directDependencyUrls.indexOf(d) === -1);
105 // No need to transform a file if it has no dependencies to prefetch.
106 if (onlyTransitiveDependencyUrls.length === 0) {
107 yield yield __await(this.files.get(documentUrl));
108 continue;
109 }
110 const prefetchUrls = new Set(onlyTransitiveDependencyUrls);
111 const html = createLinks(this._analyzer.urlResolver, document.parsedDocument.contents, document.parsedDocument.baseUrl, prefetchUrls, document.url ===
112 this._analyzer.resolveUrl(path_transformers_1.urlFromPath(this._config.root, this._config.entrypoint)));
113 const filePath = path_transformers_1.pathFromUrl(this._config.root, this._analyzer.urlResolver.relative(documentUrl));
114 yield yield __await(new File({ contents: Buffer.from(html, 'utf-8'), path: filePath }));
115 }
116 });
117 }
118}
119exports.AddPrefetchLinks = AddPrefetchLinks;
120/**
121 * Returns the given HTML updated with import or prefetch links for the given
122 * dependencies. The given url and deps are expected to be project-relative
123 * URLs (e.g. "index.html" or "src/view.html") unless absolute parameter is
124 * `true` and there is no base tag in the document.
125 */
126function createLinks(urlResolver, html, baseUrl, deps, absolute = false) {
127 const ast = parse5.parse(html, { locationInfo: true });
128 const baseTag = dom5.query(ast, dom5.predicates.hasTagName('base'));
129 const baseTagHref = baseTag ? dom5.getAttribute(baseTag, 'href') : '';
130 // parse5 always produces a <head> element.
131 const head = dom5.query(ast, dom5.predicates.hasTagName('head'));
132 for (const dep of deps) {
133 let href;
134 if (absolute && !baseTagHref) {
135 href = absUrl(urlResolver.relative(dep));
136 }
137 else {
138 href = urlResolver.relative(baseUrl, dep);
139 }
140 const link = dom5.constructors.element('link');
141 dom5.setAttribute(link, 'rel', 'prefetch');
142 dom5.setAttribute(link, 'href', href);
143 dom5.append(head, link);
144 }
145 dom5.removeFakeRootElements(ast);
146 return parse5.serialize(ast);
147}
148exports.createLinks = createLinks;
149function absUrl(url) {
150 return (url.startsWith('/') ? url : '/' + url);
151}
152//# sourceMappingURL=prefetch-links.js.map
\No newline at end of file