gulp打包主要注重工做流程的構建。css
通常先在package.json中添加須要的gulp插件,經過npm或者yarn安裝,而後在gulpfile.js中寫構建腳本.html
主要用到的插件有:webpack
gulp-usemin: 根據html頁面建立壓縮html,css,js...的構建任務;web
gulp-htmlmin: 壓縮htmlnpm
gulp-clean-css: 壓縮cssjson
gulp-uglity: 壓縮jsgulp
gulp-concat: 合併文件ui
gulp-template-compile: 轉換html模板爲jsspa
gulp-exec: 執行cmd 命令插件
del : 刪除文件(夾)
webpack-stream: 執行webpack打包
實例:
const gulp = require("gulp");const del = require("del");
const exec = require('child_process').exec; const uglify = require("gulp-uglify"); const cleanCSS = require("gulp-clean-css"); const concat = require("gulp-concat"); const template = require("gulp-template-compile"); const base64 = require("gulp-base64"); const webpack = require("webpack-stream"); let outPath = "./dist"; // 壓縮後文件的放置位置 // gulp.task("任務名", 任務體): 建立任務 gulp.task("cleanDir", () => { return del(outPath, { force: true}); }// copy 文件---通常將一些沒必要壓縮的mock數據、圖片等直接拷貝過去gulp.task("copyResource", () => { return gulp.src("源文件路徑") .pipe(gulp.dest("目標路徑"));});