Gruntfile.js 2.45 KB
module.exports = function (grunt) {
    // grunt配置,插件配置
    grunt.initConfig({
        // 调用grunt相关api,用于读取package.json文件信息
        // pkg相当于定义了一个变量,可以通过 <%== 变量 %>在后面调用
        pkg: grunt.file.readJSON('package.json'),

        // 这里插件的每个子任务代表一个功能,子任务名就用文件夹名,便于管理

        // clean清除插件配置
        clean: {
            demo: { // demo模块
                src: ['app/demo/tmp/', 'app/demo/dist/']
            }
        },

        // unglify压缩插件配置
        uglify: {
            demo: { // demo模块
                files: {
                    'app/demo/dist/demo1.min.js': ['app/demo/demo1.js']
                }
            }
        },

        // replace文本替换插件配置
        replace: {
            demo: { // demo模块
                src: ["app/demo/demo1.html"],
                dest: "app/demo/tmp/",
                "replacements": [{
                    from: /(<script src=\")([^\"]*?)(.js\")/mg,
                    to: '$1dist/$2.min$3'
                }]
            }
        },

        // static-inline静态文件插入替换插件配置
        staticinline: {
            demoo: { // demo模块
                options: {
                    basepath: 'app/demo/' // 查找路径
                },
                files: {
                    'app/demo/dist/demo1.dist.html': 'app/demo/tmp/demo1.html'
                }
            }
        }

    });

    // grunt加载的插件
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-contrib-uglify");
    grunt.loadNpmTasks("grunt-static-inline");
    grunt.loadNpmTasks("grunt-text-replace");


    // 定义grunt任务列表
    /*
        这里定义了一组任务,并且制定到default任务下,可以一键grunt执行
        任务组有顺序,如下说明:
        1、clean 清除 dist、tmp目录,准备重新生成
        2、uglify 压缩指定的脚本到dist目录,脚本后缀名为.min.js结尾
        3、replace 将指定的html文件复制到tmp目录下,将其中的js后缀名替换成.min.js
        4、staticinline 将.min.js的内容inline-内联插入到tmp的html文件中,然后重命名为.dist.html到dist目录中
     */
    grunt.registerTask('default', ['clean', 'uglify', 'replace', 'staticinline']);

};