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

Combining (concat) Files

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $6.00

We only have one CSS file, and gosh, that's just not very realistic. So, let's create a second one. Call it, layout.scss. To keep the dinosaurs happy, let's give the body a background - a nice dark green background:

body {
background-color: #006400;
}

Ya know, because they're, sorta, dark green.

Go back and refresh! Ok, it doesn't work quite yet. But hey! We do have the watch task running in the background, and it's looking for any .scss file in that directory. So we should at least have a new layout.css file. Ah, there it is.

If we want to keep things simple, we can just add another link tag in the base template to use this:

... lines 1 - 2
<head>
... lines 4 - 9
<link rel="stylesheet" href="{{ asset('vendor/bootstrap/dist/css/bootstrap.css') }}"/>
<link rel="stylesheet" href="{{ asset('css/styles.css') }}"/>
<link rel="stylesheet" href="{{ asset('css/layout.css') }}"/>
... lines 15 - 49

Perfect!

Using gulp-concat

But I don't want to make my users download a ton of CSS files. I want to combine them all into a single file. There's a plugin for that. This time, it's called gulp-concat.

Let's do our thing. Step 1: install this via npm. Hit ctrl+c to stop watch, then:

npm install gulp-concat --save-dev

While the npm robots work on that for us, let's go back and copy the require statement:

25 lines gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
... lines 5 - 25

The gulp.src() function loads a stream of many files. The new concat() function combines those streams into just 1 file. He's such minimalist.

Let's do this right after sass() - pipe(), then concat(). And pass it the filename it should create - main.css:

25 lines gulpfile.js
... lines 1 - 10
gulp.task('sass', function() {
gulp.src(config.assetsDir+'/'+config.sassPattern)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(concat('main.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('web/css'));
});
... lines 19 - 25

Make sure you keep this between the sourcemaps lines because we're smashing multiple files into one, and that'll change the line numbers and source files for everything. But sourcemaps will keep track of all of that for us. Good job sourcemaps!

Empty out the web/css directory before testing things: those shouldn't be generated anymore. Now, try gulp:

gulp

And hey, now we've got just one beautiful main.css file and its map. It's got the CSS from both our source files. Don't forget to celebrate by going back to your base template and turning those 2 link tags into 1.

Refresh to try it. Of course it works, but now with just 1 CSS file... other than my Bootstrap and font stuff. But the real test is whether the sourcemap still works. Inspect on our awesome cursive. Yep, it knows things are coming from style.scss on line 4. Right on!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.6.*", // v2.6.4
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.6
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.2.0
        "symfony/assetic-bundle": "~2.3", // v2.5.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.7
        "symfony/monolog-bundle": "~2.4", // v2.6.1
        "sensio/distribution-bundle": "~3.0", // v3.0.9
        "sensio/framework-extra-bundle": "~3.0", // v3.0.3
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "hautelook/alice-bundle": "~0.2" // 0.2
    },
    "require-dev": {
        "sensio/generator-bundle": "~2.3" // v2.4.0
    }
}
userVoice