-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathinline-critical-css_spec.ts
136 lines (119 loc) · 4.18 KB
/
inline-critical-css_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { tags } from '@angular-devkit/core';
import { InlineCriticalCssProcessor } from './inline-critical-css';
describe('InlineCriticalCssProcessor', () => {
const styles: Record<string, string> = {
'/dist/styles.css': `
body { margin: 0; }
html { color: white; }
`,
'/dist/theme.css': `
span { color: blue; }
p { color: blue; }
`,
};
const readAsset = async (file: string): Promise<string> => {
const content = styles[file];
if (content) {
return content;
}
throw new Error('Cannot read asset.');
};
const getContent = (deployUrl: string, bodyContent = ''): string => {
return `
<html>
<head>
<link href="${deployUrl}styles.css" rel="stylesheet">
<link href="${deployUrl}theme.css" rel="stylesheet">
</head>
<body>${bodyContent}</body>
</html>`;
};
it('should inline critical css', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
});
const { content } = await inlineFontsProcessor.process(getContent(''), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).not.toContain('color: blue');
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style>
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should inline critical css when using deployUrl', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
deployUrl: 'http://cdn.com',
});
const { content } = await inlineFontsProcessor.process(getContent('http://cdn.com/'), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="http://cdn.com/styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="http://cdn.com/theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style>
body { margin: 0; }
html { color: white; }
</style>`);
});
it('should compress inline critical css when minify is enabled', async () => {
const inlineFontsProcessor = new InlineCriticalCssProcessor({
readAsset,
minify: true,
});
const { content } = await inlineFontsProcessor.process(getContent(''), {
outputPath: '/dist/',
});
expect(content).toContain(
`<link href="styles.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain(
`<link href="theme.css" rel="stylesheet" media="print" onload="this.media='all'">`,
);
expect(content).toContain('<style>body{margin:0}html{color:white}</style>');
});
it('should process the inline `onload` handlers if a CSP nonce is specified', async () => {
const inlineCssProcessor = new InlineCriticalCssProcessor({
readAsset,
});
const { content } = await inlineCssProcessor.process(
getContent('', '<app ngCspNonce="{% nonce %}"></app>'),
{
outputPath: '/dist/',
},
);
expect(content).toContain(
'<link href="styles.css" rel="stylesheet" media="print" ngCspMedia="all">',
);
expect(content).toContain(
'<link href="theme.css" rel="stylesheet" media="print" ngCspMedia="all">',
);
// Nonces shouldn't be added inside the `noscript` tags.
expect(content).toContain('<noscript><link rel="stylesheet" href="theme.css"></noscript>');
expect(content).toContain('<script nonce="{% nonce %}">');
expect(tags.stripIndents`${content}`).toContain(tags.stripIndents`
<style nonce="{% nonce %}">
body { margin: 0; }
html { color: white; }
</style>`);
});
});