AngularJs 開發app之準備工作3_gulp

來源:互聯網
上載者:User

標籤:webapp   基於   自動更新   就會   blank   grunt   webp   構建   lib   

寫在前面的話:

  常見的自動構建工具有 grunt、gulp、webpack,把源碼進行合并壓縮,節省頻寬等~

一起來學習 gulp 的使用吧~

三、gulp的使用( gulp 中文網):

  gulp 的優點:基於流、任務化

  常用API: src 、dest 、watch 、task 、pipe

(1)安裝 gulp:

    npm install -g gulp

(2)再在 webapp檔案夾下安裝一些 nodejs 模組:

    npm init

  之後webapp中就會出現一個package.json檔案,以及一個 node_modules 檔案夾

(3)繼續安裝一些依賴外掛程式:

    npm install --save-dev gulp

  一個一個裝太麻煩了,可用大量安裝的方式(之間用空格分開):

    npm install --save-dev gulp-clean gulp-concat gulp-connect gulp-cssmin gulp-imagemin gulp-less gulp-load-plugins gulp-uglify open

  安裝完了之後 package.json檔案的內容如,可以看到已安裝的依賴(10個)的版本資訊:

    

 (4)設定檔夾,首先要對需要的檔案目錄預先做一下設計,參考如下: 

   目錄結構設計:
     build用於調試
     dist用於發布使用
     src:
       data (用於mock資料)
       image
       script:config,controller,directive,filter,service
       app.js
       style:
       view: 各種html檔案
     index.html
     404.html

  編寫任務   

    lib
    html
    json
    css
    js
    image
    clean
    reload
    watch

 開始編寫任務吧,所以按照任務名稱,下面有九步~

 做法:

    1)在webapp檔案下建立一個檔案gulpfile.js,內容如下:

 1 var gulp = require(‘gulp‘); 2 var $ = require(‘gulp-load-plugins‘)(); 3 var open = require(‘open‘); 4  5 var app = { 6     srcPath: ‘src/‘, 7     devPath: ‘build/‘, 8     prdPath: ‘dist/‘ 9 };10 11 gulp.task(‘lib‘, function(){12     gulp.src(‘bower_components/**/*‘)13     .pipe(gulp.dest(app.devPath + ‘vendor‘))14     .pipe(gulp.dest(app.prdPath + ‘vendor‘))15 });

     儲存後,運行如下命令:

      gulp lib

     執行成功後,webapp 檔案夾下出現了 build 、dist 兩個檔案夾,且都包含了 vendor 檔案夾。

      

    2)在webapp檔案下建立一個檔案夾 src, 並在gulpfile.js檔案新增如下代碼:

gulp.task(‘html‘, function(){    gulp.src(app.srcPath + ‘**/*.html‘)    .pipe(gulp.dest(app.devPath))    .pipe(gulp.dest(app.prdPath))});

    寫完了之後可以測試一下:在src中建立一個名為view的檔案夾,在view中建立一個1.html文檔(內容為空白都沒有關係),然後運行

      gulp html

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾中,且都包含了 view 檔案夾,證明配置成功啦~

      

    3)在gulpfile.js檔案新增如下代碼:

gulp.task(‘json‘, function(){    gulp.src(app.srcPath + ‘data/**/*.json‘)    .pipe(gulp.dest(app.devPath + ‘data‘))    .pipe(gulp.dest(app.prdPath + ‘data‘))});

    寫完了之後可以測試一下:在src中建立一個名為data的檔案夾,在data中建立一個1.json文檔(內容為空白都沒有關係),然後運行

      gulp json

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾中,且都包含了 data 檔案夾,證明配置成功啦~

      

   4)在gulpfile.js檔案新增如下代碼:

gulp.task(‘less‘, function(){    gulp.src(app.srcPath + ‘style/index.less‘)    .pipe($.less())    .pipe(gulp.dest(app.devPath + ‘css‘))    .pipe($.cssmin())    .pipe(gulp.dest(app.prdPath + ‘css‘))});

    寫完了之後可以測試一下:在src中建立一個名為 style 的檔案夾,在 style 中建立一個1.less文檔 以及一個 index.less文檔 (內容為空白都沒有關係),然後運行

      gulp less

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾中,且都包含了 css 檔案夾及index.css,證明配置成功啦~

註:此處使用index.less作為一個總的引用,在其中使用@import將其他所有的.less檔案都引入過來。

      

    5)在gulpfile.js檔案新增如下代碼:

gulp.task(‘js‘, function(){    gulp.src(app.srcPath + ‘script/**/*.js‘)    .pipe($.concat(‘index.js‘))    .pipe(gulp.dest(app.devPath + ‘js‘))    .pipe($.uglify())    .pipe(gulp.dest(app.prdPath + ‘js‘))});

    寫完了之後可以測試一下:在src中建立一個名為 script 的檔案夾,在 script 中建立一個1.js文檔 以及一個 2.js文檔 (內容為空白都沒有關係),然後運行

      gulp js

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾中,且都包含了 js 檔案夾及index.js,證明配置成功啦~

註:此處使用index.js作為一個總的引用。

      

    6)在gulpfile.js檔案新增如下代碼:

gulp.task(‘image‘, function(){    gulp.src(app.srcPath + ‘image/**/*‘)    .pipe(gulp.dest(app.devPath + ‘image‘))    .pipe($.imagemin())    .pipe(gulp.dest(app.prdPath + ‘image‘))});

    寫完了之後可以測試一下:在src中建立一個名為 image 的檔案夾,在 image 中加入一個1.png,然後運行

      gulp image

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾中,且都包含了 image 檔案夾及1.png,且圖片被壓縮得更小了,原來是72.6k, build中是72.6k, dist中是63.4k。 證明配置成功啦~

      

    7)在gulpfile.js檔案新增如下代碼:

gulp.task(‘clean‘, function(){    gulp.src([app.devPath, app.prdPath])    .pipe($.clean())});

    寫完了之後運行

       gulp clean

    執行成功後,webapp 檔案夾下的 build 、dist 兩個檔案夾都消失了。 證明配置成功啦~

  至此,9個裡面已經有7個配置完啦!可以透露一下小技巧了,執行一個命令,將所有需要編譯拷貝的檔案一下全搞定~只要在gulpfile.js檔案新增如下代碼:

gulp.task(‘build‘,[‘image‘, ‘js‘, ‘less‘, ‘lib‘, ‘html‘, ‘json‘]);

    運行: 

      gulp build

    然後之前被刪掉的都回來啦~開發時這樣用就比較方便啦,不用運行那麼多次了。同時,還可設定構建時自動開啟瀏覽器,並指定監控連接埠為1234,只要在gulpfile.js檔案新增如下代碼:

gulp.task(‘serve‘,[‘build‘], function(){    $.connect.server({        root: [app.devPath],        livereload: true,        port: 1234    });    open(‘http://localhost:1234‘);    gulp.watch(‘bower_components/**/*‘,[‘lib‘]);    gulp.watch(app.srcPath + ‘**/*.html‘, [‘html‘]);    gulp.watch(app.srcPath + ‘data/**/*.json‘, [‘json‘]);    gulp.watch(app.srcPath + ‘style/**/*.less‘, [‘less‘]);    gulp.watch(app.srcPath + ‘image/**/*‘, [‘image‘]);    gulp.watch(app.srcPath + ‘script/**/*.js‘, [‘js‘]);});

    運行:gulp serve 

當然這樣是不能夠實現重新整理的,還需要進行下一步reload才可。

8)在gulpfile.js檔案新增如下代碼:

  在1~6步 的後面都要加上 reload(),也就是下面的一句話:

    .pipe($.connect.reload());

  執行個體:只列舉一個~

    

這樣的話,每次運行 gulp serve時都能自動更新了,

還有一個方法,可以實現 只輸入 gulp 即可運行,方法如下,在gulpfile.js檔案新增如下代碼:

gulp.task(‘default‘, [‘serve‘]);  //加了這一句之後,直接運行gulp後面不加什麼,就可以直接serve

來試一下:

  gulp

額,瀏覽器自動開啟! but 為啥我的介面是這樣子的?

    

 要點擊 view,出現如下所示介面:

再點擊 1.html才可看見:

在 src/view/1.html 內容改變時,儲存時,瀏覽器會自動重新整理。

那要怎麼解決 一運行 gulp 就可以看到內容?

首先,名字取錯了~應該叫 index.html. 因為到了一個檔案下,預設運行index.html ,叫其他名字的都不會自動運行。

然後,我把代碼中監聽的路徑改了(如加粗部分所示):

open(‘http://localhost:1234/view‘);

運行 gulp ,

出現如所示介面:

 

ok 啦~

至此,本文結束~

 

AngularJs 開發app之準備工作3_gulp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.