我正在使用grunt 0.4.2和grunt-contrib-less 0.9.0。我希望将LESS编译成支持源映射的CSS。
我的LESS文件位于public/less
,主要文件名为main.less
。
将public/less/main.less
编译为public/css/main.css
有效,但源地图不起作用。
下面我的Grunt配置有什么问题?
{
less: {
dev: {
options: {
compress: true,
yuicompress: true,
optimization: 2,
sourceMap: true,
sourceMapFilename: "public/css/main.css.source-map.json", //Write the source map to a separate file with the given filename.
sourceMapBasepath: "public/less", //Sets the base path for the Less file paths in the source map.
sourceMapRootpath: "/"//Adds this path onto the Less file paths in the source map.
},
files: {
"public/css/main.css": "public/less/main.less"
}
}
},
watch: {
styles: {
files: ["public/less/*"],
tasks: ['less'],
options: {
livereload: true,
nospaces: true
}
}
}
}
我不想在我的/public/less
文件夹中创建我的CSS;我想把它放进/public/css
。否则,我可以使用其他配置,它可以工作:
{
less: {
dev: {
options: {
compress: true,
yuicompress: true,
optimization: 2,
sourceMap: true,
sourceMapFilename: "public/less/main.css.map", //I DO NOT WANT THE CSS MAP HERE
sourceMapBasepath: "public/less", //Sets the base path for the Less file paths in the source map.
},
files: {
"public/less/main.css": "public/less/main.less"//I DO NOT WANT THE CSS HERE
}
}
},
watch: {
styles: {
files: ["public/less/*"],
tasks: ['less'],
options: {
livereload: true,
nospaces: true
}
}
}
}
答案 0 :(得分:16)
我发现LESS站点文档对于grunt-contrib-less使用的params更加清晰。
LESS:命令行用法 http://lesscss.org/usage/#command-line-usage-installing-lessc
NPM:grunt-contrib-less https://www.npmjs.org/package/grunt-contrib-less
laravel/gruntfile.js
laravel/public/less/main.less
laravel/public/css/main.css
laravel/public/css/main.css.map
less: {
dev: {
options: {
compress: true,
yuicompress: true,
optimization: 2,
sourceMap: true,
sourceMapFilename: 'public/css/main.css.map', // where file is generated and located
sourceMapURL: '/css/main.css.map', // the complete url and filename put in the compiled css file
sourceMapBasepath: 'public', // Sets sourcemap base path, defaults to current working directory.
sourceMapRootpath: '/', // adds this path onto the sourcemap filename and less file paths
},
files: {
'public/css/main.css': 'public/less/main.less',
}
}
},
watch: {
styles: {
files: ["public/less/*"],
tasks: ['less'],
options: {
livereload: true,
nospaces: true
}
}
},
.class{ compiled css here } /*# sourceMappingURL=/css/main.css.map */
{"version":3,"sources":["/less/main.less"], etc...
答案 1 :(得分:4)
如果您仍然遇到问题,请尝试将SourceMapURL设置为完整路径,例如:
http://www.yourdomain.com/assets/css/styles.css.map
显然这有点痛,因为它不是相对的,所以当你移动你的网站时,它需要改变。我和你有同样的问题,这让它对我有用。
我发现的另一点是您无法从文件系统加载SourceMaps。它必须来自Web服务器。为了解决文件系统问题,我相信你可以内联SourceMap。
此thread on GitHub有很多信息。
https://github.com/less/less.js/issues/1050#issuecomment-25621390