咕噜字符串替换不起作用

Grunt-string-replace not working

我有一个 app 文件夹,我想在其中用两个文件 companies-list.component.tsevents-list.component.ts 替换 http://fomoapp-melbourne.rhcloud.comhttp://localhost:8000。我正在尝试使用 grunt-replace-string 插件,它似乎成功运行并显示绿色 Done 结果并且没有错误,但没有发生替换。

下面是 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']);
};

G运行t Files object用于指定源文件到目标文件的映射。此映射的目的是告诉 G运行t 获取源中文件的内容,对内容执行某些操作,然后将响应写入目标文件夹中的新文件。

根据您的配置,我认为您希望 G运行t 重写 app/ 目录中的两个文件。这是行不通的。我敢打赌,如果您 运行 g运行t 使用详细选项 grunt --verbose,您的输出将包含以下内容:

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

这是因为G运行t找不到源文件,需要指定它们的相对路径。

这取决于您想要如何构建您的应用程序,但您可能希望在 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 指出:

If the pattern is a string, only the first occurrence will be replaced, as stated on String.prototype.replace.

这意味着如果您想要 多个 字符串实例被替换,您必须提供 Regular Expression literal。例如:

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