Как настроить относительный путь к файлу dist на gulp-inject?
У меня есть проблема, когда вводить ресурсы в index.html с gulp-inject.
У меня есть следующие потоки для стилей приложения, js-кода поставщика и js-кода приложения:
// Vendor JS code
var vendorStream = gulp.src(mainBowerFiles({
paths: {
bowerDirectory: config.bowerDir
},
checkExistence: true
}))
.pipe(concat('vendors.js'))
.pipe(gulp.dest(config.bowerLibDist))
.pipe(size({
title:"Vendor JS code"
}));
// JS App Code
var jsStream = gulp.src(config.jsSrcPath)
.pipe(concat('app.js'))
.pipe(uglify())
.pipe(gulp.dest(config.jsDistFolder))
.pipe(size({
title:"App js code"
}));
// SASS App Code
var cssStream = gulp.src(config.sassSrcPath)
.pipe(sass({
outputStyle: 'compressed'
})
.on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulpIf(config.production, cssnano()))
.on("error", notify.onError(function (error) {
return "Error: " + error.message;
})).pipe(gulp.dest(config.cssDistFolder))
.pipe(size({
title:"Styles"
}));
Я хочу использовать index.ml, внедрить эти ресурсы и поместить их в каталог dist, выполнив следующую задачу:
gulp.task('inject', function() {
return gulp.src('index.html', {cwd: './app'})
.pipe(inject(es.merge(vendorStream, jsStream, cssStream)))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(config.distFolder));
});
Это работает правильно, но проблема в том, что путь также включает в себя каталог dist. Это каталог издательской базы:
// Starts a server using the production build
gulp.task('server', ['build'], function () {
connect.server({
root: './dist',
hostname: '0.0.0.0',
port: 90
});
});
Как я могу настроить gulp-inject так, чтобы вместо
<script src="/dist/js/app.js"></script>
быть
<script src="js/app.js"></script>
Спасибо:)
1 ответ
Решение
Одним из способов является использование функции преобразования.
gulp.task('inject', function() {
return gulp.src('index.html', {cwd: './app'})
.pipe(inject(
es.merge(vendorStream, jsStream, cssStream),
{
transform: function( filepath, file, index, length, targetFile ) {
return inject.transform.call(inject.transform, filepath.replace(/\/dist\//, ""), file, index, length, targetFile);
}
}
))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(config.distFolder));
});
Вы также можете сохранить начальную функцию преобразования, если хотите изменить только имя файла.