grunt-localhosts v0.1.0 NPM Version NPM

Change your local machine hosts.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-localhosts --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-localhosts');

change localhosts task

Run this task with the grunt localhosts command.

This task was designed to be used in conjunction with another task grunt server which open local server binds on localhosts 127.0.0.1, but if your page requests some cgi server for example cgi.qun.qq.com, the Same-Origin Policy will not allow you do this by localhosts. At the time you can change your local hosts file 127.0.0.1 qun.qq.com, then you can access the cgi server by CORS just simply open you local pages at "qun.qq.com/index.html", by the way, change the grunt connect open page to url "qun.qq.com" not localhost !

Options

rules

Type: Array Default: `empty array'

just statement which localhost you want to set or remove !

rules: [{
    ip: '127.0.0.1',    // point domain "A" to address "B"
    hostname: 'qun.qq.com',
    type: 'set'
}]
rules: [{
    domain: 'baidu.com',    // point domain "A" to domain "B"
    hostname: 'qun.qq.com',
    type: 'set'
}]

Usage examples

Basic Use

In this example, grunt localhosts (or more verbosely, grunt localhosts:set) will change your localhosts as you wish, obviously you can use 'grunt localhosts:remove' too.

// Project configuration.
grunt.initConfig({
    localhosts: {
        set : {
            options: {
                rules: [{
                    ip: '127.0.0.1',
                    hostname: 'qun.qq.com',
                    type: 'set'
                }, {
                    ip: '127.0.0.1',
                    hostname: 'find.qq.com',
                    type: 'set'
                }, {
                    domain: 'baidu.com',    // point domain "A" to domain "B"
                    hostname: 'find.qq.com',
                    type: 'set'
                }]
            }
        },
        remove : {
            options: {
                rules: [{
                    ip: '127.0.0.1',
                    hostname: 'qun.qq.com',
                    type: 'remove'
                }]
            }
        }
    },
});


grunt.registerTask('serve', function (target) {
    grunt.task.run([
        'concurrent:server',
        "localhosts:set",
        'connect:livereload',
        'open:server',
        'watch'
    ]);
});

grunt.registerTask('build', [
        'localhosts:remove',    // clear previously set localhosts
        '.......'    // content removed for brevity
]);

Release History

About

grunt-localhosts is an open-source project by Tencent which builds on top of Node.js.

Used by people within (JOIN US)

Tencent Xiaomi


Task submitted by materliu

This file was generated on Fri Apr 4 2014 22:38:50.