gulp運行及gulpfile.js配置

來源:互聯網
上載者:User

標籤:reserve   tran   git   相容   排除   mangle   無損壓縮   緩衝   logs   

控制台輸入,運行gulp

gulp default

 

gulpfile.js

 

var gulp = require(‘gulp‘), //擷取gulp    htmlmin = require("gulp-htmlmin"), //壓縮頁面javascript、css,去除頁面空格、注釋,刪除多餘屬性等操作    imagemin = require("gulp-imagemin"), //壓縮圖片檔案    cache = require(‘gulp-cache‘), //只壓縮修改的圖片    pngquant = require(‘imagemin-pngquant‘), //深度壓縮圖片    less = require("gulp-less"), //將less檔案編譯成css    sourcemaps = require(‘gulp-sourcemaps‘), //產生sourcemap檔案    cssmin = require("gulp-clean-css"), //壓縮css檔案    uglify = require("gulp-uglify"), //壓縮javascript檔案    concat = require("gulp-concat"), //合并javascript檔案,減少網路請求    rev = require("gulp-rev-append"), //引用添加版本號碼,清除頁面引用緩衝    autoprefixer = require("gulp-autoprefixer"), //自動處理瀏覽器首碼    livereload = require("gulp-livereload"); //監聽檔案發生變化時,瀏覽器自動重新整理頁面//定義testHtmlmin任務gulp.task(‘testHtmlmin‘, function() {    //testHtmlmin參數    var options = {        removeComments: true, //清除HTML注釋        collapseWhitespace: true, //壓縮HTML        collapseBooleanAttributes: true, //省略布爾屬性的值 <input checked="true"/> ==> <input />        removeEmptyAttributes: true, //刪除所有空格作屬性值 <input id="" /> ==> <input />        removeScriptTypeAttributes: true, //刪除<script>的type="text/javascript"        removeStyleLinkTypeAttributes: true, //刪除<style>和<link>的type="text/css"        minifyJS: true, //壓縮頁面JS        minifyCSS: true //壓縮頁面CSS    };    gulp.src(‘*.html‘) //該任務針對的源檔案        .pipe(htmlmin(options)) //該任務調用的模組        .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//定義testImagemin任務gulp.task(‘testImagemin‘, function() {    //testImagemin參數    var options = {        optimizationLevel: 5, //類型:Number  預設:3  取值範圍:0-7(最佳化等級)        progressive: true, //類型:Boolean 預設:false 無損壓縮jpg圖片        interlaced: true, //類型:Boolean 預設:false 隔行掃描gif進行渲染        multipass: true, //類型:Boolean 預設:false 多次最佳化svg直到完全最佳化        use: [pngquant()] //使用pngquant深度壓縮png圖片的imagemin外掛程式    };    gulp.src(‘*.{png,jpg,gif,ico}‘) //該任務針對的源檔案        .pipe(imagemin(options)) //該任務調用的模組        .pipe(cache(imagemin(options))) //只壓縮修改的圖片        .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//定義testLess任務gulp.task(‘testLess‘, function() {    //cssmin參數    var options = {        advanced: false, //類型:Boolean 預設:true [是否開啟進階最佳化(合并選取器等)]        compatibility: ‘ie7‘, //保留ie7及以下相容寫法 類型:String 預設:‘‘or‘*‘ [啟用相容模式; ‘ie7‘:IE7相容模式,‘ie8‘:IE8相容模式,‘*‘:IE9+相容模式]        keepBreaks: true, //類型:Boolean 預設:false [是否保留換行]        keepSpecialComments: ‘*‘ //保留所有特殊首碼 當你用autoprefixer產生的瀏覽器首碼,如果不加這個參數,有可能將會刪除你的部分首碼    };    //編譯src目錄下的所有less檔案    //除了reset.less和test.less(**匹配src/less的0個或多個子檔案夾)    gulp.src([‘src/less/*.less‘, ‘!src/less/**/{reset,test}.less‘]) //該任務針對的源檔案        .pipe(sourcemaps.init()) //該任務調用的模組        .pipe(less())        .pipe(sourcemaps.write())        .pipe(cssmin(options)) //相容IE7及以下需設定compatibility屬性 .pipe(cssmin({compatibility: ‘ie7‘}))        .pipe(livereload());    .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//定義testJs任務gulp.task(‘testJs‘, function() {    //uglify參數    var options = {        mangle: true, //類型:Boolean 預設:true 是否修改變數名        compress: true, //類型:Boolean 預設:true 是否完全壓縮        preserveComments: ‘all‘, //保留所有注釋        mangle: {            except: [‘require‘, ‘exports‘, ‘module‘, ‘$‘]        } //排除混淆關鍵字    };    //多個檔案以數組形式傳入    //gulp.src([‘src/js/index.js‘,‘src/js/detail.js‘]) //該任務針對的源檔案    //壓縮src/js目錄下的所有js檔案    //除了test1.js和test2.js(**匹配src/js的0個或多個子檔案夾)    gulp.src([‘src/js/*.js‘, ‘!src/js/**/{test1,test2}.js‘])        .pipe(uglify(options)) //該任務調用的模組        .pipe(concat(‘bundle.js‘)) //合并後的檔案名稱        .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//定義testRev任務//html模板// <!doctype html>// <html>//   <head>//     <link rel="stylesheet" href="css/[email protected]@hash">//     <script src="js/[email protected]@hash"></script>//     <script src="js/js-two.js"></script>//   </head>//   <body>//     <div>hello, world!</div>//     <img src="img/[email protected]@hash"  />//     <script src="js/[email protected]@hash"></script>//   </body>// </html>gulp.task(‘testRev‘, function() {    gulp.src(‘name.html‘) //該任務針對的源檔案        .pipe(rev()) //該任務調用的模組        .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//定義testAutoFx任務gulp.task(‘testAutoFx‘, function() {    //uglify參數    var options = {        browsers: [‘last 2 versions‘, ‘Android >= 4.0‘], //https://github.com/ai/browserslist#queries        cascade: true, //是否美化屬性值 預設:true 像這樣:-webkit-transform: rotate(45deg);        transform: rotate(45deg);        remove: true //是否去掉不必要的首碼 預設:true    };    gulp.src(‘name.css‘) //該任務針對的源檔案        .pipe(autoprefixer(options)) //該任務調用的模組        .pipe(gulp.dest(‘‘)); //該任務輸出的位置});//在命令列gulp auto啟動此任務gulp.task(‘auto‘, function() {    livereload.listen();    //監聽檔案修改,當檔案修改則執行less任務    gulp.watch(‘views/less/**.less‘, [‘less‘])})//使用gulp.task(‘default‘)定義預設任務//在命令列使用gulp啟動後面任務gulp.task(‘default‘, [‘testHtmlmin‘, ‘testImagemin‘, ‘testLess‘, ‘testJs‘, ‘testRev‘, ‘testAutoFx‘])

 

gulp運行及gulpfile.js配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.