Angular.js项目中使用gulp实现自动化构建以及压缩打包详解

gulp介绍

基于流的前端自动化构建工具,利用gulp可以提高前端开发效率,特别是在前后端分离的项目中。使用gulp能完成以下任务:

  • 压缩html、css和js
  • 编译less或sass等
  • 压缩图片
  • 启动本地静态服务器
  • 其他

目标

  • 一键安装项目所有的依赖模块
  • 一键安装项目所有的依赖库
  • 代码检查确保严格语法正确
  • 能将angularjs的html装换成js模块并且压缩到js文件
  • 将所有css文件合并压缩
  • 将所有的js文件合并压缩
  • 动态引入资源文件
  • 拥有开发环境和生产环境两种打包方式

工具

  • npm基于node的包管理器
  • gulp基于node文件流的构建系统
  • bower是Web开发中的一个前端文件包管理器

实现过程

1、一键安装项目所有的依赖模块

创建项目使用命令(项目目录下)

rush:js;"> npm init //生成package.json { "name": "leason","version": "1.0.0","description": "test for angular and gulp and unit testing","main": "gulpfile.js","dependencies": { },"devDependencies": { },"scripts": { "test": "echo \"Error: no test specified\" && exit 1" },"repository": {

},"keywords": [
"leason"
],"author": "leason","license": "ISC","bugs": {
},}

npm安装依赖模块采用命令

rush:bash;"> npm install xxx --save //保存到dependencies(生产) npm install xxx --save-dev //保存到devDependencies(开发)

package.json中保存相应模块,项目重新部署只需要命令

rush:bash;"> npm install //安装package中所有模块

一键安装项目所有的依赖模块使用bower管理器,用法和npm类似

2、语法检查

rush:bash;"> npm install gulp-jshint --save-dev
rush:js;"> //代码语法检查命令--gulp jshint var jshint = require('gulp-jshint'); //代码检查 gulp.task('jshint',function () { return gulp.src(paths.js) .pipe(jshint()) .pipe(jshint.reporter('default')); });

转换html为js模块

rush:bash;"> npm install gulp-angular-templatecache --save-dev
rush:js;"> //合并html模板命令--gulp template var templateCache = require('gulp-angular-templatecache'); gulp.task('template',function () { return gulp.src(['./templates/**/*.html','./templates/*.html']) .pipe(templateCache({module: 'templates'})) .pipe(gulp.dest('./js')) });

3、将所有css文件合并压缩

rush:bash;"> npm install gulp-cssmin --save-dev
rush:js;"> //合并压缩css命令--gulp deployCSS var cssmin = require('gulp-cssmin'); gulp.task('deployCSS',function() { return gulp.src(paths.css) .pipe(cssmin()) .pipe(concat('all.css')) .pipe(gulp.dest('./build')); });

4、将所有js文件合并压缩

rush:bash;"> npm install gulp-uglify --save-dev //压缩 npm install gulp-concat --save-dev //合并 npm install gulp-sourcemapsy --save-dev //处理 JavaScript 时生成 SourceMap npm install gulp-strip-debug --save-dev //去除打印
rush:js;"> //测试生产两种js压缩命令--生产gulp js --prod测试gulp js --dev gulp.task('js',function(type) { console.log(type); if (type == 'dev') { // dev return gulp.src(paths.js) .pipe(concat('all.js')) .pipe(gulp.dest('./build')); } else { // prod return gulp.src(paths.js) .pipe(sourcemaps.init()) .pipe(stripDebug()) .pipe(uglify()) .pipe(concat('all.min.js')) .pipe(sourcemaps.write()) .pipe(gulp.dest('./build')); } });

5、根据现有文件想index中引入

rush:bash;"> npm install gulp-inject --save-dev

index.html中标识写入的位置如:

rush:xhtml;"> <Meta charset="utf-8"> <<a href="https://www.jb51.cc/tag/Meta/" target="_blank" class="keywords">Meta</a> content="text/html; charset=utf-8" http-equiv="Content-Type"/> <!-- bower:css --> <!-- endinject --> <!-- inject:css --> <link rel="stylesheet" href="build/all.css" rel="external nofollow" > <!-- endinject --> <!-- bower:js --> <!-- endinject --> <!-- inject:js --> <script src="build/all.min.js"></script> <!-- endinject --> </head> <body ng-app="starter"> <div ui-view></div> </body> </html></pre> </div> <p>开发环境</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> //dev资源引用命令--gulp devIndex gulp.task('devIndex',['clean','jshint'],function () { // It's not necessary to read the files (will speed up things),we're only after their paths: return gulp.src('./index.html') .pipe(inject(gulp.src(paths.js,{read: false}),{relative: true})) .pipe(inject(gulp.src(paths.css,{relative: true})) // .pipe(inject(gulp.src(bowerFiles(),{name: 'bower',relative: true})) .pipe(gulp.dest('./')); });</pre> </div> <p>生产环境</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> //生产环境资源引用命令--gulp deployIndex gulp.task('deployIndex','jshint','template','js','deployCSS'],we're only after their paths: return gulp.src('./index.html') .pipe(inject(gulp.src(paths.buildjs,{relative: true})) .pipe(inject(gulp.src(paths.buildcss,relative: true})) .pipe(gulp.dest('./')); });</pre> </div> <p><span style="color: #800000"><h3>注意点</h3></p> <p><a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>混淆过会使angular的依赖注入无法识别,所以<a href="https://www.jb51.cc/tag/daima/" target="_blank" class="keywords">代码</a>编写的过程中要使用严格依赖的写法。如</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> angul<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>pp.con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>(['$routeProvider','$stateProvider','$urlRouterProvider',function($routeProvider,$stateProvider,$urlRouterProvider) { $stateProvider .state('sidebar',{ url: '/sidebar',// abstract: true,templateUrl: 'templates/sidebar.html',controller: 'sidebarCtrl' }) $urlRouterProvider.otherwise('/sidebar/tab1'); }]);</pre> </div> <p><h3>总结</h3></p> <p>以上就是这篇<a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a>的全部<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>,希望本文的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>。</p><div class="topcard-tags"><a href="https://m.jb51.cc/tag/angularjsp/" class="tag_link" target="_blank">angularjs</a><a href="https://m.jb51.cc/tag/gulp/" class="tag_link" target="_blank">gulp</a><a href="https://m.jb51.cc/tag/gulpp/" class="tag_link" target="_blank">gulp</a><a href="https://m.jb51.cc/tag/gulpgoujianangularjs/" class="tag_link" target="_blank">gulp构建angularjs</a><a href="https://m.jb51.cc/tag/yasuo/" class="tag_link" target="_blank">压缩</a><a href="https://m.jb51.cc/tag/yasuop/" class="tag_link" target="_blank">压缩</a><a href="https://m.jb51.cc/tag/dabaoangularjs/" class="tag_link" target="_blank">打包angularjs</a></div> </div> </div> </div> <!-- row end--> <div class="clearfix"></div> <!-- row --> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>相关文章</h1></div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/879e7ec08a36083a57dfc8b44214469d.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742335.html" title="自定义web弹窗/层:简易风格的msg与可拖放的dialog,生成博客园文章目录弹窗">自定义web弹窗/层:简易风格的msg与可拖放的dialog,生成博客园文章目录弹窗</a> </div> <div class="summary">前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/84bd76d88488cafdfb2789b5a991fecf.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742334.html" title="nice-validator表单验证插件的简单使用">nice-validator表单验证插件的简单使用</a> </div> <div class="summary">前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/cf659cff914621d918c6072273c30973.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742333.html" title="基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现">基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现</a> </div> <div class="summary">前言 图片上传是web项目常见的需求,我基于之前的博客的代码...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/02f80db4fdf55f3a24674e2854e018f2.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742332.html" title="踹掉后端,前端导出Excel!">踹掉后端,前端导出Excel!</a> </div> <div class="summary">前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/db8dce1453ed09a8230451f2a9fcdc68.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742331.html" title="Web Worker——js的多线程,实现统计博客园总阅读量、总评论量、总推荐量">Web Worker——js的多线程,实现统计博客园总阅读量、总评论量、总推荐量</a> </div> <div class="summary">前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/09-25/18/1cd1c972ce822d1a99b7d1b2c8f9cfcd.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4742330.html" title="select标签 禁止选择但又能通过序列化form表单传值到后台">select标签 禁止选择但又能通过序列化form表单传值到后台</a> </div> <div class="summary">前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...</div> </div></div> </div> <!-- row end--> </div> </div> <footer id="footer"> <div class="container"> <div class="copyright"> Copyright © 2018 编程之家. 当前版本 V7.0.16<br> <span class="ml5">编程之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> </footer> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script src="https://m.jb51.cc/js/count.js"></script> </body> </html>