Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Transitioning to Sass at Scale (CSSconf 2016 - ...

Dan Na
June 01, 2016

Transitioning to Sass at Scale (CSSconf 2016 - Oslo, Norway)

June 1, 2016. Oslo, Norway. 30min.

CSS preprocessors like Sass add a vaiety of functions that streamline CSS development: variables, nesting, functions, mixins, etc. The documentation is great, the tools are mature, and starting a new project using Sass has a clear and straight-forward workflow. But transitioning a large legacy codebase from CSS to Sass is a different story. CSS syntax errors that may be harmless in production can completely prevent Sass from compiling. But fixing those errors creates a far juicier problem: will we introduce visual bugs by fixing syntax bugs?

At Etsy we faced this exact question multiplied across over 400,000 lines of CSS and 2100+ CSS files. During this talk I'll discuss the considerations we had when approaching this project, cover the process of transforming CSS files using Abstract Syntax Trees (ASTs), some of the methodologies and tools we use to mitigate some of the biggest potential pitfalls of Sass (Sass live lint), how we ramped up our production services to gain confidence in our process and how this entire effort led to a single 1.2M line push that didn't break production and had minimal impact to developer and designer workflows.

Dan Na

June 01, 2016
Tweet

More Decks by Dan Na

Other Decks in Technology

Transcript

  1. 3

  2. Dan Na | @dxna An incredibly brief intro to Sass

    Why this was challenging The CSS->Sass conversion Long-term maintainability 10 Agenda
  3. Dan Na | @dxna Sass is an extension of CSS

    that adds power and elegance to the basic language. It allows you to use variables, nested rules, mixins, inline imports, and more, all with a fully CSS- compatible syntax. http://sass-lang.org 12
  4. Dan Na | @dxna #main { color: #222222; } a[href]

    { color: orange; } a[href]:hover { color: green; } 13
  5. Dan Na | @dxna #main { color: $main-text; } a[href]

    { color: $link; } a[href]:hover { color: $link-hover; } 14 $main-text: #222222; $link: orange; $link-hover: green; _variables.scss
  6. Dan Na | @dxna #main { color: $main-text; } a[href]

    { color: $link; &:hover { color: $link-hover; } } 15 $main-text: #222222; $link: orange; $link-hover: green; _variables.scss
  7. Dan Na | @dxna @mixin large-text { font-size: 40px; font-weight:

    bold; } .important { @include large-text; color: red; } 16 . .important { font-size: 40px; font-weight: bold; color: red; } CSS Output
  8. Dan Na | @dxna Other Sass Functionality • Function declarations

    • Data structures: lists, maps • Control flow (if, else) and iteration (for, each, while) • http://www.sass-lang.com 17
  9. Dan Na | @dxna 20 Steps (in theory) 1. Rename

    homepage.css -> homepage.scss
  10. Dan Na | @dxna 20 Steps (in theory) 1. Rename

    homepage.css -> homepage.scss 2. Render homepage.scss -> homepage.css for production
  11. Dan Na | @dxna 20 Steps (in theory) 1. Rename

    homepage.css -> homepage.scss 2. Render homepage.scss -> homepage.css for production 3. Sign autographs, go home early
  12. Dan Na | @dxna body { background-color: rgb(-10, 25555, -10);

    background-color: rgb(0, 255, 0); } 27
  13. 30

  14. 31

  15. Dan Na | @dxna Etsy CSS, October 2014 • 2,000+

    individual CSS files • 400,000+ lines of CSS 32
  16. Dan Na | @dxna 34 span {color: black; font-size: 12px;}

    .italic {font-style: italic;} .loud {font-size: 100.00px; font-weight: bold; }
  17. Dan Na | @dxna 36 Pre SCSS-lint span {color: black;

    font-weight: bold;} .italic {font-style: italic;} span { color: black; font-weight: bold; } .italic { font-style: italic; } Post SCSS-lint
  18. Dan Na | @dxna Core challenges • We have a

    ton of CSS, of varying age, with syntax errors that must be fixed. • How do we confirm that our production site won’t break in small, difficult to detect ways? 37
  19. Dan Na | @dxna Secondary challenges • An opportunity to

    enforce consistent CSS formatting: syntax, colors, spacing, etc. • How do we help coworkers learn/use Sass productively? • How do we help them do “the right thing?” 38
  20. Dan Na | @dxna State of the world • 2000+

    CSS files, 400,000+ lines of CSS • We want to fix formatting inconsistencies • We want to fix broken CSS 40
  21. Dan Na | @dxna Goal: Find any background color in

    CSS that’s set to green using regular expressions. 42
  22. Dan Na | @dxna body { background-color: green; } 44

    /\s(green);/g section { color: green; }
  23. Dan Na | @dxna body { background-color: green; } 45

    /background-color:\s(green);/g section { color: green; }
  24. Dan Na | @dxna body { background-color: green; } 45

    /background-color:\s(green);/g section { color: green; } .green-box { background-color:green; }
  25. Dan Na | @dxna body { background-color: green; } section

    { color: green; } .green-box { background-color:green; } 46 /background-color:\s?(green);/g
  26. Dan Na | @dxna body { background-color: green; } section

    { color: green; } .green-box { background-color:green; } 46 /background-color:\s?(green);/g nav { background: url("./foo.jpg") top center green no-repeat; }
  27. Dan Na | @dxna body { background-color: green; } section

    { color: green; } .green-box { background-color:green; } nav { background: url("./foo.jpg") top center green no-repeat; } 47 /background(-color){0,1}:.*(green);{0,1}/g
  28. 48

  29. 48

  30. Dan Na | @dxna 50 body { background-color: green; color:

    red; font-size: 12px; } .main { color: blue; }
  31. Dan Na | @dxna 51 body { background-color: green; color:

    red; font-size: 12px; } .main { color: blue; } Rules
  32. Dan Na | @dxna 52 body { background-color: green; color:

    red; font-size: 12px; } .main { color: blue; } Selectors
  33. Dan Na | @dxna 53 body { background-color: green; color:

    red; font-size: 12px; } .main { color: blue; } Declarations
  34. Dan Na | @dxna 54 body { background-color: green; color:

    red; font-size: 12px; } .main { color: blue; } Properties and Values
  35. Dan Na | @dxna 55 { "stylesheet": { "rules": [

    { "type": "rule", "selectors": [ "body" ], "declarations": [ … ], ] } } body { background-color: green; color: red; font-size: 12px; }
  36. Dan Na | @dxna 56 body { background-color: green; color:

    red; font-size: 12px; } "declarations": [ { "type": "declaration", "property": “background-color", "value": "green", ... }, { "type": "declaration", "property": "color", "value": "red", ... } ]
  37. Dan Na | @dxna Power of ASTs • Granularly target

    specific properties/selectors • Bound regex usage • Meaningfully compare CSS output 58
  38. Dan Na | @dxna Equivalent ASTs meant that the CSS

    in production after our conversion was the same as the CSS before our conversion. 59
  39. Dan Na | @dxna Conversion at a Glance • Hundreds

    of invalid CSS rules and selectors • Formatting @import paths • IE-specific selectors (filter:progid:….) and hacks 61
  40. Dan Na | @dxna Original AST 62 Final conversion process

    base.css base.css base.scss Fixed AST
  41. Dan Na | @dxna Sass Education Efforts • Brown-bag lunch

    sessions • Documentation on internal wiki • Available on internal chat 70
  42. 71

  43. 71

  44. 71