Skip to content

Commit 877fbef

Browse files
committed
Make the compiler also transform prefixed @Keyframes
1 parent 5479973 commit 877fbef

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

src/css/Stylesheet.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getLocator } from 'locate-character';
44
import Selector from './Selector';
55
import getCodeFrame from '../utils/getCodeFrame';
66
import hash from '../utils/hash';
7+
import isKeyframesNode from '../utils/isKeyframesNode';
78
import Element from '../compile/nodes/Element';
89
import { Validator } from '../validate/index';
910
import { Node, Ast, Warning } from '../interfaces';
@@ -26,7 +27,7 @@ class Rule {
2627
}
2728

2829
isUsed(dev: boolean) {
29-
if (this.parent && this.parent.node.type === 'Atrule' && this.parent.node.name === 'keyframes') return true;
30+
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
3031
if (this.declarations.length === 0) return dev;
3132
return this.selectors.some(s => s.used);
3233
}
@@ -67,7 +68,7 @@ class Rule {
6768
}
6869

6970
transform(code: MagicString, id: string, keyframes: Map<string, string>) {
70-
if (this.parent && this.parent.node.type === 'Atrule' && this.parent.node.name === 'keyframes') return true;
71+
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
7172

7273
const attr = `.${id}`;
7374

@@ -142,7 +143,7 @@ class Atrule {
142143
});
143144
}
144145

145-
else if (this.node.name === 'keyframes') {
146+
else if (isKeyframesNode(this.node)) {
146147
this.children.forEach((rule: Rule) => {
147148
rule.selectors.forEach(selector => {
148149
selector.used = true;
@@ -167,8 +168,8 @@ class Atrule {
167168
});
168169

169170
code.remove(c, this.node.block.start);
170-
} else if (this.node.name === 'keyframes') {
171-
let c = this.node.start + 10;
171+
} else if (isKeyframesNode(this.node)) {
172+
let c = this.node.start + this.node.name.length + 1;
172173
if (this.node.expression.start - c > 1) code.overwrite(c, this.node.expression.start, ' ');
173174
c = this.node.expression.end;
174175
if (this.node.block.start - c > 0) code.remove(c, this.node.block.start);
@@ -200,7 +201,7 @@ class Atrule {
200201
}
201202

202203
transform(code: MagicString, id: string, keyframes: Map<string, string>) {
203-
if (this.node.name === 'keyframes') {
204+
if (isKeyframesNode(this.node)) {
204205
this.node.expression.children.forEach(({ type, name, start, end }: Node) => {
205206
if (type === 'Identifier') {
206207
if (name.startsWith('-global-')) {
@@ -287,7 +288,7 @@ export default class Stylesheet {
287288
this.children.push(atrule);
288289
}
289290

290-
if (node.name === 'keyframes') {
291+
if (isKeyframesNode(node)) {
291292
node.expression.children.forEach((expression: Node) => {
292293
if (expression.type === 'Identifier' && !expression.name.startsWith('-global-')) {
293294
this.keyframes.set(expression.name, `${this.id}-${expression.name}`);

src/utils/isKeyframesNode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Node } from '../interfaces';
2+
3+
export default function isKeyframesNode(node: Node): boolean {
4+
return ['', '-webkit-', '-moz-', '-o-'].some(
5+
prefix => node.name === `${prefix}keyframes`
6+
);
7+
}

test/css/samples/keyframes-autoprefixed/expected.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<div class='animated'>animated</div>
2+
<div class='also-animated'>also animated</div>
3+
4+
<style>
5+
@keyframes why {
6+
0% { color: red; }
7+
100% { color: blue; }
8+
}
9+
10+
@-webkit-keyframes why {
11+
0% { color: red; }
12+
100% { color: blue; }
13+
}
14+
15+
@-moz-keyframes why {
16+
0% { color: red; }
17+
100% { color: blue; }
18+
}
19+
20+
@-o-keyframes why {
21+
0% { color: red; }
22+
100% { color: blue; }
23+
}
24+
25+
.animated {
26+
animation: why 2s;
27+
}
28+
29+
.also-animated {
30+
animation: not-defined-here 2s;
31+
}
32+
</style>

0 commit comments

Comments
 (0)