2017-08-30 44 views
1

我可以从索引中重命名文件名。采用以下配置使用gulp删除index.html的正文内容

Gulp.js

var rename = require("gulp-rename"); 

gulp.task('modify-html', function() { 
    gulp.src('reports/index.html') 
    .pipe(rename('/app.html')) 
    .pipe(gulp.dest('reports/')); 
}); 

有谁知道如何使用一饮而尽

的index.html

<html> 
<body> 

<h1>My First Heading</h1> 
<p>My first paragraph.</p> 

</body> 
</html> 

回答

2

您可以使用gulp-html-replace删除我的html文件的主体内容它取代了HTML块。

标志,它需要的内容被删除

<!-- build:remove --> 
Put all the content which needs to be removed, in the block 
<!-- endbuild --> 

,并使用它像

var htmlreplace = require('gulp-html-replace') 
gulp.task('modify-html', function() { 
    gulp.src('reports/index.html') 
    .pipe(htmlreplace({ remove : '' })) 
    .pipe(rename('/app.html')) 
    .pipe(gulp.dest('reports/')); 
}); 

然后,您可以使用gulp-dom

var dom = require('gulp-dom'); 
gulp.task('modify-html', function() { 
    gulp.src('reports/index.html') 
    .pipe(dom(function() { 
      this.querySelector('body').innerHTML = ''; 
      return this; 
     })) 
    .pipe(rename('/app.html')) 
    .pipe(gulp.dest('reports/')); 
}); 

注:我有没有测试过它。

+0

感谢您的回答,但我想删除使用脚本本身的内容,对于上面的一个我必须手动添加一些额外的代码,这是必要的,从html –

+1

,真棒工作,非常感谢你,它现在工作得很好 –