Adding sass/scss to a Phoenix 1.4 project involves the following:
- Adding
node-sass
andsass-loader
toassets/package.json
- Renaming
assets/css/app.css
toassets/css/app.scss
- Adding the
sass-loader
to thecss
file section inwebpack.config.js
and changing the extension to.scss
- changing
import css from "../css/app.css"
toimport css from "../css/app.scss"
inassets/js/app.js
Here’s a diff of the altered files:
package.json:
"copy-webpack-plugin": "^4.5.0",
"css-loader": "^0.28.10",
"mini-css-extract-plugin": "^0.4.0",
+ "node-sass": "^4.9.0",
"optimize-css-assets-webpack-plugin": "^4.0.0",
+ "sass-loader": "^7.0.1",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "4.4.0",
"webpack-cli": "^2.0.10"
webpack.config.js:
{
- test: /\.css$/,
- use: [MiniCssExtractPlugin.loader, 'css-loader']
+ test: /\.scss$/,
+ use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
assets/js/app.js
// We need to import the CSS so that webpack will load it.
// The MiniCssExtractPlugin is used to separate it out into
// its own CSS file.
-import css from "../css/app.css"
+import css from "../css/app.scss"
Live reloading works for this setup.