gulp-useref不会替换< - !构建 - >当它在PHP文件中时

时间:2018-05-29 05:54:07

标签: gulp gulp-useref

我尝试使用gulp-useref。只要我的文件名是index.html,一切正常,但如果我将它重命名为index.php(也在gulpfile.js中将gulp.src('app/*.html')更改为gulp.src('app/*.php')),它仍会创建缩小的连续CSS和JS文件,但不替换目标文件中的<!-- build ><!-- endbuild >块。

源索引文件(和目标index.php文件):

<!-- build:js main.min.js -->
<script src="js/test.js"></script>
<script src="js/test.js"></script>
<!-- endbuild -->

目标index.html(当我将其重命名为index.php时,这就是我想要的):

<script src="main.min.js"></script>

工作gulpfile.js:

gulp.task('useref', function(){
    return gulp.src('app/*.html')
        .pipe(useref())
        .pipe(gulpIf('*.js', uglify()))
        // Minifies only if it's a CSS file
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest('dist'))
});

不替换<!-- build ><!-- endbuild -->块的那个:

gulp.task('useref', function(){
    return gulp.src('app/*.php')
        .pipe(useref())
        .pipe(gulpIf('*.js', uglify()))
        // Minifies only if it's a CSS file
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest('dist'))
});

什么可能导致此问题以及如何快速解决?

1 个答案:

答案 0 :(得分:0)

我使它工作,gulp.watch任务导致了这个问题。我在观看文件更改时没有正确运行useref任务。

如果不起作用:

// Watch for file changes:
gulp.task('watch', ['sass', 'useref', 'copy'], function(){
    gulp.watch('app/scss/**/*.scss', ['sass']);

    // Reload the browser whenever PHP or JS files change:
    gulp.watch('app/js/**/*.js', ['copy']);
    gulp.watch('app/**/*.php', ['copy']);
    gulp.watch('app/.htaccess', ['copy']);
    // Other watchers
});

当它突然开始工作时:

// Watch for file changes:
gulp.task('watch', ['sass', 'copy'], function(){
    gulp.watch('app/scss/**/*.scss', ['sass', 'useref']);

    // Reload the browser whenever PHP or JS files change:
    gulp.watch('app/js/**/*.js', ['useref']);
    gulp.watch('app/**/*.php', ['copy']);
    gulp.watch('app/.htaccess', ['copy']);
    // Other watchers
});