This tutorial has a new version, check it out!

Sourcemaps only in Development

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

When we deploy, we're going to pass --production so that everything is minified like this. But when I do that, I don't want the sourcemaps to be there anymore. It's not a huge deal, but I'd rather not advertise my source files.

So now we have the opposite problem as before: we want to only run the sourcemaps when we're not in production. Add another config value called sourcemaps and set it to be not production:

31 lines gulpfile.js
... lines 1 - 8
var config = {
... lines 10 - 12
sourceMaps: !util.env.production
... lines 15 - 31

Make sense?

We can use this to only run the two sourcemaps line if config.sourcemaps is true. Instead of using util.noop() again, I want to show you another plugin called gulp-if. Go back and find it on the plugins page. Let's get this guy installed:

npm install gulp-if --save-dev

Go back and grab the require line:

31 lines gulpfile.js
var gulp = require('gulp');
... lines 2 - 6
var gulpif = require('gulp-if');
... lines 8 - 31

The whole point of this plugin is to help with the fact that we can't break up the pipe chain with if statements. With it, you can add gulpif() inside pipe(). The first argument is the condition to test - so config.sourcemaps. And if that's true, we'll call sourcemaps.init(). Do the same thing down for sourcemaps.write(): gulpif, config.sourcemaps, then sourcemaps.write('.'):

31 lines gulpfile.js
... lines 1 - 16
.pipe(gulpif(config.sourceMaps, sourcemaps.init()))
... lines 19 - 21
.pipe(gulpif(config.sourceMaps, sourcemaps.write('.')))
... lines 23 - 31

That pipe chain is off the hook! Go back and run just gulp:


We should see the non-minified version with sourcemaps. And that's what we've got! Now add --production:

gulp --production

No more sourcemaps!

Leave a comment!

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