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.
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
:
// ... 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:
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('.')
:
// ... lines 1 - 16 | |
gulp.src(config.assetsDir+'/'+config.sassPattern) | |
.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
:
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!
Hi Ryan,
First of all - a big thank you for all tutorials and especially this one, it is helping me a lot!
I'm trying to create a file _variables.scss which should hold all the global colors, mixins and stuff like this. What is the right approach of using these variables into the layout.scss file without gulp throwing the below error
Error: app/Resources/assets/sass/layout.scss
Error: Undefined variable: "$primary-color".
I have googled a bit and found that I should do:
@import "variables" into the layout.scss
but it seems(to me) that this is the lame way of using variables is SCSS so could you please let me know if there is any other way of using variables like the above described way?
Wish you a nice day ahead!