项目地址:css
gulp-html-importhtml
曾经学习PHP的时候,深深以为include
语法很是好用,后接触了ejs
,发现里面也有相似的语法,可以方便地引入公共html文件;在学习了vue
,react
等框架之后,“组件化思想”更是在我脑海根深蒂固,再也没法忍受每一个页面重复大量代码的原始方法。可是,在最最普通的静态html开发过程当中,我实在懒得用框架,只想用最基本的方式写几个静态页面出来,这时候才想起,没有include
语法,每一个页面的公共部分都要手动复制粘贴一次,实在不科学……vue
早上看了张鑫旭老师的文章《JS通常般的网页重构可使用Node.js作些什么》,深受启发,因而立刻蹦起床尝试着把当中内容实现一遍,并尝试着搭配gulp
,制做一个简单好用的插件,实现相似PHPinclude
语法可以引入静态html文件的功能。node
由于喜欢less语法,因此使用了相似less的@import 'xxx.less';
做为引入方式。react
下面直接粘贴项目readme的内容git
A gulp plugin which can import .html files into .html filesgithub
First, install gulp-html-import
as a devDependency:npm
npm install gulp-html-import --save-dev
Then add it to the gulpfile.js
:gulp
var htmlImport = require('gulp-html-import'); gulp.task('import', function () { gulp.src('./demo/index.html') .pipe(gulpImport('./demo/components/')) .pipe(gulp.dest('dist')); })
Here is the files tree:框架
| -- demo | | | -- components | | | | | -- header.html | | | | | -- footer.html | | | -- index.html | -- gulpfile.js
Html files:
<!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Gulp-html-import Example</title> </head> <body> @import "header.html" <p>Hello World</p> @import "footer.html" </body> </html>
In your index.html
, you should use
@import "XXX.html"
to import your components.
<!-- header.html --> <h1>I am the header</h1>
<!-- footer.html --> <h1>I am the footer</h1>
When you get into the root directory(where your gulpfile.js
is) and type
gulp import
you could see a html file in /dist
like this:
<!-- /dist/index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Gulp-html-import Example</title> </head> <body> <h1>I am the header</h1> <p>Hello World</p> <h1>I am the footer</h1> </body> </html>
Everything is OK.
Type: String
The url of your components
Copyright © 2016 Jrain Lau