gulp-file-includegulp 插件,它提供了一个 include 方法让我们可以像后端模板那样把公共部分的页面导入进来。

安装依赖包(包括了 gulp-file-include 和 del)

npm install --save-dev gulp-file-include del

项目结构目录

修改 gulpfile.js,结合 browser-sync 一起使用

'use strict';
var gulp = require('gulp'),
del = require('del'),
fileinclude = require('gulp-file-include'),
browserSync = require('browser-sync').create(); // 清除 dist 文件夹
gulp.task('clean', function () {
return del.sync('./app/dist');
}); // html 整合
gulp.task('html', function () {
return gulp.src('./app/src/template/*.html')
.pipe(fileinclude())
.pipe(gulp.dest('./app/dist'));
}); // 配置服务器
gulp.task('serve', function () {
browserSync.init({
server: {
baseDir: './app/dist'
},
port: 8000
});
// 监听 html
gulp.watch('./app/src/template/**/*.html', ['html']).on('change', browserSync.reload);
}); gulp.task('default', ['clean', 'html', 'serve']);

html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@@include('inc/header.html', {"title": "home"})
<p></p>
</body>
</html>

header.html:

<h1>@@title</h1>

打包:

gulp

浏览器会自动打开页面 http://localhost:8000,显示 home。在 dist 文件夹中查看 index.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>home</h1>
<p></p>
</body>
</html>

当然,gulp-file-include 还有很多强大的功能,具体内容可以参考官网,这里我就不多说了。

【gulp】gulp-file-include 合并 html 文件的更多相关文章

  1. 一篇迟到的gulp文章,代码合并压缩,less编译

    前言 这篇文章本应该在去年17年写的,但因为种种原因没有写,其实主要是因为懒(捂脸).gulp出来的时间已经很早了,16年的时候还很流行,到17年就被webpack 碾压下去了,不过由于本人接触gul ...

  2. gulp完成javascript压缩合并,css压缩

    最近需要对项目进行优化,主要是对js的压缩合并和css文件的压缩,查找相关资料之后发现gulp可以实现相关的功能,特此分享一下使用心得. 1.安装gulp gulp是基于Node.js的前端构建工具. ...

  3. Gulp压缩前端CS,JS,图片文件

    Gulp 基于Node.js的前端构建工具,Gulp有许多插件(这里是插件),使用Gulp可以实现前端代码的编译(sass.less).压缩.图片的压缩等,当然主要是前端工程化,不过我目前只是需要压缩 ...

  4. 合并Excel文件

    //合并Excel文件 private void MargeExcelFile(string destFile, string dirPath) { DirectoryInfo dir = new D ...

  5. 合并BIN文件的两种方法(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...

  6. 转:Intellij idea Version Control File Status Colors ( 版本控制文件状态颜色 )

    https://blog.csdn.net/Bruce_Lee__/article/details/80261308 Added —— 添加 Added in not active changelis ...

  7. 使用Python批量合并PDF文件(带书签功能)

    网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...

  8. MVC学习十一:合并资源文件(BundleConfig)

    在BundleConfig.cs文件下 //1.用户可以 手动 添加 js绑定对象,取一个 名字(虚拟路径),添加要绑定的JS文件 路径 bundles.Add(new ScriptBundle(&q ...

  9. Aspose.Pdf合并PDF文件

    使用Aspose.Pdf类库,有很多种方法可以合并PDF文件,这里简单介绍小生见到的几种: Doucment.Pages.Add PdfFileEditor.Append PdfFileEditor. ...

  10. [原]Nginx+Lua服务端合并静态文件

    http://homeway.me 0x01.About 源代码已经上传到github:https://github.com/grasses/nginx-lua-static-merger nginx ...

随机推荐

  1. node cluster模块,仿多线程并发调用,

    worker.js var cluster = require('cluster')function fibo(n) { return n == 0 ? 0 : n > 1 ? fibo(n - ...

  2. IDEA2018.2.2 版本配置注释模板

    Ctrl+Alt+S进入设置界面(我没改过按键映射,你也可以从File-OtherSetting进入设置),找到Editor->File and Code Templates,先在Include ...

  3. java加载类的顺序

    一.什么时候会加载类?使用到类中的内容时加载:有三种情况1.创建对象:new StaticCode();2.使用类中的静态成员:StaticCode.num=9;  StaticCode.show() ...

  4. [Tyvj1001]第K极值 (贪心?模拟)

    考前打tyvj的水题 题目描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去 ...

  5. async与await线程分配研究

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. python网络编程(九)

    单进程服务器-非堵塞模式 服务器 #coding=utf-8 from socket import * import time # 用来存储所有的新链接的socket g_socketList = [ ...

  7. C++程序设计方法2:函数运算符重载

    函数运算符()重载 函数运算符()也能重载,它使得对象看上去像是一个函数名 ReturnType operator() (Parameters) { ...... } ClassName Obj; O ...

  8. 在C#中,Json的序列化和反序列化的几种方式总结 转载

    转载自  https://www.cnblogs.com/caofangsheng/p/5687994.html    谢谢 在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据 ...

  9. Ubunut操作系统下nDPI的部署及简单使用

    [系统:Ubuntu16.04LTS ] [ nDPI版本:2.5.0] [ 内核:4.15.0-39-generic] 前期准备工作--依赖安装 所需依赖包(前两个ubuntu16已有不需安装) g ...

  10. mount: wrong fs type, bad option, bad superblock

    mount: wrong fs type, bad option, bad superblock on 125.64.41.244:/data/img,       missing codepage ...