scss-stringifier.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. let Stringifier = require('postcss/lib/stringifier')
  2. class ScssStringifier extends Stringifier {
  3. comment(node) {
  4. let left = this.raw(node, 'left', 'commentLeft')
  5. let right = this.raw(node, 'right', 'commentRight')
  6. if (node.raws.inline) {
  7. let text = node.raws.text || node.text
  8. this.builder('//' + left + text + right, node)
  9. } else {
  10. this.builder('/*' + left + node.text + right + '*/', node)
  11. }
  12. }
  13. decl(node, semicolon) {
  14. if (!node.isNested) {
  15. super.decl(node, semicolon)
  16. } else {
  17. let between = this.raw(node, 'between', 'colon')
  18. let string = node.prop + between + this.rawValue(node, 'value')
  19. if (node.important) {
  20. string += node.raws.important || ' !important'
  21. }
  22. this.builder(string + '{', node, 'start')
  23. let after
  24. if (node.nodes && node.nodes.length) {
  25. this.body(node)
  26. after = this.raw(node, 'after')
  27. } else {
  28. after = this.raw(node, 'after', 'emptyBody')
  29. }
  30. if (after) this.builder(after)
  31. this.builder('}', node, 'end')
  32. }
  33. }
  34. rawValue(node, prop) {
  35. let value = node[prop]
  36. let raw = node.raws[prop]
  37. if (raw && raw.value === value) {
  38. return raw.scss ? raw.scss : raw.raw
  39. } else {
  40. return value
  41. }
  42. }
  43. }
  44. module.exports = ScssStringifier