node.js - 如何设置 grunt 文件来监视和编译 less 文件

标签 node.js gruntjs

我正在尝试使用 grunt 在开发过程中将我的 less 文件编译成 css。同时,观看并重新加载。我写的文件是这样的:

module.exports = function(grunt) {
grunt.initConfig({
    less: {
        development: {
            options: {
                compress: true,
                yuicompress: true,
                optimization: 2
            },
            files: {
                // target.css file: source.less file
                "public/css/bootstrap.css": "./public/less/bootstrap.less"
            }
        }
    },
    watch: {
        styles: {
            // Which files to watch (all .less files recursively in the less directory)
            files: ["public/less/*"],
            tasks: ['less'],
            options: {
                livereload: true,
            }
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', ['watch']);
};

我将其保存为 gruntfile.js 在我的项目根目录中。我做错了什么?

PS:我使用forever来启动我的应用程序,并在同一个终端窗口中使用命令grunt watch,但是当我更改我的less文件时,什么也没有发生。

PPS:我的文件结构如下:

root
  -- public
    -- less
    -- css

正如你在上面看到的,我的主要 less 文件位于 root/public/less/bootstrap.less,我希望 css 在 root/public/css/bootstrap.css 进行编译

非常感谢

最佳答案

您的设置似乎不错,但我在这里发现了一个问题...也许它可以解决您的问题

"public/css/bootstrap.css": "./public/less/bootstrap.less"
                             ^^

你的网址是相同的,但在第二次出现时你添加了额外的“./”我想如果你删除它它会起作用。

这是我自己的代码,运行良好。

module.exports = function(grunt){
    grunt.initConfig({
        //pkg:grunt.file.readJSON('package.json'),
        less:{
              development:{
                  options:{
                      compress: true,
                      yuicompress: true,
                      optimization: 2
                  },
                files:{
                    'webroot/css/meer/index/index2.css' : 'webroot/css/meer/index/index2.less'
                }
              }
        },
        watch: {
            styles:{
                options:{
                    livereload: true,
                    spawn: false,
                    event: ['added','deleted','changed']
                },
                files:['webroot/css/meer/index/*.less'],
                tasks:['less']
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default',['watch']);
}

关于node.js - 如何设置 grunt 文件来监视和编译 less 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20496941/

相关文章:

javascript - 无法让 socket.io 工作

angularjs - 在 yeoman/angular grunt build 中未更新对字体(font-awesome)资源的引用

javascript - 咕噜声 : convert images to base64 and insert into html

visual-studio-code - 从 NW ABAP 系统检索 sapui5 BSP

javascript - 如何使用 grunt 缩小一个 css 文件并将其编译成 JS

javascript - 在我的应用程序启动时创建所有路由是个好主意吗?

node.js - 新的连接导致当前连接停止工作

javascript - 用 browserify 填充一个 jQuery 插件

node.js - 尝试在 Ubuntu 上使用 Appium 但获取 Node.js :134 error

node.js - 获取连接 ECONNREFUSED 127.0.0.1 :80 when attempting HTTP request