S
S
Stanislav Fateev2015-12-27 19:09:55
Frontend
Stanislav Fateev, 2015-12-27 19:09:55

How to make transform work in gulp-inject?

Decided to try gulp, fiddling around with setting up gulp-inject to add dependencies. Everything works great, except for the transform function. I need to convert url for js files /frontend/src/from /static/. I tried this (copy-pasted from somewhere):

transform : function ( filePath, file, i, length ) {
                var newPath = filePath.replace('/frontend/src', '');
                console.log('inject script = '+ newPath);
                return '<script src="/static/' + newPath  + '"></script>';
            }

It does not output anything to the console, as a result, unconverted urls end up in the file. Other advice from the Internet did not help either. It feels like my transform just doesn't run (the default one works all the time).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-10
@Sergamers

If you carefully read the doc, then the following syntax is written there

var sources = gulp.src('Какой-то путь',{read: false}),
param = {
      transform: function (filepath) {
        if (filepath.slice(-3) === '.js') {
          var newPath = filepath.slice(6); // Что нужно обрезать
          return '<script src="' + newPath  + '"></script>';
        }else{
          // Use the default transform as fallback:
          return inject.transform.apply(inject.transform, arguments);
        }
      }
    };

gulp.src('Куда мы вставим наши файлы')
  .pipe(plumber()) // обработка ошибок
  .pipe(wiredep()) // bower файлы туда же суем
  .pipe(inject(sources, params)) // вставить свои файлы
  .pipe(gulp.dest('Куда это все сохранить'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question