你應該嘗試使用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