UNPKG

5.58 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 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const chai_1 = require("chai");
17const File = require("vinyl");
18const path = require("path");
19const stream = require("stream");
20const polymer_project_1 = require("../polymer-project");
21const html_splitter_1 = require("../html-splitter");
22const testProjectRoot = path.resolve('test-fixtures/splitter-project');
23suite('HtmlSplitter', () => {
24 let defaultProject;
25 const unroot = ((p) => p.substring(testProjectRoot.length + 1));
26 setup(() => {
27 defaultProject = new polymer_project_1.PolymerProject({
28 root: 'test-fixtures/splitter-project/',
29 entrypoint: 'index.html',
30 shell: 'shell.html',
31 sources: [
32 'source-dir/**',
33 ],
34 });
35 });
36 test('splits scripts', (done) => {
37 const htmlSplitter = new html_splitter_1.HtmlSplitter();
38 const splitFiles = new Map();
39 defaultProject.sources()
40 .pipe(htmlSplitter.split())
41 .on('data', (f) => splitFiles.set(unroot(f.path), f))
42 .pipe(htmlSplitter.rejoin())
43 .on('data', () => { })
44 .on('end', () => {
45 const expectedSplitFiles = [
46 'index.html',
47 'shell.html',
48 'shell.html_script_0.js',
49 'shell.html_script_1.js',
50 path.join('source-dir', 'my-app.html'),
51 ];
52 chai_1.assert.deepEqual(Array.from(splitFiles.keys()).sort(), expectedSplitFiles);
53 chai_1.assert.include(splitFiles.get('shell.html_script_0.js').contents.toString(), `console.log('shell');`);
54 chai_1.assert.include(splitFiles.get('shell.html_script_1.js').contents.toString(), `console.log('shell 2');`);
55 chai_1.assert.notInclude(splitFiles.get('shell.html').contents.toString(), `console.log`);
56 chai_1.assert.include(splitFiles.get('shell.html').contents.toString(), `# I am markdown`);
57 done();
58 });
59 });
60 test('rejoins scripts', (done) => {
61 const htmlSplitter = new html_splitter_1.HtmlSplitter();
62 const joinedFiles = new Map();
63 defaultProject.sources()
64 .pipe(htmlSplitter.split())
65 .pipe(htmlSplitter.rejoin())
66 .on('data', (f) => joinedFiles.set(unroot(f.path), f))
67 .on('end', () => {
68 const expectedJoinedFiles = [
69 'index.html',
70 'shell.html',
71 path.join('source-dir', 'my-app.html'),
72 ];
73 chai_1.assert.deepEqual(Array.from(joinedFiles.keys()).sort(), expectedJoinedFiles);
74 chai_1.assert.include(joinedFiles.get('shell.html').contents.toString(), `console.log`);
75 done();
76 });
77 });
78 test('handles bad paths', (done) => {
79 const htmlSplitter = new html_splitter_1.HtmlSplitter();
80 const sourceStream = new stream.Readable({
81 objectMode: true,
82 });
83 const root = path.normalize('/foo');
84 const filepath = path.join(root, '/bar/baz.html');
85 const source = '<html><head><script>fooify();</script></head><body></body></html>';
86 const file = new File({
87 cwd: root,
88 base: root,
89 path: filepath,
90 contents: Buffer.from(source),
91 });
92 sourceStream.pipe(htmlSplitter.split())
93 .on('data', (file) => {
94 // this is what gulp-html-minifier does...
95 if (path.sep === '\\' && file.path.endsWith('.html')) {
96 file.path = file.path.replace('\\', '/');
97 }
98 })
99 .pipe(htmlSplitter.rejoin())
100 .on('data', (file) => {
101 const contents = file.contents.toString();
102 chai_1.assert.equal(contents, source);
103 })
104 .on('end', done)
105 .on('error', done);
106 sourceStream.push(file);
107 sourceStream.push(null);
108 });
109 test('does not add root elements to documents', (done) => {
110 const htmlSplitter = new html_splitter_1.HtmlSplitter();
111 const joinedFiles = new Map();
112 defaultProject.sources()
113 .pipe(htmlSplitter.split())
114 .pipe(htmlSplitter.rejoin())
115 .on('data', (f) => joinedFiles.set(unroot(f.path), f))
116 .on('end', () => {
117 const expectedJoinedFiles = [
118 'index.html',
119 'shell.html',
120 path.join('source-dir', 'my-app.html'),
121 ];
122 chai_1.assert.deepEqual(Array.from(joinedFiles.keys()).sort(), expectedJoinedFiles);
123 const shell = joinedFiles.get('shell.html').contents.toString();
124 chai_1.assert.notInclude(shell, '<html', 'html element was added');
125 chai_1.assert.notInclude(shell, '<head', 'head element was added');
126 chai_1.assert.notInclude(shell, '<body', 'body element was added');
127 done();
128 });
129 });
130});
131//# sourceMappingURL=html-splitter_test.js.map
\No newline at end of file