上一篇的gulp配置很簡單,主要就是為了demo的檢視和調試,這篇則會相對詳細一些,包括壓縮合併打時間戳等。
在網路環境比較好的城市,需要多人協作的,大一點的專案應該都用上了模組化(這裡主要指commonjs和ES6模組系統,不是再早的require.js和sea.js) 。程式碼也會更注重如何分離和注入,而不再是單純的合併。
但是在很多小公司,開發模式或技術都還是比較傳統的,或者一些小專案也完全不需要用上那些比較前沿的技術。
所以這篇配置就主要為了這樣的中小專案。
套件管理工具:yarn v0.24.5
自動化建置工具:gulp v4.0
##3.開發環境配置yarn add global gulpjs/gulp#4.0
<span style="color: #0000ff">var</span> gulp = require('gulp'<span style="color: #000000">), pug </span>= require('gulp-pug'<span style="color: #000000">), less </span>= require('gulp-less'<span style="color: #000000">), </span><span style="color: #008000">//</span><span style="color: #008000">当发生异常时提示错误 确保本地安装gulp-notify和gulp-plumber</span> notify = require('gulp-notify'<span style="color: #000000">), plumber </span>= require('gulp-plumber'<span style="color: #000000">), sourcemaps </span>= require('gulp-sourcemaps'<span style="color: #000000">), browserSync </span>= require('browser-sync'<span style="color: #000000">).create() reload </span>=<span style="color: #000000"> browserSync.reload; </span><span style="color: #0000ff">var</span> LessAutoprefix = require('less-plugin-autoprefix'<span style="color: #000000">), autoprefix </span>= <span style="color: #0000ff">new</span> LessAutoprefix({ browsers: ['last 2 versions'<span style="color: #000000">] }); </span><span style="color: #008000">//</span><span style="color: #008000"> 文件路径</span> <span style="color: #0000ff">var</span> paths =<span style="color: #000000"> { pug: { src: </span>'src/pug/pages/*.pug'<span style="color: #000000">, dest: </span>'dev/html/'<span style="color: #000000">, watch: </span>'src/pug/**/*.pug'<span style="color: #000000"> }, less: { src: </span>'src/less/**/*.less'<span style="color: #000000">, dest: </span>'dev/css/'<span style="color: #000000">, watch: </span>'src/less/**/*.less'<span style="color: #000000"> }, js: { src: </span>'src/js/**/*.js'<span style="color: #000000">, dest: </span>'dev/js/'<span style="color: #000000">, watch: </span>'src/js/**/*.js'<span style="color: #000000"> }, img: { src: </span>'src/img/**/*'<span style="color: #000000">, dest: </span>'dev/img/'<span style="color: #000000">, watch: </span>'src/img/**/*'<span style="color: #000000"> } } </span><span style="color: #008000">//</span><span style="color: #008000"> 启动 browserSync 服务,自己启动server,并且为浏览器实时刷新提供服务</span> gulp.task('browserSync', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> browserSync.init({ server: { baseDir: </span>'./'<span style="color: #000000"> }, files: </span>'./dev/**/*'<span style="color: #000000"> }); }) </span><span style="color: #008000">//</span><span style="color: #008000"> 将pug文件转换为html</span> gulp.task('pug', <span style="color: #0000ff">function</span><span style="color: #000000"> buildHTML() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.pug.src) .pipe(plumber({errorHandler: notify.onError(</span>'Error: <%= error.message %>'<span style="color: #000000">)})) .pipe(pug()) .pipe(gulp.dest(paths.pug.dest)); }); </span><span style="color: #008000">//</span><span style="color: #008000"> 编译less文件</span> gulp.task('less', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.less.src) .pipe(plumber({errorHandler: notify.onError(</span>'Error: <%= error.message %>'<span style="color: #000000">)})) .pipe(sourcemaps.init()) .pipe(less({ plugins: [autoprefix] })) .pipe(sourcemaps.write()) .pipe(gulp.dest(paths.less.dest)); }) </span><span style="color: #008000">//</span><span style="color: #008000"> 复制js文件</span> gulp.task('js', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.js.src) .pipe(gulp.dest(paths.js.dest)); }) </span><span style="color: #008000">//</span><span style="color: #008000"> 复制img文件</span> gulp.task('img', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.img.src) .pipe(gulp.dest(paths.img.dest)); }) </span><span style="color: #008000">//</span><span style="color: #008000"> 监听文件变化</span> gulp.task('watch', <span style="color: #0000ff">function</span><span style="color: #000000">() { gulp.watch(paths.pug.watch, gulp.parallel(</span>'pug'<span style="color: #000000">)) gulp.watch(paths.less.watch, gulp.parallel(</span>'less'<span style="color: #000000">)) gulp.watch(paths.js.watch, gulp.parallel(</span>'js'<span style="color: #000000">)) gulp.watch(paths.img.watch, gulp.parallel(</span>'img'<span style="color: #000000">)) }) </span><span style="color: #008000">//</span><span style="color: #008000"> 默认任务,在命令行输入`gulp`来启动任务</span> gulp.task('default', gulp.parallel('watch', 'browserSync', 'pug', 'less', 'js'))
gulp-pug這個外掛是用來編譯pug模板的,也就是以前的jade模板,pug模板是一個很強大的前後端通用的模板引擎,而且學習也很簡單,具體用法可以看我另外一篇關於pug的教學文章——基於express+mongodb+pug的部落格系統——pug篇。
#大家知道gulp在監聽任務的過程中,如果某個環節出了錯誤,gulp就會被中斷,然後得重新啟動gulp任務才行,這是一件很麻煩的事。這裡可以透過gulp-notify和gulp-plumber兩個外掛來避免gulp任務中斷。
#4.生產環境配置<span style="color: #0000ff">var</span> gulp = require('gulp'<span style="color: #000000">), del </span>= require('del'<span style="color: #000000">), pug </span>= require('gulp-pug'<span style="color: #000000">), less </span>= require('gulp-less'<span style="color: #000000">), cleanCSS </span>= require('gulp-clean-css'<span style="color: #000000">), base64 </span>= require('gulp-tobase64'<span style="color: #000000">), </span><span style="color: #008000">//</span><span style="color: #008000"> img64 = require('gulp-imgbase64'),</span> imagemin = require('gulp-imagemin'<span style="color: #000000">), babel </span>= require('gulp-babel'<span style="color: #000000">), uglify </span>= require('gulp-uglify'<span style="color: #000000">), rev </span>= require('gulp-rev'), <span style="color: #008000">//</span><span style="color: #008000"> 添加时间戳</span> revCollector = require('gulp-rev-collector'<span style="color: #000000">); </span><span style="color: #0000ff">var</span> LessAutoprefix = require('less-plugin-autoprefix'<span style="color: #000000">), autoprefix </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> LessAutoprefix({ browsers: [</span>'last 2 versions'<span style="color: #000000">] }); </span><span style="color: #008000">//</span><span style="color: #008000"> 文件路径</span> <span style="color: #0000ff">var</span> paths =<span style="color: #000000"> { pug: { src: </span>'src/pug/pages/*.pug'<span style="color: #000000">, dest: </span>'dist/html/'<span style="color: #000000"> }, less: { src: </span>'src/less/main.less'<span style="color: #000000">, dest: </span>'dist/css/'<span style="color: #000000"> }, js: { src: [</span>'src/js/**/*.js', '!src/js/lib/*.js'<span style="color: #000000">], dest: </span>'dist/js/'<span style="color: #000000"> }, img: { src: </span>'src/img/**/*'<span style="color: #000000">, dest: </span>'dist/img/'<span style="color: #000000"> } }; </span><span style="color: #008000">//</span><span style="color: #008000"> 将pug文件转换为html</span> gulp.task('pug', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.pug.src) .pipe(pug()) .pipe(gulp.dest(paths.pug.dest)); }); </span><span style="color: #008000">//</span><span style="color: #008000"> 编译less文件</span> gulp.task('less', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.less.src) .pipe(less({ plugins: [autoprefix] })) .pipe(base64({ maxsize: </span>8<span style="color: #000000"> })) .pipe(cleanCSS({ compatibility: </span>'ie8' <span style="color: #008000">//</span><span style="color: #008000"> 兼容性前缀保留</span> <span style="color: #000000"> })) .pipe(rev()) .pipe(gulp.dest(paths.less.dest)) .pipe(rev.manifest()) .pipe(gulp.dest(</span>'rev/css'<span style="color: #000000">)) }); </span><span style="color: #008000">//</span><span style="color: #008000"> 压缩图片</span> gulp.task('img', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.img.src) .pipe(imagemin({ optimizationLevel: </span>3<span style="color: #000000">, progressive: </span><span style="color: #0000ff">true</span><span style="color: #000000">, interlaced: </span><span style="color: #0000ff">true</span><span style="color: #000000"> })) .pipe(gulp.dest(paths.img.dest)); }); </span><span style="color: #008000">//</span><span style="color: #008000"> 编译JS文件</span> gulp.task('js', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> gulp.src(paths.js.src) .pipe(babel({ presets: [</span>'es2015'<span style="color: #000000">] })) .pipe(uglify()) .pipe(rev()) .pipe(gulp.dest(paths.js.dest)) .pipe(rev.manifest()) .pipe(gulp.dest(</span>'rev/js'<span style="color: #000000">)); }); </span><span style="color: #008000">//</span><span style="color: #008000"> 引用的外部 JS 库,不需要做压缩和打时间戳等处理</span><span style="color: #008000"> //</span><span style="color: #008000"> 所以直接复制就行</span> gulp.task('copyJs', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span> gulp.src('src/js/lib/*.js'<span style="color: #000000">) .pipe(gulp.dest(</span>'dist/js/lib/'<span style="color: #000000">)) }) </span><span style="color: #008000">//</span><span style="color: #008000"> 替换加了MD5时间戳的文件</span> gulp.task('rev', gulp.series(gulp.parallel('img64', 'less', 'js'), <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span> gulp.src(['rev/**/*.json', 'dist/html/*.html'<span style="color: #000000">]) .pipe(revCollector({ replaceReved: </span><span style="color: #0000ff">true</span><span style="color: #000000"> })) .pipe(gulp.dest(paths.pug.dest)); })); </span><span style="color: #008000">//</span><span style="color: #008000"> Clean 任务执行前,先清除之前生成的文件</span> gulp.task('clean', <span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span> del('dist/'<span style="color: #000000">) }); </span><span style="color: #008000">//</span><span style="color: #008000"> 默认任务,在命令行输入`gulp`来启动任务</span> gulp.task('default', gulp.series('clean', gulp.series('img', gulp.parallel('rev', 'copyJs'))))
在生成環境中,程式碼需要壓縮合併,另外在每次程式碼更新發布新版本時,為了用戶用戶端能下載更新程式碼,所以我們還需要給CSS和JS檔案打上時間戳記。
gulp-rev這個外掛程式就專門給文件打MD5戳的,打完MD5戳之後,HTML檔案裡的引用當然也需要更改,如果每個都去手動更改肯定會是一件很麻煩的事,所以我們還需要gulp-rev-collector這個外掛程式來幫我們取代打了MD5戳的檔案。
gulp-imgbase64
,這個外掛程式可以指定html檔案中,哪些img元素轉換為base64,需要更個人化轉換的可以使用這個外掛。XXX——
| — dist## | — html# | — css# | — img# | — js# | — lib
| — dev
# | — html
# | — css
# | — img
# | — js
# | — lib
| — src
| — pug# | — components
| — pages
| — layout.pug## | — less
| — pages
| — main.less
| — reset.less
| — common.less
| — feature.less
| — img# | — js
# | — lib
src資料夾裡是主要的業務程式碼,這裡面是需要長期維護的程式碼。
dev資料夾是開發時gulp產生程式碼的地方。
dist資料夾是產生時gulp產生程式碼的地方。
在這份配置裡,我並沒有像其他很多人那樣,把開發時透過gulp產生的程式碼也放在src資料夾裡,因為那樣會造成很多引用上的麻煩,而且把開發和生產的程式碼環境都分開,能很好地保持src資料夾裡的純淨,不會有任何雜亂程式碼。
所以一些沒經過gulp處理的文件,我會直接把它們複製到dev或是dist資料夾裡對應的位置。
dist資料夾在每次gulp任務產生程式碼前都會被清空,所以我們不用關心dist資料夾裡的內容。
而dev資料夾可能會有很多冗餘文件,但我們也不需要擔心它會對我們造成任何影響,文件刪除或覆蓋都不重要,只需要保證src資料夾裡的文件正確即可。 ### ### ###
以上是gulp實用配置(2)-中小項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!