报错Warning: Less failed to compile. Use --force to continue.

在grunt里配置好sass后,启用grunt server时,报错Warning: Less failed to compile. Use --force to continue.

server.js里代码如下
// SCSS.

    "\.scss": function(buffer, req, res, next) {
      var less = require("grunt-lib-less").init(grunt);
      var contentType = "text/css";
      var opts = {
        paths: ["." + req.url.split("/").slice(0, -1).join("/") + "/"]
      };

      // Compile the source.
      less.compile(String(buffer), opts, function(contents) {
        res.header("Content-type", contentType);
        res.end(contents);
      });
    },

gruntfile里配置的如下

sass: {
    dist: {
            files: {
                'app/styles/style.css': 'app/styles/style.scss'
            }
        }
    },
阅读 3.8k
2 个回答

什么鬼嘛?你用less编译sass?

现在已经解决问题,重新写了一个。利用node-sass,代码如下

function(buffer, req, res, next) {
                    var sass = require('node-sass');
                    var contentType = "text/css";
                    var opts = {
                        paths: ["." + req.url.split("/").slice(0, -1).join("/") + "/"]
                    };
                    // console.log(String(buffer));
                    sass.render({
                        data: String(buffer),
                        importer: function(url, prev, done) {
                            // url is the path in import as is, which LibSass encountered.
                            // prev is the previously resolved path.
                            // done is an optional callback, either consume it or return value synchronously.
                            // this.options contains this options hash, this.callback contains the node-style callback
                            var result = someSyncFunction(url, prev);
                            return {
                                file: result.path,
                                contents: result.data
                            };
                        },
                        includePaths: opts.paths,
                        outputStyle: 'compressed'
                    }, function(error, result) { // node-style callback from v3.0.0 onwards
                        if (error) {
                            console.log(error.status); // used to be "code" in v2x and below
                            console.log(error.column);
                            console.log(error.message);
                            console.log(error.line);
                        } else {
                            res.header("Content-type", contentType);
                            res.end(result.css.toString());
                        }
                    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进