javascript - Grunt Uglify 通配符和版本控制

标签 javascript node.js gruntjs

一段时间以来,我一直在尝试以特定方式组合两个 Grunt JS 插件。 基本上,我想使用 UglifyJS 缩小目录中的所有 JS 文件。接下来,我想使用版本控制插件(在本例中为 grunt-static-versioning)来实现缓存清除。

我的 grunt 文件如下:

module.exports = function(grunt) {
grunt.initConfig({
  clean: ['dest/js'],
  uglify: {
    options: {
      report: 'min',
      mangle: true
    },
    my_target: {
      files: [{
      expand: true,
      cwd: 'src/js',
      src: '**/*.js',
      dest: 'dest/js'
      }]
    }
  },
  cssmin: {
    options: {
    report:'min'
    },
    minify: {
        expand:true,
    cwd: 'src/css',
    src: '**/*.css',
    dest: 'dest/css',
    }
  },
  imagemin: {
    dynamic: {
      options: {
        optimizationLevel: 7
      },
      files: [{
        expand:true,
        cwd: 'src/assets',
        src: ['**/*.{png,jpg,gif}'],
        dest: 'dest/assets'
      }]
    }
  },
  htmlmin: {
    mini :{
      options: {
        removeComments: true,
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes: true,
        removeRedundantAttributes: true,
        removeEmptyAttributes: true,
        useShortDoctype: true
      },
      files: [{
        expand:true,
        cwd: 'src',
        src: '**/*.html',
        dest: 'dest'
      }]
    }
  },
  versioning: {               // Task
    options: {                // Task options
      cwd: ''
    },
    dist: {                   // Target
      options: {              // Target options
      },
      files: [{
        assets: '<%= uglify.my_target.files %>', 
        key: 'global',
        dest: 'dest/js',
        type: 'js',
        ext: '.js'
      }]
    }
  }
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-static-versioning');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');


grunt.registerTask('default', ['clean', 'uglify', 'cssmin', 'imagemin', 'htmlmin', 'versioning']);}

但是,执行后出现以下错误:

Running "versioning:dist" (versioning) task
Warning: Unable to read "dest/js" file (Error code: EISDIR). Use --force to continue.

我知道发生此错误是因为 dest/js 不是文件而是目录,但我不知道如何告诉版本正确的文件名。是否有特定的 Grunt JS 格式可以做到这一点?

最佳答案

似乎启用了一些附加选项导致上述错误。 我已经纠正了这些并做了一些更改。 运行下面的 grunt 文件,看看是否可以正常执行。

module.exports = function(grunt) {
grunt.initConfig({
    clean : [ 'dest/js' ],
    uglify : {
        options : {
            report : 'min',
            mangle : true
        },
        my_target : {
            files : [ {
                src : 'src/js/*.js',
                dest : 'dest/js/main.min.js'
            } ]
        }
    },
    cssmin : {
        options : {
            report : 'min'
        },
        minify : {
            expand : true,
            cwd : 'src/css',
            src : '**/*.css',
            dest : 'dest/css',
        }
    },
    imagemin : {
        dynamic : {
            options : {
                optimizationLevel : 7
            },
            files : [ {
                expand : true,
                cwd : 'src/assets',
                src : [ '**/*.{png,jpg,gif}' ],
                dest : 'dest/assets'
            } ]
        }
    },
    htmlmin : {
        mini : {
            options : {
                removeComments : true,
                collapseWhitespace : true,
                collapseBooleanAttributes : true,
                removeAttributeQuotes : true,
                removeRedundantAttributes : true,
                removeEmptyAttributes : true,
                useShortDoctype : true
            },
            files : [ {
                expand : true,
                cwd : 'src',
                src : '**/*.html',
                dest : 'dest'
            } ]
        }
    },
    versioning : { // Task
        options : { // Task options
            cwd : 'public',
            outputConfigDir : 'public/config'
        },
        dist : { // Target
            options : { // Target options
            },
            files : [ {
                assets : '<%= uglify.my_target.files %>',
                key : 'global',
                dest : 'dest/js',
                type : 'js',
                ext : '.js'
            } ]
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-static-versioning');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');

grunt.registerTask('default', [ 'clean', 'uglify', 'cssmin', 'imagemin',
        'htmlmin', 'versioning' ]);
}

关于javascript - Grunt Uglify 通配符和版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100288/

相关文章:

javascript - Grunt "watch"警告 : Path must be a string. 收到未定义

node.js - 带有 grunt 的 Concat bower 组件

javascript - Protractor 测试、访问和修改 Window 对象属性

javascript - 如何检查特定列的所有行中是否包含 "abc"

node.js - 指定根文件夹 express-http-proxy

node.js - 如何用变量替换全部?

node.js - 尝试访问 mongo 时 lambda 超时

javascript - 将选择元素序列化为字典的 jQuery 扩展方法

javascript - 在 Javascript/jQuery 中创建两个数字之间所有整数的数组,包括两个数字

node.js - 使用 Grunt watch 和 SASS 时出错