2015-05-14 74 views
0

我有一个简单的任务,我怎样才能改变与咕噜另一行的内容?

当我写的命令

“咕噜打造”我希望这2条线改变状态,就像这样:

 baseUrl : 'http://localhost:3000/' 
     //baseUrl : 'http://188.166.18.108/' 

后:该命令之前

命令

 //baseUrl : 'http://localhost:3000/' 
     baseUrl : 'http://188.166.18.108/' 

构建变化的最终回:

 baseUrl : 'http://localhost:3000/' 
     //baseUrl : 'http://188.166.18.108/' 

无论如何可以用grunt来这么做吗?多谢你们!

+0

这是哪里的内容在什么位置?进入一个js文件? – jmartins

+0

是的,在一个完整的js文件目录里面 – totothegreat

回答

3

你应该尝试使用grunt-string-replace,像这样:

'string-replace': { 
    dist: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://localhost:3000/'", 
     replacement: "baseUrl : 'http://188.166.18.108:3000/'" 
     }] 
    } 
    } 
} 

那么你只能有一个在你的文件中的行,而不必评论它。

baseUrl : 'http://localhost:3000/' 

另外,如果你想更换本地主机的所有出现,你可以使用它作为一个模式,它会取代所有您IP地址

... 
pattern: "localhost", 
replacement: "188.166.18.108" 
... 

对于变回从IP地址本地主机您可以添加一个新任务来替换字符串并在您的构建中运行这两个任务。这将是这样的:

'string-replace': { 
    prev: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://localhost:3000/'", 
     replacement: "baseUrl : 'http://188.166.18.108:3000/'" 
     }] 
    } 
    }, 
    after: { 
    files: { 
     src: 'path/to/your/file', 
     dest: 'path/to/your/file' 
    }, 
    options: { 
     replacements: [{ 
     pattern: "baseUrl : 'http://188.166.18.108:3000/'", 
     replacement: "baseUrl : 'http://localhost:3000/'" 
     }] 
    } 
    } 
} 

,然后运行它想:

grunt.registerTask('replace', ['string-replace:prev', 'string-replace:after']);