node.js - Grunt + Compass 不创建 CSS 文件

标签 node.js sass gruntjs compass-sass grunt-contrib-compass

使用 grunt compass 时出现以下错误:

“您必须从项目目录编译单独的样式表。”

我用 grunt 尝试了 3 种不同的方式。更多内容如下。

我的文件夹结构:

test \
     Gruntfile.js
     package.json
     \ node_modules \
     \ _src \ (this is the root folder of my Jekyll site)
        \ config.rb
        \ index.html
        \ static/
            \ _sass \
            \ css \
            \ images \
            \ js \

什么有效:

一切正常使用grunt和使用/_src/config.rb:

#project_path = "_src/"
http_path = "/"
css_dir = "css"
css_path = "static/" + css_dir
sass_dir = "_sass"
sass_path = "static/" + sass_dir
images_dir = "images"
images_path = "static/" + images_dir
output_style = :expanded
relative_assets = true
line_comments = false

因此,在 /src 中,我可以使用 compass watch 进行编译。


什么不起作用

无论如何,我决定放弃所有 GUI 应用程序并使用 grunt 来加强它。这就是问题开始的地方。

我也是最新的:

$ grunt --version
>> grunt-cli v0.1.13

$ npm -v
>> 1.4.3

$ node -v
>> v0.10.26

第一次尝试

我尝试的第一件事是尝试使用现有的 config.rb:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        // Config
        watch: {
            compass: {
                files: ['**/*.{scss,sass}'],
                tasks: ['compass:dev']
            },
            js: {
                // PLACEHOLDER HERE
            }
        },
        compass: {
            dev: {
                // dev options
                // ↓↓↓ HERE'S THE COMPASS CONFIG FILE ↓↓↓
                options: { config: '_src/config.rb' }
            },
            prod: {
                // prod options
            },
        },
        jshint: {
            options: { jshintrc: '.jshintrc' },
            all: ['Gruntfile.js', '_src/static/js/*.js']
        },
        browserSync: {
            files: {
                src : [
                    '_src/static/css/*.css',
                    '_src/static/images/*',
                    '_src/static/js/*.js',
                    '_src/**/*.html'
                ],
            },
            options: {
                watchTask: true
            }
        }
    });
    // Load the Grunt plugins.
    grunt.loadNpmTasks('grunt-contrib-compress');
    grunt.loadNpmTasks('grunt-contrib-compass');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-browser-sync');
    // Register the default tasks.
    grunt.registerTask('default', ['compass:dev', 'browserSync', 'watch']);
    grunt.registerTask('prod', ['compass:prod']);
};

/test 运行 grunt 没有错误,它甚至可以识别文件更改,但不会编译我的 sass。没有创建 css 文件。

我试过 grunt compass watch --verbose 但它中止了:

Running "compass:prod" (compass) task
Verifying property compass.prod exists in config...OK
File: [no files]
Options: cssDir=["_src/static/css"], sassDir=["_src/static/_sass"], imagesDir=["_src/static/images"], javascriptsDir=["_src/static/js"], fontsDir=["_src/static/fonts"], outputStyle="compressed", relativeAssets="true", noLineComments="true"
Options: cssDir=["_src/static/css"], sassDir=["_src/static/_sass"], imagesDir=["_src/static/images"], javascriptsDir=["_src/static/js"], fontsDir=["_src/static/fonts"], outputStyle="compressed", relativeAssets="true", noLineComments="true", time
You must compile individual stylesheets from the project directory.
Warning: ↑ Use --force to continue.

Aborted due to warnings.

…我一点都不明白。

第二次尝试——首选尝试

因为我不喜欢 config.rb 放在 /_src/ 中,所以我删除了它并替换了 options: { config: ' _src/config.rb' Gruntfile.js 中:

options: {
    //config: '_src/config.rb'
    cssDir: ['_src/static/css'],
    sassDir: ['_src/static/_sass'],
    imagesDir: ['_src/static/images'],
    javascriptsDir: ['_src/static/js'],
    fontsDir: ['_src/static/fonts'],
    outputStyle: 'expanded',
    relativeAssets: 'true',
    noLineComments: 'false'
}

报错与前者一模一样:

You must compile individual stylesheets from the project directory.

第三次尝试

作为最后的手段,我尝试将 config.rb 放在根目录 (/test) 中,向其中添加 project_path = "_src"并在 Gruntfile 中使用 options: { config: 'config.rb' } 。这确实和我的其他试验一样糟糕。

我非常希望 Gruntfile-only 版本能够工作。我想 compass 的基本路径不太好,但不知道该怎么做。


我已经在 StackExchange 上读到的解决这个问题的摘录

我已经读过:

PS:我也用RVM和Git。但我想这些在这里并不重要。


更新:

小步骤……一些进步(不是真的):

/Gruntfile.js

compass: {
    dev: {
        // dev options
        options: {
            config: 'config.rb',
            cssDir: '_src/static/css',
            sassDir: '_src/static/_sass'
        }
    },

我不明白为什么添加 config.rb(而不是像上面的“第二次尝试”那样把它全部写下来会改变一切,然后 grunt 决定最终输出一个 css 文件。

/config.rb

http_path = "/"
css_dir = "static/css"
sass_dir = "static/_sass"
images_dir = "static/images"
javascript_dir = "static/js"
fonts_dir = "static/fonts"
relative_assets = false

SASS:

.logo {
    background-image: image-url($logo);
    //width: image-width($logo);
    //height: image-height($logo);
}

呈现的 CSS:

// relative_assets = true
// image still shows up, but it's not the sites root
// /test/_src/static/images ==> _src should be the root
background-image: url('../../../static/images/gaya-design-logo.png');

// relative_assets = false
background-image: url('/static/images/gaya-design-logo.png');

我也得到一个错误:

WARNING: 'gaya-design-logo.png' was not found (or cannot be read) in /Users/pattulus/Sites/test/static/images

这很好,因为我在使用常规 compass watch 时也得到了这个。但是,对于 grunt,当我取消注释 SASS 代码中的两行时,它会中止吐出:

Errno::ENOENT on line ["28"] of /Users/pattulus/.rvm/gems/ruby-2.0.0-p451@test/gems/compass-0.12.6/lib/compass/sass_extensions/functions/image_size.rb: No such file or directory - /Users/patte/Sites/test/static/images/gaya-design-logo.png

奇怪,因为“普通 compass ”有效。我没有在全局范围内安装 compass ,gem 仅在该项目文件夹中处于事件状态。因此排除了这种可能性。

最佳答案

最后(经过一天的反复试验)归结为将 relativeAssets 设置为 false。

我在“第二次尝试”时犯的错误是将 bool 值放入 relativeAssets: 'false', 而不是 relativeAssets: false,

那个和一些调整 basePath 和“更多”。

module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        // Config
        watch: {
            compass: {
                files: ['**/*.{scss,sass}'],
                tasks: ['compass:dev']
            },
        },

        compass: {
            dev: {
                // dev options
                options: {
                    httpPath: '/',
                    basePath: '_src',
                    cssDir: 'static/css',
                    sassDir: 'static/_sass',
                    imagesDir: 'static/images',
                    javascriptsDir: 'static/js',
                    fontsDir: 'static/fonts',
                    outputStyle: 'expanded',
                    relativeAssets: false,
                    noLineComments: false
                }
            },
        },
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: ['Gruntfile.js', '_src/static/js/*.js']
        },
        browserSync: {
            files: {
                src : [
                    '_src/static/css/*.css',
                    '_src/static/images/*',
                    '_src/static/js/*.js',
                    '_src/**/*.html'
                ],
            },
            options: {
                watchTask: true
            }
        }

    });

    // Load the Grunt plugins.
    grunt.loadNpmTasks('grunt-contrib-compress');
    grunt.loadNpmTasks('grunt-contrib-compass');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-browser-sync');

    // Register the default tasks.
    grunt.registerTask('default', ['compass:dev', 'browserSync', 'watch']);
    grunt.registerTask('prod', ['compass:prod']);
};

这确实是漫长的一天……事实上,从周五开始我就一直在努力让它完全像这样运行(使用 browserSync 和整个战利品)。希望对下一位有帮助。

关于node.js - Grunt + Compass 不创建 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326211/

相关文章:

javascript - Bluebird — 在处理程序中创建了一个 promise ,但没有从中返回

sass - 如何使用 Webpack 2 导入 Foundation 的 SCSS?

css - 从 asset_path 输出中删除双引号

node.js - Node JS - child_process spawn ('npm install')在 Grunt 任务中导致 ENOENT 错误

javascript - Node 集群 : Why are all requests handled by only one worker?

node.js - 如何成功锁定 monorepo 中的 Node 模块依赖项?

html - 根据宽度渲染动态生成的背景

testing - Yeoman webapp 生成器 - 如何在浏览器中运行 mocha 测试

javascript - 使用 templateUrl 的单元测试指令有问题

javascript - 如何在某些路径上而不是其他路径上在 ExpressJS 中提供静态文件