【问题标题】:gulp replace task not workinggulp替换任务不起作用
【发布时间】:2015-06-11 03:49:51
【问题描述】:

我正在使用 AngularJS 构建一个 Ionic 应用程序,并且我有一个 gulp 任务应该替换文件中的数据。该任务执行没有错误,但它不会替换它要替换的数据。它就像什么都没有执行。

gulp.task('replace', function () {  
  // Get the environment from the command line
  var env = args.env || 'localdev';

  // Read the settings from the right file
  var filename = env + '.json';

  var settings = JSON.parse(fs.readFileSync('./config/' + filename, 'utf8'));
  console.log(settings.apiUrl)
// Replace each placeholder with the correct value for the variable.  
  gulp.src(paths.replace)  
    .pipe(replace({
      patterns: [
        {
          match: 'apiUrl',
          replacement: settings.apiUrl
        }
      ]
    }))
  .pipe(gulp.dest('./js/services'));
  console.log("here")
});

js/constants.js 文件

angular.module('loanstreet.constants',[])   
    .constant('apiUrl', '@@apiUrl');

localdev.json

{
  "apiUrl":"http://10.0.3.2:3000"
}

在替换 apiUrl 值后,它应该创建并发送到目标 .js/services/constants.js 但它也不会创建它。

任何建议,因为老实说我看不出代码有什么问题。任何帮助表示赞赏。

【问题讨论】:

  • 是源文件,你给出的正确吗?
  • 你的代码中是否使用了replace() gulp-replace?
  • 我相信本例中使用的插件是gulp-replace-task,它基于applause

标签: angularjs ionic-framework gulp


【解决方案1】:

假设你使用的是gulp-replace,那么你使用的参数是错误的。

应该是replace(pattern, replacement)

我还认为您的示例中的 match 值是错误的。它将替换键 值。您需要匹配器中的@@。

改成

.pipe(replace('@@apiUrl', settings.apiUrl))

如果您需要替换多个内容,则将更多调用链接到replace()

.pipe(replace('@@apiUrl', settings.apiUrl))
.pipe(replace('@@another', 'foo'))
.pipe(replace('@@more', 'bar'))

【讨论】:

【解决方案2】:

我认为你应该换行:

gulp.src(paths.replace) 

收件人:

gulp.src('./js/constants.js') 

或者告诉我们paths.replace此时有什么价值。

【讨论】:

  • paths.replace 在 d 时刻有 './js/constants.js'
  • 您是否得到了您添加的console.log() 的任何输出?
  • 是的,控制台输出是正确的,但奇怪的是它没有改变,也没有创建 d dest 文件
【解决方案3】:

假设使用了gulp-replace-task,我得到了同样的结果,任务执行没有错误但没有发生数据替换。

env: windows
node: v10.14.1
gulp: 3.9.1

最后我还是使用了applause.

因此在上面的代码中会是这样的:

applause      = require('applause'),
.... 

var settings = JSON.parse(fs.readFileSync('./config/' + filename, 'utf8'));
var options = {
  patterns: [
    {
      match: 'apiUrl',
      replacement: settings.apiUrl
    }
  ]
};

var applauseRunner = applause.create(options);
var result = applauseRunner.replace(content);

// Do something with the result.content (i.e. pipe to stream)

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多