1. 程式人生 > >使用gulp自動優化requireJS專案

使用gulp自動優化requireJS專案

//引入gulp
var gulp = require('gulp');


//引入元件
var concat = require('gulp-concat');           //合併
var jshint = require('gulp-jshint');           //js規範驗證
var uglify = require('gulp-uglify');           //壓縮
var rename = require('gulp-rename');          //檔名命名
var amdOptimize = require("amd-optimize");           //require優化
var watch = require('gulp-watch');




//指令碼檢查
gulp.task('lint', function () {
    gulp.src('./src/js/**/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});


//require合併
gulp.task('rjs', function () {
    gulp.src('./src/js/**/*.js')
        .pipe(amdOptimize("main", {
 //require config
            paths: {
                "jquery": "../../libs/jquery/dist/jquery.min",
                "jquery.serializeJSON": "../../libs/jquery.serializeJSON/jquery.serializejson.min",
                "sug": "src/js/suggestion/suggestion",
                "validate": "../util/src/js/util/validate",
                "urlParam": "../util/src/js/util/url.param"
            },
            shim: {
                "jquery.serializeJSON": ['jquery']
            }
        }))
        .pipe(concat("index.js"))           //合併
        .pipe(gulp.dest("dist/js"))          //輸出儲存
        .pipe(rename("index.min.js"))          //重新命名
        .pipe(uglify())                        //壓縮
        .pipe(gulp.dest("dist/js"));         //輸出儲存
});

gulp.task('default', function () {
    //監聽js變化
    gulp.watch('./src/js/**/*.js', function () {       //當js檔案變化後,自動檢驗 壓縮
        //gulp.run('lint', 'scripts');
        gulp.run('lint', 'rjs');
    });


});