1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const chai_1 = require("chai");
|
17 | const File = require("vinyl");
|
18 | const path = require("path");
|
19 | const stream = require("stream");
|
20 | const polymer_project_1 = require("../polymer-project");
|
21 | const html_splitter_1 = require("../html-splitter");
|
22 | const testProjectRoot = path.resolve('test-fixtures/splitter-project');
|
23 | suite('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 |
|
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 |
|
\ | No newline at end of file |