天天看點

詳細建構工具配置檔案、建構工具gulp、包管理工具bower的使用。

詳細建構工具配置檔案、建構工具gulp、包管理工具bower的使用。

本地安裝bower 建議先看我的部落格– angularjs && bower安裝和使用 入門級安裝 直接上手

項目配置檔案

1、在檔案本地初始化 npm init

2、全局安裝gulp 執行指令:cnpm i -g gulp

3、本地安裝gulp 執行指令:cnpm i –save-dev gulp

4、依次本地安裝上圖(配置圖檔)執行指令:cnpm i –save-dev gulp-clean …………

依據項目需求總計9個子產品的安裝

5、子產品安裝完以後,在本地目錄建立gulpfile.js

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var open = require('open');
var app = {
  srcPath: 'src/',
  devPath: 'build/',
  prdPath: 'dist/'
};

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

gulp.task('less', function() {
  gulp.src(app.srcPath + 'less/*.less')
  .pipe($.plumber())
  .pipe($.less())
  .pipe(gulp.dest(app.devPath + 'css'))
  .pipe($.cssmin())
  .pipe(gulp.dest(app.prdPath + 'css'))
  .pipe($.connect.reload());
});

gulp.task('js', function() {
  gulp.src(app.srcPath + 'js/*.js')
  .pipe($.plumber())
  .pipe(gulp.dest(app.devPath + 'js'))
  .pipe($.uglify())
  .pipe(gulp.dest(app.prdPath + 'js'))
  .pipe($.connect.reload());
});

gulp.task('image', function() {
  gulp.src(app.srcPath + 'img/**/*')
  .pipe($.plumber())
  .pipe(gulp.dest(app.devPath + 'img'))
  .pipe($.imagemin())
  .pipe(gulp.dest(app.prdPath + 'img'))
  .pipe($.connect.reload());
});

gulp.task('build', ['image', 'js', 'less', 'html']);

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

gulp.task('serve', ['build'], function() {
  $.connect.server({
    root: [app.devPath],
    livereload: true,
    port: 
  });

  open('http://localhost:3000');

  gulp.watch(app.srcPath + '**/*.html', ['html']);
  gulp.watch(app.srcPath + 'less/*.less', ['less']);
  gulp.watch(app.srcPath + 'js/*.js', ['js']);
  gulp.watch(app.srcPath + 'img/**/*', ['image']);
});

gulp.task('default', ['serve']);
           

6、依據圖檔用gulp建立任務

7、項目介紹:

7.1、src 為項目源代碼 –需要手動建立目錄

7.2、build 為開發模式

7.3、dist 為生産模式

7.4、例如gulp.src(‘bower_components/*/.js’)為擷取項目路徑

7.5、例如 .pipe(gulp.dest(app.devPath + ‘vendor’)) 通過gulp流拷貝到開發模式下的vendor檔案夾下

7.6、.pipe($.connect.reload()) 通過gulp流 監控頁面的變化

8、項目自定義浏覽器熱加載–nb

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

gulp.task('serve', ['build'], function() {
    $.connect.server({
        root: [app.devPath],
        livereload: true,
        port: 
    });

    open('http://localhost:3000');

    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 + 'script/**/*.js', ['js']);
    gulp.watch(app.srcPath + 'image/**/*', ['image']);
});

gulp.task('default', ['serve']);
           

從打包到建立服務再到監聽—-通過預設任務簡化了指令行:gulp serve

直接執行 gulp 即可。

詳細建構工具配置檔案、建構工具gulp、包管理工具bower的使用。