|
| 1 | +import postcss, { |
| 2 | + type ChildNode as PostCssChildNode, |
| 3 | + type Container as PostCssContainerNode, |
| 4 | + type Root as PostCssRoot, |
| 5 | + type Source as PostcssSource, |
| 6 | +} from 'postcss' |
| 7 | +import { atRule, comment, decl, rule, type AstNode } from '../../tailwindcss/src/ast' |
| 8 | + |
| 9 | +const EXCLAMATION_MARK = 0x21 |
| 10 | + |
| 11 | +export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undefined): PostCssRoot { |
| 12 | + let root = postcss.root() |
| 13 | + root.source = source |
| 14 | + |
| 15 | + function transform(node: AstNode, parent: PostCssContainerNode) { |
| 16 | + // Declaration |
| 17 | + if (node.kind === 'declaration') { |
| 18 | + let astNode = postcss.decl({ |
| 19 | + prop: node.property, |
| 20 | + value: node.value ?? '', |
| 21 | + important: node.important, |
| 22 | + }) |
| 23 | + astNode.source = source |
| 24 | + parent.append(astNode) |
| 25 | + } |
| 26 | + |
| 27 | + // Rule |
| 28 | + else if (node.kind === 'rule') { |
| 29 | + let astNode = postcss.rule({ selector: node.selector }) |
| 30 | + astNode.source = source |
| 31 | + astNode.raws.semicolon = true |
| 32 | + parent.append(astNode) |
| 33 | + for (let child of node.nodes) { |
| 34 | + transform(child, astNode) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // AtRule |
| 39 | + else if (node.kind === 'at-rule') { |
| 40 | + let astNode = postcss.atRule({ name: node.name.slice(1), params: node.params }) |
| 41 | + astNode.source = source |
| 42 | + astNode.raws.semicolon = true |
| 43 | + parent.append(astNode) |
| 44 | + for (let child of node.nodes) { |
| 45 | + transform(child, astNode) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Comment |
| 50 | + else if (node.kind === 'comment') { |
| 51 | + let astNode = postcss.comment({ text: node.value }) |
| 52 | + // Spaces are encoded in our node.value already, no need to add additional |
| 53 | + // spaces. |
| 54 | + astNode.raws.left = '' |
| 55 | + astNode.raws.right = '' |
| 56 | + astNode.source = source |
| 57 | + parent.append(astNode) |
| 58 | + } |
| 59 | + |
| 60 | + // AtRoot & Context should not happen |
| 61 | + else if (node.kind === 'at-root' || node.kind === 'context') { |
| 62 | + } |
| 63 | + |
| 64 | + // Unknown |
| 65 | + else { |
| 66 | + node satisfies never |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (let node of ast) { |
| 71 | + transform(node, root) |
| 72 | + } |
| 73 | + |
| 74 | + return root |
| 75 | +} |
| 76 | + |
| 77 | +export function postCssAstToCssAst(root: PostCssRoot): AstNode[] { |
| 78 | + function transform( |
| 79 | + node: PostCssChildNode, |
| 80 | + parent: Extract<AstNode, { nodes: AstNode[] }>['nodes'], |
| 81 | + ) { |
| 82 | + // Declaration |
| 83 | + if (node.type === 'decl') { |
| 84 | + parent.push(decl(node.prop, node.value, node.important)) |
| 85 | + } |
| 86 | + |
| 87 | + // Rule |
| 88 | + else if (node.type === 'rule') { |
| 89 | + let astNode = rule(node.selector) |
| 90 | + node.each((child) => transform(child, astNode.nodes)) |
| 91 | + parent.push(astNode) |
| 92 | + } |
| 93 | + |
| 94 | + // AtRule |
| 95 | + else if (node.type === 'atrule') { |
| 96 | + let astNode = atRule(`@${node.name}`, node.params) |
| 97 | + node.each((child) => transform(child, astNode.nodes)) |
| 98 | + parent.push(astNode) |
| 99 | + } |
| 100 | + |
| 101 | + // Comment |
| 102 | + else if (node.type === 'comment') { |
| 103 | + if (node.text.charCodeAt(0) !== EXCLAMATION_MARK) return |
| 104 | + parent.push(comment(node.text)) |
| 105 | + } |
| 106 | + |
| 107 | + // Unknown |
| 108 | + else { |
| 109 | + node satisfies never |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + let ast: AstNode[] = [] |
| 114 | + root.each((node) => transform(node, ast)) |
| 115 | + |
| 116 | + return ast |
| 117 | +} |
0 commit comments