0

我对 Grunt.js 比较陌生,设置起来很简单,但现在我有两个问题:1)首先,你如何跟踪给定源文件夹中的任何文件夹?例如,图像文件夹本身可能包含图像,以及带有图像的文件夹和带有图像的文件夹等。2)有没有办法在它的主要(构建)文件夹中查看图像?没有任何永远循环...

这是我当前的配置:

module.exports = function(grunt) {
grunt.initConfig({
    jsDir: 'sources/js/',
    jsDistDir: 'public/js/',
    cssDir: 'sources/css/',
    cssDistDir: 'public/css/',
    concat: {
        js: {
            src: ['<%=jsDir%>*.js'],
            dest: '<%=jsDistDir%>javascript.js'
        },
        css: {
            src: ['<%=cssDir%>*.css'],
            dest: '<%=cssDistDir%>styles.css'
        }
    },
    min: {
        dist: {
            src: ['<%= concat.js.dest %>'],
            dest: '<%=jsDistDir%>javascript.js'
        }
    },
    cssmin: {
        dist: {
            src: ['<%= concat.css.dest %>'],
            dest: '<%=cssDistDir%>styles.css'
        }
    },
    imagemin: {
        dynamic: {
            files: [{
                expand: true,
                cwd: 'sources/images/',
                src: ['**/*.{png,jpg,gif}'],
                dest: 'public/images/'
            }]
        }
    },
    watch: {
        min: {
            files: ['<%=jsDir%>*.js'],
            tasks: ['concat:js', 'min']
        },
        cssmin: {
            files: ['<%=cssDir%>*.css'],
            tasks: ['concat:css', 'cssmin']
        },
        imagemin: {
            files: ['sources/images/**/*.{png,jpg,gif}'],
            tasks: ['imagemin']
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-yui-compressor');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', [
    'concat',
    'min',
    'cssmin',
    'imagemin',
    'watch'
]);
};

谢谢!

4

1 回答 1

0

问题1):

表达式**/*正是这样做的。(请参阅 imagemin > 动态 > 文件 > src)。在 concat 部分中,您可以执行类似的操作src: ['<%=jsDir%>**/*.js'],

问题2):使用一个监视任务:

安装:npm install grunt-contrib-watch --save-dev

--save-dev表示将依赖项写入 package.json 文件)

Gruntfile.js 中的配置:

(function () {
    'use strict';
}());

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt);  // auto load grunt tasks
    var globalConfig = {
        jsPath: 'js/',
        cssPath: 'css/',
        scssPath: 'scss/',
    };

    grunt.initConfig({
        globalConfig: globalConfig,
        pkg: grunt.file.readJSON('package.json'),

        concat: {
            // conf goes here
        },
        // your other tasks go here
        imagemin: {
            // conf goes here
        },

        watch: {
            dev:{
                files: ['<%= globalConfig.jsPath %>_*.js', '<%= globalConfig.scssPath %>**/*.scss'],
                tasks: ['concat','imagemin', 'yourothertasks']
            }
        }
    });

    grunt.registerTask('default', ['concat', 'imagemin', 'yourothertasks', 'watch:dev']);
};
于 2016-04-03T16:15:05.293 回答