Замените URL-адрес CSS на задачу Gulp

Я пытаюсь заменить путь файла CSS в index.html с помощью Gulp. Проблема в том, что у меня есть несколько проектов с разными именами тем, и путь к исходному файлу отличается в дистрибутиве.

index.html

<link href="app/style/themes/dark/theme.css" id="hmi-theme" rel="stylesheet" />

В моем дистрибутиве тема копируется в что-то вроде src\projects\project\app\style\themes

расстояние/index.html

<link href="[set the path here/]app/style/themes/dark/theme.css" id="hmi-theme" rel="stylesheet" />

Вот попытка с помощью gulp-find найти URL-адрес в index.html:

var gulp = require('gulp');
var find = require('gulp-find');
var replace = require('gulp-replace');

gulp.task('templates', function(){
    gulp.src(['index.html'])
        .pipe(find(/app\/style\/themes\/([^"]*)/g))
        .pipe(gulp.dest('file.txt'));

Это работает, у меня есть значение в файле назначения. Но можно ли использовать это значение с gulp-replace для изменения значения в файле HTML?

Я пробовал также что-то вроде:

.pipe(replace(/app\/style\/themes\/([^"]*)/g, "dist/" + find(/app\/style\/themes\/([^"]*)/g)))

Но значение в index.html было:

dist/[object Object]

person Jonathan Anctil    schedule 10.02.2017    source источник


Ответы (1)


Хорошо, я наконец нашел решение:

return gulp.src(path.join(projectDir, 'index.html'))
  .pipe(replace(/app\/style\/themes\/([^"]*)/g, function(cssPath) {
      return "my-new-path/" + cssPath;
  } ))
  .pipe(gulp.dest(distDir));
person Jonathan Anctil    schedule 10.02.2017