grunt contrib-sass sourcemap enable - sass

I can't get a lot of contrib-sass features to work in grunt. I dived into grunt a day ago and I found it really good.
Link to contrib-sass repo which says sourcemaps should be working:
https://github.com/gruntjs/grunt-contrib-sass/commit/e85ee70ccb8839867172b57ca1378293291f8037
note: I have sass bleeding edge, and this feature works fine if I use: sass --watch --scss --sourcemap --no-cache with google chrome canary sourcemaps and Sass stylesheet debugging
here is my Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd hh:mm:ss") %> */\n',
concat: {
options: {
separator: '\n// New file\n',
banner: '<%= banner %>'
},
develop: {
files: [
{ src: ['js/develop/plugins.js', 'js/develop/main.js'], dest: 'js/concDev.js' }
]
},
vendor: {
files: [
{ src: ['js/vendor/*.js', '!js/vendor/jquery-1.9.1.min.js', '!js/vendor/modernizr-2.6.2.min.js'], dest: 'js/concVend.js' }
]
}
},
uglify: {
options: {
banner: '<%= banner %>'
},
develop: {
files: [
{ src: ['<%= concat.develop.files[0].dest %>'], dest: 'js/concDev.min.js' }
]
},
vendor: {
files: [
{ src: ['<%= concat.vendor.files[0].dest %>'], dest: 'js/concVend.min.js' }
]
}
},
removelogging: {
dist: {
files: [
{ src: ['js/concDev.min.js'], dest: 'js/concDev.min.js' },
{ src: ['js/concVend.min.js'], dest: 'js/concVend.min.js' },
{ src: ['js/concDev.js'], dest: 'js/concDev.js' },
{ src: ['js/concVend.js'], dest: 'js/concVend.js' }
]
}
},
jshint: {
files: ['gruntfile.js', 'js/develop/*.js'],
options: {
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
},
cssmin: {
compress: {
options: {
banner: '<%= banner %>'
},
files: [
{ src: ['css/main.css'], dest: 'css/main.min.css' }
]
}
},
imagemin: {
dynamic_mappings: {
files: [
{
expand: true,
cwd: 'img/',
src: ['**/*.png', '**/*.jpg'],
dest: 'img/',
ext: '.png'
}
]
}
},
sass: {
compressed: {
files: {
'css/main.css': 'css/develop/main.scss'
},
options: {
outputStyle: 'compressed'
}
},
nested: {
files: {
'css/main.css': 'css/develop/main.scss'
},
options: {
sourcemap: true,
outputStyle: 'nested'
}
}
},
rsync: {
deploy: {
src: "./",
dest: '<%= connection.dest %>', // i.e. "var/www"
host: '<%= connection.host %>', // i.e. "user#server.com"
recursive: true,
syncDest: false,
exclude: ["/node_modules", ".*"]
}
},
watch: {
options: {
livereload: true
},
html: {
files: '*.html'
},
js: {
files: ['js/develop/plugins.js', 'js/develop/main.js'],
tasks: ['jshint', 'concat:develop']
},
css: {
files: 'css/develop/main.scss',
tasks: ['sass:nested']
}
}
});
// Load Plugins
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks("grunt-remove-logging");
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-rsync');
// Task Lists
grunt.registerTask('default', ['jshint', 'concat', 'uglify', 'imagemin', 'sass:nested']);
grunt.registerTask('server', ['watch']);
grunt.registerTask('deploy', ['sass:compressed', 'rsync' ]);
};
Btw, as I said im totally new with grunt, if you find other bad practise in my code please let me know. Also great plugin names for ftront-end work always welcome, I saw there are many, only faminilar with a few contrib ones yet.
Note: Somewhy, a lot of sass options doen't work, for example: noCache, lineNumbers, debugInfo, outputStyle:'compact','expanded' (compressed, nested works oO)
~ ae

As of today (07/10/2013):
If you install pre version of sass
gem install sass --pre
and grunt-contrib-sass package, your config file will allow to generate sourcemaps.
If you use compass try using compass: true option in sass task config block or loadPath

I was able to get this to work using the following:
* one note: the map file doesn't get tracked anywhere so I didn't realize it was rewriting it until I deleted a version of the map and then I noticed that it was writing the file.
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
dist: {
options: {
style: 'expanded',
debugInfo: true,
sourcemap: true
},
files: {
'styles/styles.css' : 'styles/sass/styles.scss'
}
},
},
watch: {
css: {
files: '**/*.scss',
tasks: ['sass'],
sourceComments: 'normal'
}
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default',['watch']);
}

Just to provide this as an actual answer, sourcemaps aren't available in sass stable yet. They're being worked on in an alpha release. The original question referenced a commit message that noted the code was being future-proofed.
As of 6/24/2013, sourcemaps aren't available in grunt-contrib-sass or grunt-contrib-compass.

It's easy right now, SASS version 3.4.5 works with source maps very well and has some more options to set it up:
$ sass -h
Usage: sass [options] [INPUT] [OUTPUT]
Description:
Converts SCSS or Sass files to CSS.
[...]
Input and Output:
--scss Use the CSS-superset SCSS syntax.
--sourcemap=TYPE How to link generated output to the source files.
auto (default): relative paths where possible,
file URIs elsewhere
file: always absolute file URIs
inline: include the source text in the sourcemap
none: no sourcemaps
[...]
So you can configure your Gruntfile.js e.g. like this:
[...]
sass : {
dist : {
files : {
'example.css' : 'example.scss'
},
options: {
sourcemap: 'auto'
}
}
}
[...]
Now if you run grunt sass task source maps are generated automatically.

Related

Why doesn't work this sass task in my gruntfile.js?

This task doesn't do anything:
sass: {
options: {
style: 'expanded',
sourceMap: true,
importer: compass
//includePaths: sassLib
},
dist: {
files: [{
expand: true,
cwd: 'scss',
src: ['globbed/style.scss'],
dest: 'css',
ext: '.style.css'
}]
}
},
But this is working, it compiles the style.scss to style.css:
sass: {
dist: {
files: {
'css/style.css': 'scss/globbed/style.scss',
}
}
}
What should I modify in the first task?
Try to move the options object into the dist one. Alternatively, since you are using Compass, you could try to use the grunt-contrib-compass module for an easier approach. In your case the code could be:
// ...
compass: {
compile: {
options: {
sassDir: "scss",
cssDir: "css",
relativeAssets: true,
outputStyle: "expanded"
}
}
},
// ...

Why my gruntfile does not compile scss files which includes "#import"?

It compiles a single scss file to css.
But if there are in that for example: "#import "variables/_colors.scss";" it is not working.
It is just a guess that this is the reason.
What is missing?
This is my gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
connect: {
uses_defaults: {}
},
sass: {
dist: {
options: {
style: 'expanded'
},
files: [{
expand: true,
cwd: 'sites/all/themes/tarsoly/sass/',
src: ['**/*.scss'],
dest: 'sites/all/themes/tarsoly/css/',
ext: '.css'
}]
}
},
watch: {
css: {
files: 'sites/all/themes/tarsoly/**/*.scss',
tasks: [ 'sass' ],
options: { livereload: true }
}
},
});
// Default task(s).
grunt.registerTask('default', ['watch']);
// Load Grunt plugins
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-sass-globbing');
};

Grunt Watch: Need to Save Twice After Every SASS Error

When using grunt-watch while editing my SASS files, if I get a SASS error, I have to save twice after correcting the error for it to be resolved. Here's the sequence of events:
I include a mixin that doesn't exist in my SASS file and save
grunt-watch throws a SASS error
I fix the error and save
grunt-watch throws the same error
I save again
grunt-watch compiles correctly
Here's my Gruntfile.js:
module.exports = function(grunt) {
// Configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: 'assets/img',
src: ['assets/img/**/*.{png,jpg,gif}'],
dest: 'assets/img'
}]
}
},
sass: {
dist: {
options: {
loadPath: require('node-neat').includePaths,
style: 'compact',
lineNumbers: true,
cacheLocation: 'assets/sass/.sass-cache'
},
files: [{
expand: true,
cwd: 'assets/sass',
src: ['*.scss'],
dest: 'assets/css',
ext: '.css'
}]
}
},
watch: {
options: {
livereload: true
},
css: {
files: ['assets/sass/**/*.scss'],
tasks: ['newer:sass'],
options: {
spawn: false
}
},
images: {
files: ['assets/img/**/*.{png,jpg,gif}'],
tasks: ['imagemin'],
options: {
spawn: false
}
},
js: {
files: ['assets/js/**/*.js'],
options: {
spawn: false
}
},
html: {
files: ['*.html'],
options: {
spawn: false
}
},
php: {
files:['**/*.php'],
options: {
spawn: false
}
}
}
});
// List plugins we're using
grunt.loadNpmTasks('grunt-contrib-watch'); // Watch - http://goo.gl/yxNE0
grunt.loadNpmTasks('grunt-contrib-imagemin'); // Image Minify - http://goo.gl/mkIRPE
grunt.loadNpmTasks('grunt-contrib-sass'); // SASS - http://goo.gl/pCHySn
grunt.loadNpmTasks('grunt-newer'); // Newer - https://goo.gl/3vBTnf
// Plugins to run when we run the 'grunt' command
grunt.registerTask('default', [
'imagemin',
'sass'
]);
};
Turns out that mysterious spawn option in Watch actually needs to be set to true for this particular instance. Like most people, I really have no idea how spawn works, but it fixes the issue.
watch: {
css: {
files: ['assets/sass/**/*.scss'],
tasks: ['newer:sass'],
options: {
spawn: true
}
}
}
You don't have to explicitly set the option to true, it's set by default so you can just not add it at all. I'm just including it here as an example.

Getting started with Grunt and sass (partials)

I am just getting started with Grunt and am a bit confused about how to configure my gruntfile.js to recognize changes to sass partial files. I've followed the article on 24ways.org by Chris Coyier and feel pretty comfortable with the basics. When running grunt watch, I can make changes to my style.scss file and grunt will watch and compile changes. However, I typically use style.scss as a long list of #imports:
#import 'partials/partialfilename';
When I make a change to any of my partials grunt does not notice anything has changed and thus nothing is compiled.
Within my project directory I have a sass directory that includes a style.scss file and a partials directory with lots of partials.
here is my grunt file:
module.exports = function(grunt) {
// 1. All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
dist: {
src: [
'js/custom.js',
'js/foundation.js',
'js/jquery.listnav.js',
'js/jquery.ioslider.min.js'
],
dest: 'js/build-scripts.js',
}
},
uglify: {
build: {
src: 'js/build-scripts.js',
dest: 'js/build-scripts-min.js'
}
},
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: 'images/',
src: ['**/*.{png,jpg,gif}'],
dest: 'images/'
}]
}
},
sass: {
dist: {
options: {
style: 'compressed'
},
files: {
'style.css': 'sass/style.scss'
}
}
},
watch: {
options: {
livereload: true,
},
scripts: {
files: ['js/*.js'],
tasks: ['concat', 'uglify'],
options: {
spawn: false,
}
},
css: {
files: ['sass/**/*.scss'],
tasks: ['sass'],
options: {
spawn: false,
}
},
images: {
files: ['images/**/*.{png,jpg,gif}', 'images/*.{png,jpg,gif}'],
tasks: ['imagemin'],
options: {
spawn: false,
}
}
}
});
// 3. Where we tell Grunt we plan to use this plug-in.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
// 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('default', ['watch', 'concat', 'uglify', 'imagemin', 'sass']);
};
I have tried a few different options for the watch css arguments such as:
css: {
files: ['sass/**/*.scss'],
tasks: ['sass'],
options: {
spawn: false
}
}
and
css: {
files: ['sass/*.scss'],
tasks: ['sass'],
options: {
spawn: false
}
}
I realize that I may be 'missing the point' here, I have read a bit about the grunt sass plugin 'ignoring' (_) underscores or partials because of the sass globbing rule (?) and through my research I have noticed that most people are using compass in their projects which I am not.
So, again my question is: How can I configure grunt to watch and compile changes to my sass partials and compile my css correctly?
I have found a solution.
css: {
files: ['sass/*.scss'],
files: ['sass/partials/*.scss'],
tasks: ['sass'],
options: {
spawn: false,
}
}

Grunt not executing sass command

My watch command executes all my tasks except for sass. When I execute grunt sass, I receive the following message error:
Warning:
You need to have ruby and sass installed and in your path for this task to work.
I don't know why this message is showed because I can generate my .css file from the .scss through the Ruby console (sass --watch styles/scss/general.scss:styles/css/general.css), so I have both Ruby and sass installed.
My Gruntfile.js is the following:
module.exports = function(grunt) {
// 1. All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
dist: {
src: [
'js/libs/*.js', // All JS in the libs folder
],
dest: 'js/build/production.js',
}
},
uglify: {
build: {
src: 'js/build/production.js',
dest: 'js/build/production.min.js'
}
},
imagemin: {
dynamic: {
files: [{
expand: true,
cwd: 'images/',
src: ['**/*.{png,jpg,gif}'],
dest: 'images/optimized/'
}]
}
},
sass: {
dist: {
options: {
style: 'compressed'
},
files: {
'styles/css/general.css': 'styles/scss/general.scss'
}
}
},
watch: {
scripts: {
files: ['**/*.{png,jpg,gif}', 'js/libs/*.js'],
tasks: ['imagemin', 'concat', 'uglify'],
options: {
spawn: false,
}
},
sass: {
files: ['css/*.scss'],
tasks: ['sass']
}
}
});
// 3. Where we tell Grunt we plan to use this plug-in.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-compass');
// 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('default', ['concat', 'uglify', 'imagemin', 'watch', 'sass']);
};
My OS is Windows7. Any idea?
Try
var config = {
app: 'app',
dist: 'dist'
};
grunt.initConfig({
sass: {
dist: {
options: {
style: 'compressed'
},
files: [{
cwd: 'styles/scss/',
src: '*.scss',
dest: 'styles/css/',
ext: '.css'
}],
},
watch: {
sass: {
files: ['styles/scss/{,*/}*.scss'],
tasks: ['sass'],
options: {
livereload: true
}
}
}
};
and would be better to place all the grunt.loadNpmTasks at the top before the grunt.initConfig

Resources