-
Notifications
You must be signed in to change notification settings - Fork 243
/
SeparateKeyframes.js
218 lines (194 loc) · 6.77 KB
/
SeparateKeyframes.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const {insertText, createElement, hasAttribute, firstChildByTag} = require('../NodeUtils');
const safeParser = require('postcss-safe-parser');
const postcss = require('postcss');
const cssnano = require('cssnano-simple');
const allowedKeyframeProps = new Set([
'animation-timing-function',
'offset-distance',
'opacity',
'visibility',
'transform',
'-webkit-transform',
'-moz-transform',
'-o-transform',
'-ms-transform',
]);
/**
* SeparateKeyframes - moves keyframes, media, and support from amp-custom
* to amp-keyframes.
*
* This transformer supports the following options:
*
* - `minify [Boolean]`: compresses the CSS. The default is `true`.
*/
class SeparateKeyframes {
constructor(config) {
this.log_ = config.log.tag('SeparateKeyframes');
this.minify = config.minify !== false;
}
async transform(tree) {
const html = firstChildByTag(tree, 'html');
if (!html) return;
const head = firstChildByTag(html, 'head');
if (!head) return;
const body = firstChildByTag(html, 'body') || head;
if (this.isAmpStory(body)) {
return;
}
let stylesCustomTag;
let stylesKeyframesTag;
// Get style[amp-custom] and remove style[amp-keyframes]
head.children = head.children.filter((tag) => {
if (tag.tagName !== 'style') return true;
if (!stylesKeyframesTag && hasAttribute(tag, 'amp-keyframes')) {
stylesKeyframesTag = tag;
return false;
}
if (!stylesCustomTag && hasAttribute(tag, 'amp-custom')) {
stylesCustomTag = tag;
}
return true;
});
const extraPlugins = this.minify ? [cssnano] : [];
// If no custom styles, there's nothing to do
if (!stylesCustomTag) return;
let stylesText = stylesCustomTag.children[0];
if (!stylesText || !stylesText.data) return;
stylesText = stylesText.data;
// initialize an empty keyframes tree
const keyframesTree = postcss.parse('');
const isInvalidKeyframe = (keyframe) => {
let invalidProperty;
for (const frame of keyframe.nodes) {
for (const declaration of frame.nodes) {
if (!allowedKeyframeProps.has(declaration.prop)) {
invalidProperty = declaration.prop;
break;
}
}
if (invalidProperty) break;
}
return invalidProperty;
};
const keyframesPlugin = () => {
const logInvalid_ = this.logInvalid.bind(this);
return {
postcssPlugin: 'postcss-amp-keyframes-mover',
Once(root) {
root.nodes = root.nodes.filter((rule) => {
if (rule.name === 'keyframes') {
// We can't move a keyframe with an invalid property
// or else the style[amp-keyframes] is invalid
const invalidProperty = isInvalidKeyframe(rule);
if (invalidProperty) {
logInvalid_(rule.name, invalidProperty);
return true;
}
keyframesTree.nodes.push(rule);
return false;
}
// if rule has any keyframes duplicate rule and move just
// the keyframes
if (rule.name === 'media' || rule.name === 'supports') {
const copiedRule = Object.assign({}, rule, {nodes: []});
rule.nodes = rule.nodes.filter((rule) => {
if (rule.name !== 'keyframes') return true;
const invalidProperty = isInvalidKeyframe(rule);
if (invalidProperty) {
logInvalid_(rule.name, invalidProperty);
return true;
}
copiedRule.nodes.push(rule);
});
if (copiedRule.nodes.length) {
keyframesTree.nodes.push(copiedRule);
}
// if no remaining rules remove it
return rule.nodes.length;
}
return true;
});
},
};
};
keyframesPlugin.postcss = true;
const {css: cssResult} = await postcss([...extraPlugins, keyframesPlugin])
.process(stylesText, {
from: undefined,
parser: safeParser,
})
.catch((err) => {
this.log_.warn(`Failed to process CSS`, err.message);
return {css: stylesText};
});
// if no rules moved nothing to do
if (keyframesTree.nodes.length === 0) {
// re-serialize to compress the CSS
stylesCustomTag.children[0].data = cssResult;
return;
}
if (!stylesKeyframesTag) {
// Check body for keyframes tag, removing it if found
body.children = body.children.filter((tag) => {
if (tag.tagName === 'style' && hasAttribute(tag, 'amp-keyframes')) {
stylesKeyframesTag = tag;
return false;
}
return true;
});
if (!stylesKeyframesTag) {
stylesKeyframesTag = createElement('style', {'amp-keyframes': ''});
}
}
// Insert keyframes styles to Node
const keyframesTextNode = stylesKeyframesTag.children[0];
const currentKeyframesTree = postcss.parse((keyframesTextNode && keyframesTextNode.data) || '');
currentKeyframesTree.nodes = keyframesTree.nodes.concat(currentKeyframesTree.nodes);
let keyframesText = '';
postcss.stringify(currentKeyframesTree, (part) => {
keyframesText += part;
});
// if we have extra plugins make sure process the keyframes CSS with them
if (extraPlugins.length > 0) {
const cssResult = await postcss(extraPlugins).process(keyframesText, {
from: undefined,
parser: safeParser,
});
keyframesText = cssResult.css;
}
if (!keyframesTextNode) {
insertText(stylesKeyframesTag, keyframesText);
} else {
keyframesTextNode.data = keyframesText;
}
// Add keyframes tag to end of body
body.children.push(stylesKeyframesTag);
// Update stylesCustomTag with filtered styles
stylesCustomTag.children[0].data = cssResult;
}
logInvalid(name, property) {
this.log_.warn(
`Found invalid keyframe property '${property}' in '${name}' not moving to style[amp-keyframes]`
);
}
isAmpStory(body) {
return body.children.some((child) => child.tagName === 'amp-story');
}
}
module.exports = SeparateKeyframes;