如何使用Grunt为LESS配置sourceMaps?

时间:2014-02-13 00:53:18

标签: css less gruntjs source-maps

我正在使用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
            }
        }
    }
}

2 个答案:

答案 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

文件'main.css.map'注意:

  • 如果您愿意,可以重命名为:main.css.source-map.json
  • 我猜你有一些服务器规则设置不能正确地从'css'文件夹中提供* .map文件

压缩说明:

  • cleancss:true =将从main.css中删除sourceMappingURL注释
  • yuicompress:true =不会从main.css中删除sourceMappingURL注释

Gruntfile.js

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
        }
    }
},

laravel /公共/ CSS / main.css的

.class{ compiled css here } /*# sourceMappingURL=/css/main.css.map */

laravel /公共/ 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