如何使用Grunt在构建中包含图像?

时间:2014-08-13 20:38:20

标签: javascript gruntjs grunt-usemin

以下脚本正确地连接并缩小了css和js。

我需要在我的构建目录中复制一些foldersa及其文件和一些其他来自root的文件(没有缩小或连接)。 示例是文件夹icons(如果可能,包括子文件夹),images和root.xml中的config.xml。

知道如何更改脚本吗?


   module.exports = function (grunt) {

        // project configuration
        grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            uglify: {
                options: {
                    banner: '/* App: <%= pkg.name %> - Version: <%= pkg.version %> - Date: <%= grunt.template.today("dd-mm-yyyy") %> - Author: <%= pkg.author %> */\n\n'
                }
            },
            cssmin: {
                options: {
                    banner: '/* App: <%= pkg.name %> - Version: <%= pkg.version %> - Date: <%= grunt.template.today("dd-mm-yyyy") %> - Author: <%= pkg.author %> */\n'
                }
            },
            useminPrepare: {
                html: 'index.html',
                options: {
                    dest: 'build'
                }
            },
            usemin: { html: ['build/index.html'] },
            copy: {
                task0: {
                    src: 'index.html',
                    dest: 'build/index.html'
                }
            }
        });

        // load required modules
        grunt.loadNpmTasks('grunt-contrib-copy');
        grunt.loadNpmTasks('grunt-contrib-concat');
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.loadNpmTasks('grunt-usemin');

        // task definitions
        grunt.registerTask('build', [
          'copy:task0',
          'useminPrepare',
          'concat',
          'cssmin',
          'uglify',
          'usemin'
        ]);
    };

1 个答案:

答案 0 :(得分:1)

我使用以下脚本解决了我的问题。 诀窍是将一些任务添加到copy对象

        task1: {
            expand: true,
            src: ['icons/**'],
            dest: 'build/'
        },

module.exports = function (grunt) {

    // project configuration
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                banner: '/* App: <%= pkg.name %> - Date: <%= grunt.template.today("dd-mm-yyyy") %> - Author: <%= pkg.author %> */\n\n'
            }
        },
        cssmin: {
            options: {
                banner: '/* App: <%= pkg.name %> - Date: <%= grunt.template.today("dd-mm-yyyy") %> - Author: <%= pkg.author %> */\n'
            }
        },
        useminPrepare: {
            html: 'index.html',
            options: {
                dest: 'build'
            }
        },
        usemin: { html: ['build/index.html'] },
        copy: {
            task0: {
                src: 'index.html',
                dest: 'build/index.html'
            },
            task1: {
                expand: true,
                src: ['icons/**'],
                dest: 'build/'
            },
            task2: {
                expand: true,
                src: ['img/**'],
                dest: 'build/'
            },
            task3: {
                src: 'config.xml',
                dest: 'build/'
            },
            task4: {
                src: 'widget.info',
                dest: 'build/'
            },
            task5: {
                src: 'config.js',
                dest: 'build/'
            },
        },
        clean: {
            build: {
                src: [".tmp"]
            }
        }
        });

    // load required modules
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-usemin');
    grunt.loadNpmTasks('grunt-contrib-clean');

    // task definitions
    grunt.registerTask('build', [
      'copy:task0',     
      'copy:task1',     
      'copy:task2',     
      'copy:task3',     
      'copy:task4',    
      'copy:task5',     
      'useminPrepare', 
      'concat',
      'cssmin',
      'uglify',
      'usemin',         // build
      'clean'           // clean temp folders
    ]);
};

有用的资源可以帮助我:

https://www.youtube.com/watch?v=gIbfDxF69c8

grunt-contrib-build' is not in the npm registry

How to install grunt and how to build script with it

https://github.com/gruntjs/grunt-contrib-copy