Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Sass & Sourcemaps

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Our layout.js file requires main.css:

... lines 1 - 6
... lines 8 - 12

That's cool... I guess... if you like using boring old CSS. But I want to be more Hipster, so let's use Sass instead. Well, I could use Stylus to be super hipster... and Encore does support that, but let's use something a bit more familiar.

To start, rename the file to main.scss. Now, we can use a fancier syntax for these pseudo-selectors:

... lines 1 - 16
.btn-login {
... lines 18 - 26
&:hover,&:focus {
color: #fff;
background-color: #53A3CD;
border-color: #53A3CD;
... lines 33 - 77


Obviously, the build is failing because, in layout.js, that file path is wrong! Change it to main.scss:

... lines 1 - 6
... lines 8 - 12

So... does it already work?

Activating Optional Features

No! On the watch tab of our terminal, it failed when loading main.scss. Out-of-the-box, Encore cannot process Sass files. But, it tells you how to fix this! We just need to enable it inside our config & install some extra packages. Remember: Encore is full of features. But to stay light, it doesn't enable everything automatically. Instead, you are in control: enable what you need, and Encore will tell you what to do. It's kinda cool!

Go back to webpack.config.js and add .enableSassLoader():

... lines 1 - 3
... lines 5 - 23
... lines 26 - 29

Then, back on your terminal, copy the yarn add line, stop Encore with Ctrl+C, and paste!

yarn add sass-loader node-sass --dev

Let it do its thing, then... restart Encore!

yarn run encore dev --watch

No errors! To prove it works, move over to your browser and... refresh! It still looks great! Well, most importantly, on the login page, when we hover of the button, it does have that styling.

Encore Versus Webpack Concepts

There's one thing I want you to notice: the name of this method: enableSassLoader():

... lines 1 - 3
... lines 5 - 23
... lines 26 - 29

"Loader" is a Webpack concept. Encore tries to make Webpack as easy as possible, but it reuses Webpack's language and terms whenever possible. And that's important! If you ever need to do something custom with Webpack, it's usually pretty easy to figure out how that fits into Encore.

Also, we're requiring bootstrap.css right now:

... lines 1 - 4
... lines 6 - 12

But, with Sass support, you could instead import Bootstrap's Sass files directly. The advantage is that you can override Bootstrap's Sass variables and take control of colors, sizes and other stuff. To do that with Bootstrap 3, you'll need the bootstrap-sass package. For Bootstrap 4, the Sass files are included in the main package.


Let's fix one more problem quickly: sourcemaps! If you click on a row, we have some console.log() debugging code. But, where does that code come from? Well, if you click on the rep_log.js link, apparently it's coming from line 197. But, that's a lie! Well, sort of. This is the built rep_log.js file, not the source file.

And this highlights a classic problem: when you build many files into one file, debugging gets harder because error messages and other info don't point to the real line number or the original filename.

Let's fix that! Back in webpack.config.js, add .enableSourceMaps() with an argument: !Encore.isProduction():

... lines 1 - 3
... lines 5 - 24
... lines 27 - 30

This enables extra debugging info - called sourcemaps - whenever we are creating a development build.

Because we just updated the Webpack config, restart Encore:

yarn run encore dev --watch

Thanks to this, all of our JavaScript and CSS files now have some extra content at the bottom that hints to our browser where the source content came from. This time, when I click a row, in the console, awesome! It's coming from RepLogApp.js line 104. That is the real spot.

Oh, by the way: if you don't enable sourcemaps, you may still see some sourcemap info at the bottom of your CSS files during development. That's just an internal quirk - it won't be there on production.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": "^7.2.0",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^1.6", // 1.8.1
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
        "doctrine/doctrine-fixtures-bundle": "~3.0", // 3.0.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.3.1
        "doctrine/orm": "^2.5", // v2.7.2
        "friendsofsymfony/jsrouting-bundle": "^2.2", // 2.2.0
        "friendsofsymfony/user-bundle": "dev-master", // dev-master
        "sensio/framework-extra-bundle": "^5.1", // v5.1.5
        "symfony/asset": "^4.0", // v4.0.4
        "symfony/console": "^4.0", // v4.0.4
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "^4.0", // v4.0.4
        "symfony/framework-bundle": "^4.0", // v4.0.4
        "symfony/lts": "^4@dev", // dev-master
        "symfony/monolog-bundle": "^3.1", // v3.1.2
        "symfony/polyfill-apcu": "^1.0", // v1.7.0
        "symfony/serializer-pack": "^1.0", // v1.0.1
        "symfony/swiftmailer-bundle": "^3.1", // v3.1.6
        "symfony/twig-bundle": "^4.0", // v4.0.4
        "symfony/validator": "^4.0", // v4.0.4
        "symfony/yaml": "^4.0", // v4.0.4
        "twig/twig": "2.10.*" // v2.10.0
    "require-dev": {
        "symfony/debug-pack": "^1.0", // v1.0.4
        "symfony/dotenv": "^4.0", // v4.0.4
        "symfony/phpunit-bridge": "^4.0", // v4.0.4
        "symfony/web-server-bundle": "^4.0" // v4.0.4

What JavaScript libraries does this tutorial use?

// package.json
    "devDependencies": {
        "@symfony/webpack-encore": "^0.19.0", // 0.19.0
        "bootstrap": "3", // 3.3.7
        "copy-webpack-plugin": "^4.4.1", // 4.4.1
        "font-awesome": "4", // 4.7.0
        "jquery": "^3.3.1", // 3.3.1
        "node-sass": "^4.7.2", // 4.7.2
        "sass-loader": "^6.0.6", // 6.0.6
        "sweetalert2": "^7.11.0", // 7.11.0
        "webpack-notifier": "^1.5.1" // 1.5.1