原文地址:前者快速开模版
之前一直当开移动端的单页面应用,而移动web对性能和作用要求是比较苛刻的,所以用的还是轻量级的框架体系。基本上是Zepto加自己开之单页面框架,再加上若干大概的堆栈。这样前端要加载的文书充分有点,修改起来为十分便利,同时这样的轻量级的系统使gulp进行自动化管理为是殊方便的。
自从开发react项目后,对它们的工程化和开模式呢是初步了见识,标准的框架体系就是重量级的react+redux+webpack。开发大型项目和后台管理体系就此react,vue确实是毋庸置疑的。但是付出有稍项目,比如前端h5之类的还要产生杀鸡用牛刀的感觉到。
于是借鉴webpack工程化思想入手写了只前端快速开模版,用于支付快速简洁的种,比如事先的单页面应用。而此项目模版就是相仿前端开发的底手架了,基本的笔触就是是自动化和前端编译,主要及以下目的:
- 利用less预编译css,使用autoprefixer进行体制兼容
- js代码用babel编译,因此而基于es6,es7编辑代码
- 类库文件使用npm包,js文件使用browserify组织和打包,不再行使sea,require之类的加载器
- html代码用swig模版引擎进行团队和预编译
- 静态资源版本采用rev管理,当css或js文件内容有转变,打包时自动更新版本
- 前端开发基础样式,框架的组合。
工具链
- 构建工具gulp
- 编译打包工具browserify
- 代码编译babel
- css兼容autoprefixer
- css预编译less
- html代码预编译模版swig
- 前者代码版本更新rev
集体类库
- jQuery
- Zepto
- 友好付出之精简单页面框架app.js
- 图标fontello
- 字体reboto
- 基础样式base.css
文件目录结构
- css: css文件
- less: less文件
- js: js文件
- dist: 打包生成的种类文件
- layout: html母板
- lib: 公共类库
- template: html页面
种类自动化
没有采取重量级的webpack,而是采用轻量级的gulp和browserify
国有类库
公共类库的文本,主要有基础样式base.css,图标样式reboto,字体样式fontello,单页面框架app,直接拷贝到dist文件就ok。其他npm包跟nodejs项目一律以node_modules中,不需处理。
//复制公共库目录下的所有内容
gulp.task('copy',function(){
return gulp.src('./lib/**')
.pipe(gulp.dest('./dist/lib/'));
});
css文件
首先以less文件转为css,用autoprefixer添加兼容性前缀,合并减,放到dist目录,最后所以rev插件生成版本信息,这个后面用于替换更新链接用。
//编译css,添加兼容后缀,压缩
gulp.task('css', function() {
return gulp.src('./less/*.less')
.pipe(less())
// .pipe(concatCss("index.css"))
.pipe(postcss([ autoprefixer({
"browsers": ["last 2 version", "> 0.5%", "ie 6-8","Firefox < 20"]
// "browsers": ["last 2 version", "> 0.1%"]
})
]))
.pipe(cleanCSS())
// .pipe(rename({suffix: '.min'}))
// .pipe(gulp.dest('./dist/css'));
.pipe(rev())
.pipe(gulp.dest('./dist/css'))
.pipe(rev.manifest())
.pipe(gulp.dest('./rev/css'));
});
js文件
我此出少数单输入文件,可以天天将新入口文件上加到scripts数组吃,这里举行的即是应用babel转换代码,然后拿表面文件,npm包打包上,生成sourcemap,输出到dist文件夹,最后一样用rev插件生成版本信息。
var scripts=['app','index'];
scripts.map(name=>{
gulp.task(name,function(){
return browserify({
entries:'./js/'+name+'.js', //entries file name
debug:true // set true so the bundle file can generate sourcemap
})
.transform(babelify,{
plugins: ["transform-runtime"],
presets: [
'es2015', //convert to es5
'stage-0' //es7
]
})
.bundle() //merge
.pipe(source(name+'.js'))
.pipe(buffer())
// .pipe(uglify())
.pipe(sourcemaps.init({loadMaps: true})) //External sourcemap file
.pipe(sourcemaps.write('./'))
.pipe(rev())
.pipe(gulp.dest('./dist/js/'))
.pipe(rev.manifest(name+'.json'))
.pipe(gulp.dest('./rev/js/'));
});
});
html文件
html编译我利用的凡模版引擎是swig,这里用的凡gulp-swig插件,当然为可据此ejs或jade的发动机。但自己个人于习惯用swig,因为它还灵敏,支持各种文件格式,可以一直运用html文件,也支持ejs不支持之layout等。gulp-swig插件的用啊非常简单。
//swig编译输出html
gulp.task('html', function() {
return gulp.src('./template/*.html')
.pipe(swig({
defaults: {cache: false }
}))
.pipe(gulp.dest('./'))
});
文本版本控制
之前编译css和js的时刻已经调用rev生成了笔录了版本信息的json文件,如果情节产生变动它会转变新的md5文件称。这里虽之所以rev-collector替换html文件被除了md5部很相同文件称之链接,这样便不怕贯彻了版本更新功能,用之插件可以创新css,js,图片等的本。
//更新css和js版本,同时替换html中的链接标签
gulp.task('rev', scripts.concat(["css","html"]),function () {
return gulp.src(['./rev/**/*.json', './*.html'])//add md5 suffix to js and css file, replace the link of html as well
.pipe( revCollector({
replaceReved: true,
dirReplacements: {
'/css': '/css',
'/js': '/js'
}
}))
.pipe( gulp.dest('./dist') );
});
html里面替换后底链接格式如下
<link rel="stylesheet" href="./css/app-d333f590b0.css">
<script src="./js/app-62bad8e549.js"></script>
类模版
项目自动化配置了晚,接着就是配置型模版了,这里分点儿种档次的模版:1.移动端优先的单页面;2.pc端优先的常备页面。
swig标签语法
- extends 继承父模板,必须在文件最前
- block 定义一个片,使之得给延续的沙盘重写,或者更写父模板的和名块
- parent 将大模板被及名块注入当前片被
- include 包含一个模板到即职
倒端单页面模版
于layout文件夹新建移动端的单页面模版app-layout.html,然后再度盖立子目录partial,用于存放公共代码块。
我们清楚加载页面时更去加载外部文件是待吃加载时间,而页面直起好大大提高加载速度,同时先行加载部分样式也足以免加载时之白屏问题。这为是咱这里将国有部分的js和css以代码块的花样嵌入至html文件被,达到页面直出的效力。
swig支持以html文件嵌入,同时也得以将css文件嵌入,我此就是是将单页面样式文件app.css和基本功样式文件app-base.css的始末一直出口到style标签中。主要的代码块有:
- meta.html: 头部meta标签
- header-script.html: 主要是动态设置fontsize大小的剧本
- footer-script.html: 主要是页面加载过程的卡通片脚本
当然矣图标样式,好看的书体文件,单页面的css,基础之体css都发出,
同时母版还挖掘来了几乎单block给后续的页面进行重写,比如block title,block
css,block js,内容block
content。css块和js块既好形容外部链接也足以一直写代码。
我们可以依据自己的急需开展改动及部署文件,具体内容看如下代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
{% include './partial/meta.html' %}
<!-- 页面内加载的头部js -->
{% include './partial/header-script.html' %}
<!-- 页面内加载的css -->
<style>{% include '../lib/app/app.css' %}{% include '../lib/app-base.css' %}</style>
<!-- 图标 -->
<link rel="stylesheet" href="./lib/fontello/fontello.css">
<!-- 字体 -->
<link rel="stylesheet" href="./lib/reboto/roboto.css">
<!-- 项目css -->
<link rel="stylesheet" href="./css/app.css">
<!-- 外部css块 -->
{% block css %}{% endblock %}
</head>
<body>
<div id="app" class="view-wrap"></div>
<div id="loading" class="loading-wrap">
<div class="loading">
<div class="round"></div>
<div class="txt">0%</div>
</div>
</div>
<div id="freshing" class="fresh-wrap">
<div class="loading">
<div class="round"></div>
<p class="txt">loading...</p>
</div>
</div>
<!-- 内容填充块 -->
{% autoescape false %}
{% block content %}{% endblock %}
{% endautoescape %}
<!-- 页面内加载的尾部js -->
{% include './partial/footer-script.html' %}
<!-- 外部js块 -->
{% block js %}{% endblock %}
</body>
</html>
PC端优先模版
接着就是建立PC端优先的模版layout.html,这个跟单页面模版相似,主要分就是基础样式,同时不要搭移动端的代码,页面模版请看如下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
{% include './partial/meta.html' %}
<link rel="stylesheet" href="./lib/fontello/fontello.css">
<link rel="stylesheet" href="./lib/reboto/roboto.css">
<style>{% include '../lib/base.css' %}</style>
<link rel="stylesheet" href="./css/index.css">
{% block css %}{% endblock %}
</head>
<body>
{% autoescape false %}
{% block content %}{% endblock %}
{% endautoescape %}
{% include './partial/copyright.html' %}
{% block js %}{% endblock %}
</body>
</html>
采用方式
项目模板就了,我因树立单页面项目也例演示使用的步骤
-
率先由github clone模版到文件夹webapp-project,安装相关npm包。
git clone https://github.com/edwardzhong/project-template.git webapp-project npm install
-
于less文件夹内建app.less,并编辑代码
@lightBlue:hsl(198, 73%, 53%); // base html,body{ font-family: "Roboto", "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif; font-size: 0.25rem; color: #555; width: 100%; height: 100%; } body,p,h1,h2,h3,h4,h5,h6{margin:0;} ul,ol{list-type:none; margin:0; padding:0;} // todo more ...
-
每当js文件夹建立app.js并编制代码, 使用es6描写代码就是舒服
require('../lib/zepto.js');// zepto没有使用CommonJs规范,修改后使用require引入 import { webApp } from '../lib/app/app.js' var App=webApp({ container:'app', animate:{ effects:['slideInRight', 'slideOutLeft'],delay:600}, preLoad:function(){ }, aftLoad:function(){ loadPercent(100); } }) .other('/index',{temId:'tempIndex',create:createIndex}) .when('/button',{temId:'tempButton'}) .when('/form',{temId:'tempForm'}) .when('/dialog',{temId:'tempDialog',create:createDialog}) .init(); // todo: more ...
-
每当template文件夹建立app.html,引用单页面模板app-layout.html,并编制内容,我此将单页面的各个视图代码也因代码块的方式引入。
{% extends '../layout/app-layout.html' %} {% block title %}Web App{% endblock %} {% block js %} <script src="./js/app.js"></script> {% endblock %} {% block content %} {% include './partial/index.html' %} {% include './partial/button.html' %} {% include './partial/dialog.html' %} {% include './partial/form.html' %} {% endblock %}
-
运转自动化命令,进行编译,合并,压缩,打包
npm run build
-
最终项目文件都输出及dist文件夹,找到app.html运行即可。
最后
代码请看https://github.com/edwardzhong/project-template.git