jQuery插件grunt构建更改webservice网址

时间:2015-05-28 14:24:26

标签: jquery plugins build gruntjs

快速提问,

我有一个使用Grunt的jquery插件,现在在我的js文件中我列出了一个web服务,即

//webservice settings
api_endpoint: "http://localhost/api/v1"

这在我的开发中很好,但是当我得到刺激时,我希望将其改为即。

api_endpoint: "http://prod/api/v1"

我的Grunt文件看起来像这样

    // Concat definitions
    concat: {
        options: {
            banner: "<%= meta.banner %>"
        },
        dist: {
            src: ["src/jquery.myPlugin.autocomplete.js", "src/jquery.myPlugin.tabs.js", "src/jquery.myPlugin.clearsearch.js", "src/jquery.myPlugin.v2.0.js"],
            dest: "dist/jquery.myPlugin.v2.0.js"
        }
    },

    // Lint definitions
    jshint: {
        files: ["src/jquery.myPlugin.v2.0.js"],
        options: {
            jshintrc: ".jshintrc"
        }
    },

    // Minify definitions
    uglify: {
        my_target: {
            src: ["dist/jquery.myPlugin.v2.0.js"],
            dest: "dist/jquery.myPlugin.v2.0.min.js"
        },
        options: {
            banner: "<%= meta.banner %>"
        }
    },

grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-variablize');
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-sass");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-copy');

grunt.registerTask("build", [
    "clean:pre",
    "concat",
    "uglify",
    "cssmin",
    "copy",
    'clean:post',
    "watch"
]);

1 个答案:

答案 0 :(得分:0)

使用grunt-replace

设置配置

replace: {
    api: {
        options: {
            patterns: [
                {
                    match: '/http://localhost/api/v1/g',
                    replacement: 'http://prod/api/v1'
                }
            ]
        },
        files: [
            {
                expand: true, 
                flatten: true, 
                src: ['dist/jquery.myPlugin.v2.0.js'], 
                dest: 'dist/'
            }
        ]
    }
}

加载包

grunt.loadNpmTasks('grunt-replace');

添加分发任务

// This could be done better, but just example
grunt.registerTask("dist", [
    "clean:pre",
    "concat",
    "replace:api",
    "uglify",
    "cssmin",
    "copy",
    'clean:post',
    "watch"
]);

并运行任务

grunt dist