Grunt - 在同一文件夹中输出文件

时间:2016-04-04 12:29:37

标签: gruntjs

我有以下gruntfile.js:

module.exports = function(grunt) {
  require('jit-grunt')(grunt);

  grunt.initConfig({
    uglify: {
      options: {
        manage: false
      },
      my_target: {
        files: [{
          expand: true,
          cwd: 'assets/js',
          src: '*.js',
          dest: 'assets/js/min' 
        }]
      },
    },
    less: {
      development: {
        options: {
          compress: true,
          yuicompress: true,
          optimization: 2
        },
        files: [
        {
          expand: true, // Recursive
          cwd: "assets", // The startup directory
          src: ["**/*.less"], // Source files
          dest: "assets/", // Destination
          ext: ".css" // File extension 
        }
      ]
      }
    },
    watch: {
      styles: {
        files: ['assets/**/*.less', 'assets/js/*.js'], // which files to watch
        tasks: ['less', 'newer:uglify'],
        options: {
          nospawn: true
        }
      }

    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-newer');
  grunt.registerTask('default', ['less', 'watch', 'newer:uglify']);
};

以下路径分布:

| assets
|-- folder-1
    |-- less
|-- folder-2
    |-- less

使用当前gruntfile.js,我的输出是less文件夹的子级别:

| assets
|-- folder-1
    |-- less
       |-- css
|-- folder-2
    |-- less
       |-- css

但它应该在LESS文件的同一级别上。例如:

| assets
|-- folder-1
    |-- less
    |-- css
|-- folder-2
    |-- less
    |-- css

有关如何修改gruntfile.js的任何想法吗?这可能吗?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您需要一个css /文件夹作为每个文件夹{x} /文件夹中less /文件夹的兄弟。

这可以通过将rename属性添加到您的less task config:

来完成
files: [
    {
        expand: true, // Recursive
        cwd: "assets", // The startup directory
        src: ["**/*.less"], // Source files
        dest: "assets/", // Destination
        ext: ".css", // File extension
        rename: function (dest, src) {
            return (dest + src.replace('less/', 'css/'));
        }
    }
]