jQuery插件grunt构建更改Web服务URL

jQuery插件grunt构建更改Web服务URL,jquery,plugins,build,gruntjs,Jquery,Plugins,Build,Gruntjs,快速提问 我有一个使用Grunt的jquery插件,现在在我的js文件中列出了一个webservice,即 //webservice settings api_endpoint: "http://localhost/api/v1" 这在我的开发中很好,但是当我得到prod时,我想把它改成,即 api_endpoint: "http://prod/api/v1" 我的Grunt文件看起来像这样 // Concat definitions concat: { op

快速提问

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

//webservice settings
api_endpoint: "http://localhost/api/v1"
这在我的开发中很好,但是当我得到prod时,我想把它改成,即

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"
]);
//Concat定义
康卡特:{
选项:{
横幅:“
},
地区:{
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”
}
},
//皮棉定义
jshint:{
文件:[“src/jquery.myPlugin.v2.0.js”],
选项:{
jshintrc:“.jshintrc”
}
},
//缩小定义
丑陋的:{
我的目标:{
src:[“dist/jquery.myPlugin.v2.0.js”],
dest:“dist/jquery.myPlugin.v2.0.min.js”
},
选项:{
横幅:“
}
},
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(“构建”[
“清洁:预处理”,
“海螺”,
“丑陋”,
“cssmin”,
“复制”,
"清洁:邮政",,
“手表”
]);
使用

设置配置

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

获取一个错误,无法在没有@抱歉的情况下替换字符串。它必须是regexp模式。您还可以将端点url替换为
@@API\U端点
,并将
匹配
替换为
API\U端点
。但这意味着您也必须在
build
任务中替换它。