From 75fa924e8975149fd247c026ea10bd296ad3201d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 7 Jul 2017 18:24:39 -0400 Subject: [PATCH] remove processCss --- src/generators/Generator.ts | 2 - src/generators/Selector.ts | 2 +- src/generators/shared/processCss.ts | 104 ---------------------------- 3 files changed, 1 insertion(+), 107 deletions(-) delete mode 100644 src/generators/shared/processCss.ts diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index a2624bc64c..6c1c365f2e 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -10,12 +10,10 @@ import namespaces from '../utils/namespaces'; import { removeNode, removeObjectKey } from '../utils/removeNode'; import getIntro from './shared/utils/getIntro'; import getOutro from './shared/utils/getOutro'; -import processCss from './shared/processCss'; import annotateWithScopes from '../utils/annotateWithScopes'; import clone from '../utils/clone'; import DomBlock from './dom/Block'; import SsrBlock from './server-side-rendering/Block'; -import { walkRules } from '../utils/css'; import Selector from './Selector'; import Stylesheet from './Stylesheet'; import { Node, Parsed, CompileOptions } from '../interfaces'; diff --git a/src/generators/Selector.ts b/src/generators/Selector.ts index 674cd37702..b5e2941d1c 100644 --- a/src/generators/Selector.ts +++ b/src/generators/Selector.ts @@ -1,5 +1,5 @@ import MagicString from 'magic-string'; -import { groupSelectors, isGlobalSelector, walkRules } from '../utils/css'; +import { groupSelectors, isGlobalSelector } from '../utils/css'; import { Node } from '../interfaces'; export default class Selector { diff --git a/src/generators/shared/processCss.ts b/src/generators/shared/processCss.ts deleted file mode 100644 index 5caec55239..0000000000 --- a/src/generators/shared/processCss.ts +++ /dev/null @@ -1,104 +0,0 @@ -import MagicString from 'magic-string'; -import { groupSelectors, isGlobalSelector, walkRules } from '../../utils/css'; -import Generator from '../Generator'; -import { Node } from '../../interfaces'; - -const commentsPattern = /\/\*[\s\S]*?\*\//g; - -export default function processCss( - generator: Generator, - code: MagicString, - cascade: boolean -) { - const css = generator.parsed.css.content.styles; - const offset = generator.parsed.css.content.start; - - const attr = `[svelte-${generator.parsed.hash}]`; - - const keyframes = new Map(); - - function walkKeyframes(node: Node) { - if (node.type === 'Atrule' && node.name.toLowerCase() === 'keyframes') { - node.expression.children.forEach((expression: Node) => { - if (expression.type === 'Identifier') { - if (expression.name.startsWith('-global-')) { - code.remove(expression.start, expression.start + 8); - } else { - const newName = `svelte-${generator.parsed.hash}-${expression.name}`; - code.overwrite(expression.start, expression.end, newName); - keyframes.set(expression.name, newName); - } - } - }); - } else if (node.children) { - node.children.forEach(walkKeyframes); - } else if (node.block) { - walkKeyframes(node.block); - } - } - - generator.parsed.css.children.forEach(walkKeyframes); - - function transform(rule: Node) { - rule.selector.children.forEach((selector: Node) => { - if (cascade) { - // TODO disable cascading (without :global(...)) in v2 - const start = selector.start - offset; - const end = selector.end - offset; - - const selectorString = css.slice(start, end); - - const firstToken = selector.children[0]; - - let transformed; - - if (firstToken.type === 'TypeSelector') { - const insert = firstToken.end - offset; - const head = firstToken.name === '*' ? css.slice(firstToken.end - offset, insert) : css.slice(start, insert); - const tail = css.slice(insert, end); - - transformed = `${head}${attr}${tail}, ${attr} ${selectorString}`; - } else { - transformed = `${attr}${selectorString}, ${attr} ${selectorString}`; - } - - code.overwrite(selector.start, selector.end, transformed); - } - }); - - rule.block.children.forEach((block: Node) => { - if (block.type === 'Declaration') { - const property = block.property.toLowerCase(); - if (property === 'animation' || property === 'animation-name') { - block.value.children.forEach((block: Node) => { - if (block.type === 'Identifier') { - const name = block.name; - if (keyframes.has(name)) { - code.overwrite(block.start, block.end, keyframes.get(name)); - } - } - }); - } - } - }); - } - - walkRules(generator.parsed.css.children, transform); - - if (!cascade) { - generator.selectors.forEach(selector => { - selector.transform(code, attr); - }); - } - - // remove comments. TODO would be nice if this was exposed in css-tree - let match; - while ((match = commentsPattern.exec(css))) { - const start = match.index + offset; - const end = start + match[0].length; - - code.remove(start, end); - } - - return code.slice(generator.parsed.css.content.start, generator.parsed.css.content.end); -}