Compile less files with grunt-contrib-less won'

2019-02-03 07:37发布

I'm using Grunt for building my web project. I installed grunt-contrib-less package und added a task to my grunt.initConfig({..});

less : {
            options: {
                paths: ['js/base']
            },
            files: {
                'js/base/*.css' : 'js/base/*.less'
            }
        }

when I run the target less via grunt less, it runs without errors but doesn't compile the less file to a css file.

Running "less:files" (less) task

Done, without errors.

I have installed the lessc package via node, too. Doing lessc <source> <dest> works fine.

Currently I have pointed with the files option directly to one dir which contains one less file for testing. Even if I write the whole file name into files option, it happens nothing...

Later on I want to scan the whole js directory and compile all new modified *.less files.

I have installed following versions:

grunt-cli v0.1.6
grunt v0.4.0
node v0.8.7
npm 1.1.49

BR, mybecks

3条回答
走好不送
2楼-- · 2019-02-03 07:58

The glob pattern js/base/*.css does not match any files, therefore there is no destination. Usually, tasks like this expect multiple inputs to combine into a single output. Also, bear in mind that less is a multi-task, and putting files as a child of less is not doing what you expect. (it is treating it as a target, not a src/dest map)

If you want a 1-1 transform of .less into .css, you can use dynamic expansion. (or you can define each src/dest pair manually, but who wants to do that?)

In your case:

less: {
    options: {
        paths: ['js/base']
    },
    // target name
    src: {
        // no need for files, the config below should work
        expand: true,
        cwd:    "js/base",
        src:    "*.less",
        ext:    ".css"
    }
}
查看更多
ら.Afraid
3楼-- · 2019-02-03 08:07

I used Anthonies solution but stil had an error

Warning: Object true has no method indexOf

If I changed the order putting expand true as second it gave me the error

Unable to read "less" file

where "less" was the value of the first item in my list.

I solved it by changing files into an array like this:

less: {
    options: {

            paths: ["js/base"]
        },
        files: [{
            expand: true,
            cwd: "js/base",
            src: ["**/*.less"],
            dest: "js/base",
            ext: ".css"
        }]
},

I used "grunt-contrib-less" : "^0.11.0"

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-03 08:21

This works for me, but modified to reflect this scenario:

less: {
    options: {
        paths: ["js/base"]
    },
    files: {
        expand: true,
        cwd: "js/base",
        src: ["**/*.less"],
        dest: "js/base",
        ext: ".css"
    }
},
查看更多
登录 后发表回答