Angular.js項(xiàng)目中使用gulp實(shí)現(xiàn)自動化構(gòu)建以及壓縮打包詳解
gulp介紹
基于流的前端自動化構(gòu)建工具,利用gulp可以提高前端開發(fā)效率,特別是在前后端分離的項(xiàng)目中。使用gulp能完成以下任務(wù):
- 壓縮html、css和js
- 編譯less或sass等
- 壓縮圖片
- 啟動本地靜態(tài)服務(wù)器
- 其他
目標(biāo)
- 一鍵安裝項(xiàng)目所有的依賴模塊
- 一鍵安裝項(xiàng)目所有的依賴庫
- 代碼檢查確保嚴(yán)格語法正確
- 能將angularjs的html裝換成js模塊并且壓縮到j(luò)s文件中
- 將所有css文件合并壓縮
- 將所有的js文件合并壓縮
- 動態(tài)引入資源文件
- 擁有開發(fā)環(huán)境和生產(chǎn)環(huán)境兩種打包方式
工具
- npm基于node的包管理器
- gulp基于node文件流的構(gòu)建系統(tǒng)
- bower是Web開發(fā)中的一個前端文件包管理器
實(shí)現(xiàn)過程
1、一鍵安裝項(xiàng)目所有的依賴模塊
創(chuàng)建項(xiàng)目使用命令(項(xiàng)目目錄下)
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安裝依賴模塊采用命令
npm install xxx --save //保存到dependencies(生產(chǎn)) npm install xxx --save-dev //保存到devDependencies(開發(fā))
package.json中保存相應(yīng)模塊,項(xiàng)目重新部署只需要命令
npm install //安裝package中所有模塊
一鍵安裝項(xiàng)目所有的依賴模塊使用bower管理器,用法和npm類似
2、語法檢查
npm install gulp-jshint --save-dev
//代碼語法檢查命令--gulp jshint
var jshint = require('gulp-jshint'); //代碼檢查
gulp.task('jshint', function () {
return gulp.src(paths.js)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
轉(zhuǎn)換html為js模塊
npm install gulp-angular-templatecache --save-dev
//合并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文件合并壓縮
npm install gulp-cssmin --save-dev
//合并壓縮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文件合并壓縮
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 //去除打印
//測試生產(chǎn)兩種js壓縮命令--生產(chǎn)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、根據(jù)現(xiàn)有文件想index中引入
npm install gulp-inject --save-dev
index.html中標(biāo)識寫入的位置如:
<!doctype html> <html> <head> <meta charset="utf-8"> <title ng-bind="headTitle"></title> <meta 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>
開發(fā)環(huán)境
//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, {read: false}), {relative: true}))
// .pipe(inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower', relative: true}))
.pipe(gulp.dest('./'));
});
生產(chǎn)環(huán)境
//生產(chǎn)環(huán)境資源引用命令--gulp deployIndex
gulp.task('deployIndex', ['clean', 'jshint', 'template', 'js', 'deployCSS'], 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.buildjs, {read: false}), {relative: true}))
.pipe(inject(gulp.src(paths.buildcss, {read: false}), {relative: true}))
// .pipe(inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower', relative: true}))
.pipe(gulp.dest('./'));
});
注意點(diǎn)
代碼混淆過會使angular的依賴注入無法識別,所以代碼編寫的過程中要使用嚴(yán)格依賴的寫法。如
angularApp.config(['$routeProvider','$stateProvider','$urlRouterProvider',function($routeProvider,$stateProvider,$urlRouterProvider) {
$stateProvider
.state('sidebar', {
url: '/sidebar',
// abstract: true,
templateUrl: 'templates/sidebar.html',
controller: 'sidebarCtrl'
})
$urlRouterProvider.otherwise('/sidebar/tab1');
}]);
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- JavaScript 實(shí)現(xiàn)自己的安卓手機(jī)自動化工具腳本(推薦)
- JavaScript 常見安全漏洞和自動化檢測技術(shù)
- 使用auto.js實(shí)現(xiàn)自動化每日打卡功能
- PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動化操作的代碼實(shí)例
- nodejs前端自動化構(gòu)建環(huán)境的搭建
- Angular.Js的自動化測試詳解
- 從零搭建docker+jenkins+node.js自動化部署環(huán)境的方法
- Angular.js自動化測試之protractor詳解
- python接口自動化(十七)--Json 數(shù)據(jù)處理---一次爬坑記(詳解)
- JavaScript揭秘:實(shí)現(xiàn)自動化連連看游戲
相關(guān)文章
Angular中自定義Debounce Click指令防止重復(fù)點(diǎn)擊
本篇文章主要介紹了Angular中自定義Debounce Click指令詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
如何利用AngularJS打造一款簡單Web應(yīng)用
如果大家希望在應(yīng)用程序的創(chuàng)建工作中采取各類最佳實(shí)踐,那么AngularJS也能夠帶來極大的助益。總而言之,這套框架的強(qiáng)大功能與特性永遠(yuǎn)不會讓有著應(yīng)用開發(fā)需求的朋友們失望2015-12-12
Angularjs中如何使用filterFilter函數(shù)過濾
這篇文章主要介紹了Angularjs中如何使用filterFilter函數(shù)過濾的相關(guān)資料,需要的朋友可以參考下2016-02-02
動態(tài)創(chuàng)建Angular組件實(shí)現(xiàn)popup彈窗功能
這篇文章主要介紹了動態(tài)創(chuàng)建angular組件實(shí)現(xiàn)popup彈窗,需要的朋友可以參考下2017-09-09
AngularJS使用ngMessages進(jìn)行表單驗(yàn)證
這篇文章主要介紹了AngularJS使用ngMessages進(jìn)行表單驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2015-12-12
Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
這篇文章主要給大家介紹了Angular2學(xué)習(xí)教程之組件中DOM操作的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。2017-05-05
angular學(xué)習(xí)之動態(tài)創(chuàng)建表單的方法
這篇文章主要介紹了angular學(xué)習(xí)之動態(tài)創(chuàng)建表單的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

