GithubHelp home page GithubHelp logo

demo-gulp-file-include's Introduction

搭建目录结构

开始项目之前,我们先构建一下目录结构:

|-src  // 生产环境的资源文件夹
	|-client  // 可视资源存放的文件夹
		|-index.html  // 主页html 文件
		|-include  // 可复用的html片段
		|-lib  // 第三方工具库
		|-logic  // 自定义JS逻辑代码
		|-scss  // 项目样式文件
			|-index.scss
			|-include  // 可复用的样式文件夹
			|-images  // 各页面公共图片
			|-fonts  // 字体文件:iconfont
		|-images  // 项目示例图片
	|-server // 数据资源存放的文件夹
|-config //编辑部分配置文件:站点模板/图标

搭建复用HTML的环境

win7系统下安装node直接下一步就可以的,这是使用gulp的环境,先安装node再安装gulpgulp是复用HTML工具的环境;

注意:

  1. node和npm绑定安装,使用node -vnpm -v来检验是否安装成功;
  2. 使用gulp -v检验gulp是否安装成功,如果command not found,则需要考虑设置全局变量;(正常不会,如有问题自行百度

复用HTML相关的Gulp包

mkdir gulpFileInclude && cd gulpFileInclude
npm install --save-dev gulp gulp-file-include

建立目录结构

提取模板

|-index.html

<body>
  <div>
    @@include('./include/header.html')
    <section>这里是内容</section>
    @@include('./include/footer.html')
  </div>
</body>

|-header.html

<header>这里是头部</header>

|-footer.html

<header>这里是尾部</header>

配置gulp任务

var gulp = require('gulp');
var fileInclude = require('gulp-file-include');

gulp.task('default',function(){
		//src和dest的路径都是以gulpfile.js为基寻找的
		gulp.src('../src/client/*.html')
		.pipe(fileInclude({
				prefix:'@@',
				basepath:'@file'
		}))
		.pipe(gulp.dest('../dist'))
})

options.basepath: type: string, 值类型是字符串,该值/作为查找@@include文件的路径前缀; @root: @@include引用文件的相对路径是基于gulpfile.js所在的路径; @file:@@include引用文件的相对路径是基于引用文件所在的路径;(默认该设置的) your-basepath:@@include引用文件的相对路径是基于自定义的路径;

好了,上面是使用纯静态的页面来实现HTML复用的,不包含任何数据,可以简单的提高自己的效率,使得自己写的HTML代码更加具有维护性和可复用性。

demo-gulp-file-include's People

Contributors

mihuartuanr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.