Merge pull request #1505 from kaisermann/transform-prefixed-keyframes

Make the compiler also transform prefixed @keyframes
pull/1516/head
Rich Harris 7 years ago committed by GitHub
commit 024530ed53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ import { getLocator } from 'locate-character';
import Selector from './Selector'; import Selector from './Selector';
import getCodeFrame from '../utils/getCodeFrame'; import getCodeFrame from '../utils/getCodeFrame';
import hash from '../utils/hash'; import hash from '../utils/hash';
import isKeyframesNode from '../utils/isKeyframesNode';
import Element from '../compile/nodes/Element'; import Element from '../compile/nodes/Element';
import { Validator } from '../validate/index'; import { Validator } from '../validate/index';
import { Node, Ast, Warning } from '../interfaces'; import { Node, Ast, Warning } from '../interfaces';
@ -26,7 +27,7 @@ class Rule {
} }
isUsed(dev: boolean) { isUsed(dev: boolean) {
if (this.parent && this.parent.node.type === 'Atrule' && this.parent.node.name === 'keyframes') return true; if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
if (this.declarations.length === 0) return dev; if (this.declarations.length === 0) return dev;
return this.selectors.some(s => s.used); return this.selectors.some(s => s.used);
} }
@ -67,7 +68,7 @@ class Rule {
} }
transform(code: MagicString, id: string, keyframes: Map<string, string>) { transform(code: MagicString, id: string, keyframes: Map<string, string>) {
if (this.parent && this.parent.node.type === 'Atrule' && this.parent.node.name === 'keyframes') return true; if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
const attr = `.${id}`; const attr = `.${id}`;
@ -142,7 +143,7 @@ class Atrule {
}); });
} }
else if (this.node.name === 'keyframes') { else if (isKeyframesNode(this.node)) {
this.children.forEach((rule: Rule) => { this.children.forEach((rule: Rule) => {
rule.selectors.forEach(selector => { rule.selectors.forEach(selector => {
selector.used = true; selector.used = true;
@ -167,8 +168,8 @@ class Atrule {
}); });
code.remove(c, this.node.block.start); code.remove(c, this.node.block.start);
} else if (this.node.name === 'keyframes') { } else if (isKeyframesNode(this.node)) {
let c = this.node.start + 10; let c = this.node.start + this.node.name.length + 1;
if (this.node.expression.start - c > 1) code.overwrite(c, this.node.expression.start, ' '); if (this.node.expression.start - c > 1) code.overwrite(c, this.node.expression.start, ' ');
c = this.node.expression.end; c = this.node.expression.end;
if (this.node.block.start - c > 0) code.remove(c, this.node.block.start); if (this.node.block.start - c > 0) code.remove(c, this.node.block.start);
@ -200,7 +201,7 @@ class Atrule {
} }
transform(code: MagicString, id: string, keyframes: Map<string, string>) { transform(code: MagicString, id: string, keyframes: Map<string, string>) {
if (this.node.name === 'keyframes') { if (isKeyframesNode(this.node)) {
this.node.expression.children.forEach(({ type, name, start, end }: Node) => { this.node.expression.children.forEach(({ type, name, start, end }: Node) => {
if (type === 'Identifier') { if (type === 'Identifier') {
if (name.startsWith('-global-')) { if (name.startsWith('-global-')) {
@ -287,7 +288,7 @@ export default class Stylesheet {
this.children.push(atrule); this.children.push(atrule);
} }
if (node.name === 'keyframes') { if (isKeyframesNode(node)) {
node.expression.children.forEach((expression: Node) => { node.expression.children.forEach((expression: Node) => {
if (expression.type === 'Identifier' && !expression.name.startsWith('-global-')) { if (expression.type === 'Identifier' && !expression.name.startsWith('-global-')) {
this.keyframes.set(expression.name, `${this.id}-${expression.name}`); this.keyframes.set(expression.name, `${this.id}-${expression.name}`);

@ -0,0 +1,7 @@
import { Node } from '../interfaces';
export default function isKeyframesNode(node: Node): boolean {
return ['', '-webkit-', '-moz-', '-o-'].some(
prefix => node.name === `${prefix}keyframes`
);
}

@ -0,0 +1 @@
@keyframes svelte-xyz-why{0%{color:red}100%{color:blue}}@-webkit-keyframes svelte-xyz-why{0%{color:red}100%{color:blue}}@-moz-keyframes svelte-xyz-why{0%{color:red}100%{color:blue}}@-o-keyframes svelte-xyz-why{0%{color:red}100%{color:blue}}.animated.svelte-xyz{animation:svelte-xyz-why 2s}.also-animated.svelte-xyz{animation:not-defined-here 2s}

@ -0,0 +1,32 @@
<div class='animated'>animated</div>
<div class='also-animated'>also animated</div>
<style>
@keyframes why {
0% { color: red; }
100% { color: blue; }
}
@-webkit-keyframes why {
0% { color: red; }
100% { color: blue; }
}
@-moz-keyframes why {
0% { color: red; }
100% { color: blue; }
}
@-o-keyframes why {
0% { color: red; }
100% { color: blue; }
}
.animated {
animation: why 2s;
}
.also-animated {
animation: not-defined-here 2s;
}
</style>
Loading…
Cancel
Save