UNPKG

8.17 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 The complete set of authors may be found
7 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
8 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
9 * Google as part of the polymer project is also subject to an additional IP
10 * rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13 return new (P || (P = Promise))(function (resolve, reject) {
14 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
17 step((generator = generator.apply(thisArg, _arguments || [])).next());
18 });
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21const chai_1 = require("chai");
22const polymer_analyzer_1 = require("polymer-analyzer");
23const polymer_project_1 = require("../polymer-project");
24const prefetch_links_1 = require("../prefetch-links");
25const util_1 = require("./util");
26const mergeStream = require('merge-stream');
27suite('prefetch-links', () => {
28 suite('AddPrefetchLinks', () => {
29 test('adds prefetch links for transitive deps of unbundled', () => __awaiter(this, void 0, void 0, function* () {
30 const project = new polymer_project_1.PolymerProject({
31 root: 'test-fixtures/bundle-project/',
32 entrypoint: 'index.html',
33 });
34 const files = yield util_1.emittedFiles(mergeStream(project.sources(), project.dependencies())
35 .pipe(project.addPrefetchLinks()), project.config.root);
36 const html = files.get('index.html').contents.toString();
37 // No prefetch links needed for direct dependency.
38 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/simple-import.html">');
39 // Prefetch added for the transitive dependencies of `index.html`,
40 // which are all direct dependencies of `simple-import.html`.
41 chai_1.assert.include(html, '<link rel="prefetch" href="/simple-script.js">');
42 chai_1.assert.include(html, '<link rel="prefetch" href="/simple-style.css">');
43 chai_1.assert.include(html, '<link rel="prefetch" href="/simple-import-2.html">');
44 }));
45 test('add prefetch links for transitive deps of bundled', () => __awaiter(this, void 0, void 0, function* () {
46 const project = new polymer_project_1.PolymerProject({
47 root: 'test-fixtures/bundle-project/',
48 entrypoint: 'index.html',
49 fragments: ['simple-import.html'],
50 });
51 const files = yield util_1.emittedFiles(mergeStream(project.sources(), project.dependencies())
52 .pipe(project.bundler({ inlineScripts: false }))
53 .pipe(project.addPrefetchLinks()), project.config.root);
54 const expectedFiles = ['index.html', 'simple-import.html', 'simple-script.js'];
55 chai_1.assert.deepEqual(expectedFiles, [...files.keys()].sort());
56 const html = files.get('index.html').contents.toString();
57 // `simple-import.html` is a direct dependency, so we should not add
58 // prefetch link to it.
59 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/simple-import.html">');
60 // `simple-import.html` has inlined `simple-import-2.html` which has an
61 // external script import `simple-script.js`. A prefetch link is added
62 // for `simple-script.js` because it is a transitive dependency of the
63 // `index.html`
64 chai_1.assert.include(html, '<link rel="prefetch" href="/simple-script.js">');
65 }));
66 test('prefetch links do not include lazy dependencies', () => __awaiter(this, void 0, void 0, function* () {
67 const project = new polymer_project_1.PolymerProject({
68 root: 'test-fixtures/bundler-data/',
69 entrypoint: 'index.html',
70 });
71 const files = yield util_1.emittedFiles(mergeStream(project.sources(), project.dependencies())
72 .pipe(project.addPrefetchLinks()), project.config.root);
73 const html = files.get('index.html').contents.toString();
74 // Shell is a direct dependency, so should not have a prefetch link.
75 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/shell.html">');
76 // Framework is in the shell, so is a transitive dependency of index, and
77 // should be prefetched.
78 chai_1.assert.include(html, '<link rel="prefetch" href="/framework.html">');
79 // These are lazy imports and should not be prefetched.
80 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/entrypoint-a.html">');
81 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/entrypoint-b.html">');
82 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/entrypoint-c.html">');
83 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/common-dependency.html">');
84 chai_1.assert.notInclude(html, '<link rel="prefetch" href="/lazy-dependency.html">');
85 }));
86 test('prefetch links are relative when base tag present', () => __awaiter(this, void 0, void 0, function* () {
87 const project = new polymer_project_1.PolymerProject({
88 root: 'test-fixtures/differential-serving/',
89 entrypoint: 'index.html',
90 fragments: ['shell.html'],
91 });
92 const files = yield util_1.emittedFiles(mergeStream(project.sources(), project.dependencies())
93 .pipe(project.bundler({ inlineScripts: false }))
94 .pipe(project.addPrefetchLinks()), project.config.root);
95 const html = files.get('index.html').contents.toString();
96 // The `external-script.js` file is imported by `shell.html` so is
97 // transitive dependency of `index.html`. Because `index.html` has a base
98 // tag with an href, the prefetch is a relative URL.
99 chai_1.assert.include(html, '<link rel="prefetch" href="shell-stuff/external-script.js">');
100 }));
101 });
102 suite('createLinks', () => {
103 const urlResolver = new polymer_analyzer_1.FsUrlResolver('');
104 const html = '<html><body>foo</body></html>';
105 const htmlWithBase = '<html><base href="/base/"><body>foo</body></html>';
106 const deps = new Set([
107 'bower_components/polymer/polymer.html',
108 'src/my-icons.html',
109 ].map((u) => urlResolver.resolve(u)));
110 test('with no base tag and absolute true', () => {
111 const url = 'index.html';
112 const expected = ('<html>' +
113 '<link rel="prefetch" href="/bower_components/polymer/polymer.html">' +
114 '<link rel="prefetch" href="/src/my-icons.html">' +
115 '<body>foo</body></html>');
116 const actual = prefetch_links_1.createLinks(urlResolver, html, urlResolver.resolve(url), deps, true);
117 chai_1.assert.equal(actual, expected);
118 });
119 test('with a base tag and absolute true', () => {
120 const url = 'index.html';
121 const expected = ('<html><base href="/base/">' +
122 '<link rel="prefetch" href="bower_components/polymer/polymer.html">' +
123 '<link rel="prefetch" href="src/my-icons.html">' +
124 '<body>foo</body></html>');
125 const actual = prefetch_links_1.createLinks(urlResolver, htmlWithBase, urlResolver.resolve(url), deps, true);
126 chai_1.assert.equal(actual, expected);
127 });
128 });
129});
130//# sourceMappingURL=prefetch-links_test.js.map
\No newline at end of file