gulp-html-import,在html中引入外部html文件

项目地址:

gulp-html-import

曾经学习PHP的时候,深深觉得include语法非常好用,后接触了ejs,发现里面也有类似的语法,能够方便地引入公共html文件;在学习了vue,react等框架以后,“组件化思想”更是在我脑海根深蒂固,再也无法忍受每个页面重复大量代码的原始方法。但是,在最最普通的静态html开发过程中,我实在懒得用框架,只想用最基本的方式写几个静态页面出来,这时候才想起,没有include语法,每个页面的公共部分都要手动复制粘贴一次,实在不科学……

早上看了张鑫旭老师的文章《JS一般般的网页重构可以使用Node.js做些什么》,深受启发,于是马上蹦起床尝试着把当中内容实现一遍,并尝试着搭配gulp,制作一个简单好用的插件,实现类似PHPinclude语法能够引入静态html文件的功能。

因为喜欢less语法,所以使用了类似less的@import 'xxx.less';作为引入方式。

下面直接粘贴项目readme的内容

gulp-html-import

A gulp plugin which can import .html files into .html files

Usage

First, install gulp-html-import as a devDependency:

npm install gulp-html-import --save-dev

Then add it to the gulpfile.js:

var htmlImport = require('gulp-html-import');gulp.task('import', function () {    gulp.src('./demo/index.html')        .pipe(gulpImport('./demo/components/'))        .pipe(gulp.dest('dist')); })

Example

Here is the files tree:

|-- demo|   ||   -- components|   |    ||   |    -- header.html|   |    ||   |    -- footer.html|   ||   -- index.html|-- gulpfile.js

Html files:

    Gulp-html-import Example    @import "header.html"    Hello World    @import "footer.html"

In your index.html, you should use

@import "XXX.html"

to import your components.

# I am the header
# I am the footer

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:

    Gulp-html-import Example    # I am the header    Hello World    # I am the footer

Everything is OK.

API

htmlImport(string)

string

Type: String

The url of your components

MIT

Copyright © 2016 Jrain Lau

关键字:gulp, JavaScript, node, node.js


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部