Giter VIP home page Giter VIP logo

fe's Introduction

fe

包含:开发环境服务/生产环境构建/数据Mock等功能的前端开发模板

主要功能:

  • 模拟后端接口,在尽可能真实的环境下开发
  • 定义_map.js文件,自动拦截javascript中的“线上接口地址”到“本地模拟的接口”,无需修改javascript文件
  • 使用handlebars模板代替html文件,可根据不同的环境编译成不同版本的html文件

依赖于:

  • 基于express.js实现,可以使用任何express的中间件;
  • 使用gulp执行自动化构建;
  • 使用基于webpack的cmd模块化开发规范。

Installation

直接clone本项目,然后执行

npm install

Usage

所有开发文件在dev目录下,执行编译后的待发布文件在dist目录下

启动服务

gulp server

编译打包

gulp build

还可以单独运行各个任务,任务都定义在gulpfile.js文件下。

Documents

编译hbs文件

.hbs的文件也就是handlebars模板,你可以在bin/helpers中写自定义的helper,然后就可以直接在hbs文件中使用了,默认设置了developmentdevelopment两个helper,在本地运行的时候,默认是development环境,gulp编译时,是production环境

<head>
  {{#development}}
  <link href="css/index.css" type="text/css" media="all" rel="stylesheet" />
  {{/development}}
  {{#production}}
  <link href="css/index.min.css" type="text/css" media="all" rel="stylesheet" />
  {{/production}}
</head>

在本地运行的时候,会应用index.css文件,编译后的html文件只显示index.min.css

Mock

在Mock文件夹下js文件(除了_map.js)都会被认为是mock文件,其实就是express中定义的路由接口,具体可以参考express的文档

拦截ajax请求

mock已经很好用了,但是还不够好用。因为我们必须把http://localhost:3000/xxxx这样的接口写在js里,发布前还要替换(当然你可以使用gulp-replace等)。 我理想的方式是拦截url,重定位到本地接口(类似fiddler&charles),更妙的是我们使用的express,可以自定义静态文件中间件,通过一个map文件替换js里的url,然后直接响应给浏览器,而实际上js文件是没有变的。 例如我们的本地文件有这样一个请求:

  $.ajax({
    url: 'https://github.com',
    success: function(){

    }
  });

mock/_map.js中这么写:

module.exports = {
    // url映射举例
    'https://github.com': '/json'
};

启动服务,在浏览器中打开js文件,我们不会看到http://github.com,只会看到/json。 当然还有种方法是,在hbs中定义url的变量,因为有时url是根据后端的输出来拼接的。

##License MIT

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.