2017-08-30 43 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

,真棒工作,非常感謝你,它現在工作得很好 –