grunt.file.copy exclude empty folders

五迷三道 提交于 2019-12-06 12:45:01

UPDATE

I made a PR to grunt-contrib-copy but @shama came up with a better solution.

You can now handle this in all grunt tasks using filter:

copy: {
  main: {
    src: 'lib/**/*',
    dest: 'dist/',
    filter: function(filepath) {
      return ! grunt.file.isDir(filepath) || require('fs').readdirSync(filepath).length > 0;
    },
  },
},

The problem with your case is that those folders are not empty just being excluded.

A good solution would be to use grunt-contrib-copy and then grunt-contrib-clean :

module.exports = function(grunt) {
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.initConfig({
    copy: {
      main: {
        expand: true,
        cwd: 'src',
        src: ['**', '!**/*.js'],
        dest: 'dist/'
      }
    },
    clean: {
      main: {
        src: ['*/**'],
        filter: function(fp) {
          return grunt.file.isDir(fp) && require('fs').readdirSync(fp).length === 0;
        }
      }
    }
  });
  grunt.registerTask('copyclean', ['copy:main', 'clean:main']);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!