1

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

我需要在我的构建目录中复制一些文件夹 a 及其文件以及从根目录中的一些其他文件(没有缩小或连接)。示例是文件夹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'
        ]);
    };
4

1 回答 1

2

我使用以下脚本解决了我的问题。诀窍是向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' 不在 npm 注册表中

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

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

于 2014-08-14T14:27:15.000 回答