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

How to include images in a build using Grunt?

本文关键字:包含 映像 构建 何使用 Grunt      更新时间:2023-09-26

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

我需要在我的构建目录中复制一些文件夹及其文件和一些其他文件从根目录(没有缩小或连接)。例如目录icons(如果可能的话包括子文件夹)、images和根目录下的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'
        ]);
    };

我用下面的脚本解决了这个问题。窍门是添加一些任务到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& # 39;不在NPM注册表

如何安装grunt以及如何使用它构建脚本

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