你应该尝试使用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']);
这是哪里的内容在什么位置?进入一个js文件? – jmartins
是的,在一个完整的js文件目录里面 – totothegreat