2016-07-31 73 views
0

我有一個app文件夾,我想在的兩個文件中替換http://localhost:8000companies-list.component.tsevents-list.component.ts。我試圖使用grunt-replace-string插件,它看起來成功運行,結果爲綠色Done,沒有錯誤,但沒有發生替換。Grunt-string-replace不能正常工作

這裏是如何Gruntfile.js樣子:

module.exports = function(grunt){ 

[ 
    'grunt-string-replace', 
].forEach(function(task){ 
    grunt.loadNpmTasks(task); 
}); 

// configure plugins 
grunt.initConfig({ 
    'string-replace': { 
     dist: { 
     files: { 
      './app/': ['companies-list.component.ts','events-list.component.ts'], 
     }, 
     options: { 
      replacements: [{ 
      pattern: 'http://localhost:8000', 
      replacement: 'http://fomoapp-melbourne.rhcloud.com', 
      }] 
     } 
     } 
    }, 
}); 

// register tasks 
    grunt.registerTask('default', ['string-replace']); 
}; 

回答

1

繁重Files object是用於指定目標文件的源文件的映射。這個映射的目的是告訴Grunt獲取源文件中的內容,對內容做些什麼,然後將響應寫入目標文件夾中的新文件。

它從你的配置中看到我希望Grunt重寫app /目錄中的兩個文件。這是行不通的。我敢打賭,如果你有詳細選項,grunt --verbose運行的呼嚕聲,你的輸出將包含以下內容:

Files: [no src] -> ./app/ 

這是因爲咕嚕找不到源文件,因爲你需要指定其相對路徑。

這取決於你如何構建你的應用程序,但你可能想要在app /下有一個src /文件夾和dist /文件夾。如果您選擇build your files objects dynamically,你的配置可能是這個樣子:

files: [{ 
    expand: true, 
    cwd: './app/src/', 
    dest: './app/dest/', 
    src: ['companies-list.component.ts', 'events-list.component.ts'] 
}] 

此外,documentation for grunt-string-replace狀態:

如果模式是一個字符串,只有第一次出現將被替換,如前所述在String.prototype.replac e。

這意味着,如果你想多個被替換的字符串情況下,你必須提供一個Regular Expression literal。例如:

replacements: [{ 
    pattern: /http:\/\/localhost:8000/g, 
    replacement: 'http://fomoapp-melbourne.rhcloud.com' 
}]