{"id":4017,"date":"2019-03-18T14:48:19","date_gmt":"2019-03-18T14:48:19","guid":{"rendered":"https:\/\/leap.staging.ribbitt.com\/insights\/migrating-from-sass-to-postcss\/"},"modified":"2019-03-18T14:48:19","modified_gmt":"2019-03-18T14:48:19","slug":"migrating-from-sass-to-postcss","status":"publish","type":"insights","link":"https:\/\/leap.staging.ribbitt.com\/insights\/migrating-from-sass-to-postcss\/","title":{"rendered":"Migrating From SASS to PostCSS"},"content":{"rendered":"
PostCSS<\/a> is a tool that developers can use to write CSS in a familiar way. Additionally it gives them the flexibility of a pre-processor. <\/p>\n But adopting PostCSS into your workflow is a process. I’ll show you how to convert common SASS features into the PostCSS way and leave you with a challenge. To follow along, you will need an understanding of CSS\/SASS, npm, and JavaScript, but are not required.<\/p>\n By the end of this article, you’ll know how to:<\/p>\n 1. Install or run PostCSS PostCSS is best used in conjunction with a build tool such as Parcel or Webpack but can also used via their command line tool (CLI). <\/p>\n Instead of installing the command line tool, we can use npx to run any commands needed. The npx command pulls a package, runs the command with the arguments given, and then deletes itself. This is great for one time use tools or if you want to keep your filesystem clean.<\/p>\n \nThis command executes the following steps…<\/p>\n 1. Tells PostCSS to look for a file named css\/app.css In SASS\/SCSS, you declare variables the $ symbol. With PostCSS, you have several options.<\/p>\n 1. Install the postcss-nested-vars<\/strong><\/a><\/strong> plugin, which enables SASS style variables I recommend using option 2. Using native CSS variables removes some overhead, as you don’t need a plugin. Not to mention, they’re fun to use! They also offer you flexibility since CSS variables can be changed in real time. Think of the possibilities!<\/p>\n Writing CSS variables is easy. To declare global variables, you can add them to a :root element. This will wrap them into a global context of sorts. You can also declare variables inside of another selector if you wanted.<\/p>\n <\/p>\n PostCSS, by default, does not include support for nested selectors. Yet, this functionality can be enabled through the use of the postcss-nested<\/a> plugin. But before we do that, we need to create a configuration file.<\/p>\n This configuration file tells PostCSS what plugins to use and where they’re located. You can name the file anything, but for best practices, it’s best to name it postcss.config.js<\/strong>. Place this file in the root of your project.<\/p>\n Next, initialize an npm project.<\/p>\n Now install the plugin.<\/p>\n Now when you run your PostCSS command you can specify that you have a settings file using the -c flag.<\/p>\n \nWith that out of the way, you can now write nested selectors that will expand.<\/p>\n <\/p>\n
\n2. Build your PostCSS into an output file
\n2. Replace SASS variables
\n3. Replace SASS nested rules
\n4. Use named media queries<\/p>\nInstalling & Running<\/h2>\n
\n2. Compiles the css
\n3. Outputs the new css into a file named main.css<\/p>\nVariables<\/h2>\n
\n2. Refactor your variables into native CSS variables<\/p>\n\n:root {\n --primary-color: #393a4a;\n}\n\n.bg-primary {\n --secondary-color: #eee;\n Background-color: var(--primary-color);\n Color: var(--secondary-color);\n}\n<\/code><\/pre>\n<\/div>\n
Nested Selectors<\/h2>\n
module.exports = {\n plugins: [\n require(‘postcss-nested’), \n ]\n}\n<\/code><\/pre>\n<\/div>\n
npm init<\/code><\/pre>\n<\/div>\n
npm install postcss-nested –save<\/code><\/pre>\n<\/div>\n
\nnpx postcss -o main.css -c postcss.config.js css\/app.css<\/code><\/pre>\n<\/div>\n
\n.selector-1 {\n\t.selector-2 {\n\t\tColor: black;\n\t}\n}\n\n\/* output *\/\n.selector-1 .selector-2 {\n\tColor: black;\n}<\/code><\/pre>\n<\/div>\n
Named Media Queries<\/h2>\n