javascript - 使用 gulp 删除 index.html 的正文内容

标签 javascript html node.js gulp gulp-rename

我可以从索引中重命名文件名。使用以下配置

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/'));
});

有谁知道如何使用 gulp 删除我的 html 文件的正文内容

index.html

<html>
<body>

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

</body>
</html>

最佳答案

您可以使用gulp-html-replace它取代了 HTML block 。

标记需要删除的内容

<!-- 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/'));
});

注意:我没有测试过。

关于javascript - 使用 gulp 删除 index.html 的正文内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45953110/

相关文章:

javascript:首先遍历树宽度并在同一行记录相同级别的值

javascript - 使用 ES6 访问参数对象

html - 在 Bootstrap 中,如何将所有元素保持在同一行?

python - Docker Node :8. 16.0-alpine 错误:未找到:python2

javascript - 使用 nodeJS 和 Socket.io 统计在线用户数,不重复

javascript - 使用 document.write 显示 window.location.search 是否不安全/易受 xss 攻击?

javascript - D3圆包: html in text attribute

css - 如何在 CSS 中使用 "two-toned"字体变体?

html - 仅使用 css 输入后换行

node.js - 我将如何为 node.js 设计和实现非阻塞内存映射模块