[英]How can I replace the variable which is in test.js file from gruntfile?
例如,我有這段代碼,用這個代碼我只能替換任何文件中的字符串,但是由於替換了變量的值,所以我需要類似的東西。
'string-replace': {
dist:{
files: [{
expand: true,
src: 'test.js'
}],
options: {
replacements: [{
pattern: 'blabla',
replacement: 'blabla1'
}],
}
}
我100%確定您要達到的目標。 您可以像下面那樣替換變量分配,以便它們獲得新的價值。 這是您要做什么?
'string-replace': {
dist:{
files: [{
expand: true,
src: 'test.js'
}],
options: {
replacements: [{
pattern: 'var myVar = oldValue', //replace var assignment
replacement: 'var myVar = newValue'//var has newValue now
}],
}
}
更新您只能在運行時訪問myVar的當前值。 您不能使用grunt來確定。 但是,您可以如上所述將myVar設置為所需的任何值。 或者例如,如果您有類似以下的代碼:
if(myVar)
{
//do this block
}
您可以進行文本替換以插入
var tmp = myVar
myVar = true;
if(myVar){
//will now always run
}
myVar=tmp // resetState
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.