I am using grunt with cssmin to minify and concatenate css files.
Css files are well concatenated and minified but when I try to watch the non minified files in chrome dev tools under 'Sources' tab, files appears empty.
Here is my cssmin task:
cssmin: {
options: {
report: 'gzip',
keepSpecialComments: 0,
sourceMap: true,
outputSourceFiles: true
},
target: {
files: {
'web/assets/dist/css/vendors.min.css': [
'bower_components/bootstrap/dist/css/bootstrap.min.css',
'bower_components/chosen/chosen.min.css',
'bower_components/slabText/css/slabtext.css',
'bower_components/video.js/dist/video-js.css',
'bower_components/video.js/dist/video-js.css'
],
'web/assets/dist/css/app.min.css': [
'app/Resources/assets/css/jumbotron-narrow.css',
'app/Resources/assets/css/custom.css',
],
}
}
},
Source map setting is enabled under chrome.
web/assets/dist/css/app.min.css.map looks like this:
{
"version":3,
"sources":["app/Resources/assets/css/jumbotron-narrow.css","app/Resources/assets/css/custom.css"],
"names":[],
"mappings":"AAeA,QAdA,KAeI,eAAgB,KADpB,QA6BA,WAEI,cAAe,IAAI,MAAM,QA7C7B,KACI,YAAa,KCUb,YAAa,eAAkB,WDHnC,QAFA,QACA,WAEI,cAAe,KACf,aAAc,KASlB,WACI,WAAY,EACZ,cAAe,EACf,YAAa,KAIjB,QACI,YAAa,KACb,MAAO,KACP,WAAY,IAAI,MAAM,QAI1B,yBACI,WACI,UAAW,OAGnB,qBACI,OAAQ,KAAK,EAIjB,WACI,WAAY,OAGhB,gBACI,QAAS,KAAK,KACd,UAAW,KAIf,WACI,OAAQ,KAAK,EAEjB,gBACI,WAAY,KAIhB,oCAII,QAFA,QACA,WAEI,cAAe,EACf,aAAc,EAGlB,QACI,cAAe,KAGnB,WACI,cAAe,GC3EvB,WACI,YAAa,eACb,IAAK,iCAAkC,mBAG3C,WACI,YAAa,YACb,IAAK,8BAA+B,mBAOxC,GACI,WAAY,KACZ,eAAgB,UAChB,YAAa,EACb,YAAa,YAAe,WAC5B,YAAa,EAAI,EAAI,IAAI,KAG7B,4BACI,QAAS,MACT,YAAa,KACb,aAAc,KACd,cAAe,IAGnB,UACI,UAAW,MACX,WAAY,KAIhB,oBACI,MAAO,IAGX,sBACI,gBAAiB,KACjB,YAAa,EAGjB,cACI,aAAc,QAIlB,iCACI,QAAS"
}
Version from my package.json:
"grunt-contrib-cssmin": "^0.12.0",
What am I doing wrong?
Edit: Firstly, try specifying a
rootin youroptionsof theGruntfile.jsas follows:The Issue
The sourceMap file that
grunt-contrib-cssminis generating is incorrectly specifying the paths in thesourcesArray. The resultant sourceMap should be as follows:app.min.css.map
Note the sourceRoot prefixes
../../../../added to each path in thesourcesArray.In the SourceMap Specification a section reads:
If specifying a
rootpath, (as initially mentioned), does not resolve the issue then it indicates that this part of the specification is not being honoured bygrunt-contrib-cssmin.Alternate Solution
If the initial suggested fix does not work then consider adding a custom Task to invoke a Function which fixes each
sourcespath after the sourceMap(s) have been created bygrunt-contrib-cssmin.You could do something like this:
Gruntfile.js
Notes
The the custom grunt.registertask named
fixSourceMapsinvokes theprefixSourceMapfunction for each sourceMap file to fix.Two arguments are passed to the
prefixSourceMapfunction, namely:filePath- The path to the sourceMap to fix.sourceRootPrefix- The sourceRoot prefix e.g.../../For fixing both sourceMap files (
vendors.min.css.mapandapp.min.css.map) thesourceRootPrefixargument is specified as../../../../- This is specified as four levels due to the relationship between the resultant.min.cssfile and it's location to the original unminified source.cssfile within your directory structure.If you were to add another set of
.cssfiles to minify to yourcssmin.target.filesTask, in which the relationship between the resultant.min.cssfile and the unminified source.cssfile was not four levels, you will need to specify a differentsourceRootPrefixvalue.For example, lets say in
cssmin.target.filesthe following css is to be minified:As the resultant
quux.min.cssis saved in the directory two levels deep then thesourceRootPrefixargument is passed to theprefixSoureMapfunctions as'../../'. For example: