【问题标题】:Grunt watch throws NoMethodErrorGrunt 手表抛出 NoMethodError
【发布时间】:2014-01-08 18:12:09
【问题描述】:

我已阅读 grunt、sass 和 compass 文档,但找不到太多关于此错误的信息。我还看到另一个人有类似的错误,但这是由于他们的 contrib.rb 文件中的语法问题,我不相信我的 contrib.rb 文件中有。

问题:我运行 grunt watch 任务并在任何时候对我的 sass (*.scss) 文件进行更改时出现错误。 错误内容如下:

NoMethodError on line 264 of /Library/Ruby/Gems/1.8/gems/compass-0.12.2/lib/compass/configuration/inheritance.rb: _
Run with --trace to see the full backtrace

因此,我的 sass 文件都没有处理到我的 css 文件夹。我是 grunt 的新手,诚然,我对我所做的大量初始设置感到有些不知所措。关于我的文件可能如何配置不正确,或者与 grunt 相关的 compass 或 sass 版本之一是否可能存在错误(都是最新的非 beta 版本),我很乐意提供任何建议。

这是我的 contrib.rb 文件的文本:

require "susy"
css_dir = _/css
sass_dir = _/components/sass
javascripts_dir = _/js 
output_style = :compressed

我的 gruntfile.js :

module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.initConfig({
  uglify: {
    my_target: {
        files: {
            '_/js/script.js': ['_/components/js/*.js'] //tells grunt to track and compress any .js file found in components/js folder   
        } //files
      } // my_target
    }, // uglify
  compass: {
    dev: {
        options: {
            config: 'config.rb',
        force: true // <-- ???
        } //options
    } //dev
  }, //compass
  watch: {
    options: { livereload: true },
    scripts: {
        files: ['_/components/js/*.js'],
        tasks: ['uglify']
    }, //scripts
    sass: {
        files: ['_/components/sass/*.scss'],
        tasks: ['compass:dev']
    }, //sass
    html: {
      files: ['*.html']
    }
   } // watch
  })// initConfig
 grunt.registerTask('default', 'watch'); //makes 'grunt' command automaticaly execute the watch command to monitor JS files
} //exports

还有我的项目文件夹结构:

sass_first_proj
   |
   _(folder)
     |
     components
     |
     css
     |
     js
   images
   node_modules
     |
     grunt
     |
     grunt-contrib-compass
     |
     grunt-contrib-uglify
     |
     grunt-contrib-watch
     |
     matchdep

 .gitignore
 |
 config.rb
 |
 gruntfile.js
 |
 index.html
 |
 package.json

【问题讨论】:

  • 你能运行grunt compass还是得到同样的错误?我的猜测是问题根本不在于watch 任务。
  • 我在运行 grunt compass 时产生了同样的错误,但仍然无法让 css 文件链接到 scss 文件。
  • 嗯...你可以直接从命令行运行compass 吗?我不确定你需要的论点到底是什么。但如果它也不能运行,那么 Grunt 可能根本就不是问题。
  • 我能够从命令行运行 compass watch 任务而没有错误消息,但是当我切换到我的项目目录(上面显示为 sass_first_proj 的根文件夹)时,我再次生成了 NoMethodError。
  • 我不是在谈论 Grunt 任务,我是在问您是否可以在 Grunt 之外从命令行运行 compass 本身。我们需要将问题隔离到 compass 或 watch 任务。但有这样的错误,我猜这是指南针问题。

标签: css sass gruntjs compass-sass


【解决方案1】:

不确定您是否能够解决此问题,但我收到了同样的错误。

我必须在目录前加上一个正斜杠才能让 Grunt 找到正确的路径。我在为我的项目使用 Grunt 和 Compass 时使用了类似的步骤,而且我在我的 PC 上,所以我只是将路径放在单引号中。根据您的项目目录的结构,config.rb 文件中的两行分别如下所示:

css_dir = '/_/css'
sass_dir = '/_/components/sass'

在您的问题中,我注意到您说它是“contrib.rb”文件,但在您的文件目录中,它被命名为 config.rb。您在 gruntfile.js 文件中也将其引用为 config.rb,但我只是想确保项目根目录中的此文件被命名为“config.rb”。

我知道这是很多设置,所以我希望这对你有用。

【讨论】:

    【解决方案2】:

    所以事情是这样的,在将 compass 与 Grunt 一起使用时,我从未使用过 contrib.rb 文件。我知道你可以,如果你让它工作,那就太好了。但另一个选择是只在 grunt 配置中指定路径。当然,这一切都假设您能够实际编译 sass 文件。在谷歌上搜索该错误时,似乎大部分时间是导致问题的 contrib.rb 文件的问题。

    话虽如此,也许您可​​以放弃contrib.rb 文件而只使用Grunt?

    compass: {
      dev: {
        options: {
          sassDir: "_/components/sass",
          specify: "_/components/sass/**/*.scss", // might not be necessary for you
          cssDir: "_/css",
          javascriptsPath: "_/js",
          outputStyle: "compressed",
          require: "susy"
        }
      }
    }
    

    【讨论】:

    • 在查看了 github 上的 gruntfile.js 后,尝试了一下并尝试了一些我的想法,但仍然没有运气。我真的很感谢所有的帮助,但认为我最好的选择是把它全部炸毁并重新开始。如果我想出任何解决方案,我会在这里发布。再次感谢!
    • 啊……太糟糕了。如果您找到解决方案,请随时在此处发布,也许我们可以将旧的和新的并排,看看有什么变化。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多