Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SASS/SCSS with Phoenix framework?

Tags:

Is there a way to use sass/scss for stylesheets when using Phoenix Framework? And if there is, then how ?

like image 599
NoDisplayName Avatar asked Aug 13 '15 02:08

NoDisplayName


People also ask

Does SCSS use Sass?

SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.

Is Sass deprecated?

As part of our ongoing initiatives, we've decided to deprecate Sass, with the aim of improving the user experience of storefronts, and paving the way for future advancements. In the short term, Sass will continue to work on Shopify themes, but we are actively migrating our themes to use only CSS stylesheets.

Does SCSS run in browser?

Unfortunately, the features of SCSS have yet to be introduced in the CSS specs and, therefore, are not supported by browsers. To run SCSS code in a web browser, you must first convert it to CSS.


2 Answers

Phoenix framework uses brunch for the asset pipeline.

From the docs:

Instead of implementing its own asset pipeline, Phoenix uses Brunch, a fast and developer-friendly asset build tool. Phoenix comes with a default configuration for Brunch and it will work out of the box, but it is very easy to bend it to our needs, add support for various script and style languages, like CoffeeScript, TypeScript, or LESS.

To add support for SASS, add 'sass-brunch' in your package.json in the project root as:

{   "repository": {   },   "dependencies": {     "brunch": "^1.8.1",     "babel-brunch": "^5.1.1",     "clean-css-brunch": ">= 1.0 < 1.8",     "css-brunch": ">= 1.0 < 1.8",     "javascript-brunch": ">= 1.0 < 1.8",     "sass-brunch": "^1.8.10",     "uglify-js-brunch": ">= 1.0 < 1.8"   } } 

Then run npm install.

Phoenix framework also supports asset management without brunch as the default.

While creating a new project:

mix phoenix.new --no-brunch my_project

will create a project without brunch configuration. You need to setup a system that can copy the built assets into priv/static/ and also watch your source files to make automatic compilation on every change. Read the docs for more info.

like image 108
Lenin Raj Rajasekaran Avatar answered Oct 12 '22 00:10

Lenin Raj Rajasekaran


Here's a working demo repo with the steps I took as commits:

https://github.com/sergiotapia/phoenix-sass-example


To use SASS/SCSS, you need to install the sass-brunch node package.

npm install --save sass-brunch 

Then edit brunch-config.js so your plugins section looks like this:

// Configure your plugins plugins: {   babel: {     // Do not use ES6 compiler in vendor code     ignore: [/web\/static\/vendor/]   },   sass: {     mode: "native" // This is the important part!   } }, 

Once you do that, any .sass or .scss files will work seamlessly.

like image 23
Sergio Tapia Avatar answered Oct 12 '22 00:10

Sergio Tapia