Gulp 快速入门
https://www.gulpjs.com.cn/docs/getting-started/quick-start/
gulp-file-include快速入门
Installation
npm install --save-dev gulp-file-include
package.json
{"name": "gulp-project","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1","build": "npx gulp fileinclude"},"author": "koubs","license": "ISC","devDependencies": {"gulp": "^4.0.2","gulp-file-include": "^2.2.2"}}
Example
目录结构
project
- dist
- node_modules
- src
- include
- page
- gulpfile.js
- package.json
复用代码
在include文件夹内添加三个文件,这些文件内包含每个页面复用的部分
footer.html
<meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>测试文件引入</title><meta name="keywords" content="aa,bb,cc,dd,ee,ff"><meta name="description" content="1234567890"><link rel="icon" sizes="any" mask="" href="" /><link rel="stylesheet" type="text/css" href="/dist/css/common.css" /><meta name="format-detection " content="telephone=no">
nav.html
<div><button><a href="/index.html">@@home</a></button><button><a href="/trade-news.html">@@news</a></button><button><a href="/product.html">@@product</a></button><button><a href="/contact.html">@@contact</a></button></div>
footer.html
<div>这是footer部分</div>
引入代码
在page目录添加文件
project.html
<!DOCTYPE html><html lang="zh"><head>@@include('../include/meta.html')</head><body>@@include('../include/nav.html',{"home": "主页","news": "行业新闻","product": "公司产品","contact": "联系我们"})<div>公司产品页</div>@@include('../include/footer.html')</body></html>
配置文件
var fileinclude = require('gulp-file-include');var gulp = require('gulp');gulp.task('fileinclude', function (done) {gulp.src('src/page/**/*.html').pipe(fileinclude({prefix: '@@',basepath: '@file'})).pipe(gulp.dest('dist'))done()});
运行命令
npm run build
输出文件
dist/project.html
<!DOCTYPE html><html lang="zh"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>测试文件引入</title><meta name="keywords" content="aa,bb,cc,dd,ee,ff"><meta name="description" content="1234567890"><link rel="icon" sizes="any" mask="" href="" /><link rel="stylesheet" type="text/css" href="/dist/css/common.css" /><meta name="format-detection " content="telephone=no"></head><body><div><button><a href="/index.html">主页</a></button><button><a href="/trade-news.html">行业新闻</a></button><button><a href="/product.html">公司产品</a></button><button><a href="/contact.html">联系我们</a></button></div><div>公司产品页</div><div>这是footer部分</div></body></html>
