您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)webpack+gulp如何實(shí)現(xiàn)自動(dòng)構(gòu)建部署的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
正文
本篇包含以下三點(diǎn):
1.區(qū)分開發(fā)環(huán)境和生產(chǎn)環(huán)境
2.集成gulp
3.package.json 配置
前面主要介紹了webpack 的一些基礎(chǔ)用法,該篇做一個(gè)整體介紹,如何集成gulp?如何應(yīng)對多部署環(huán)境?
項(xiàng)目結(jié)構(gòu)說明
. ├── gulpfile.js # gulp任務(wù)配置 ├── mock/ # 假數(shù)據(jù)文件 ├── package.json # 項(xiàng)目配置 ├── README.md # 項(xiàng)目說明 ├── src # 源碼目錄 │ ├── pageA.html # 入口文件a │ ├── pageB.html # 入口文件b │ ├── pageC.html # 入口文件c │ ├── css/ # css資源 │ ├── img/ # 圖片資源 │ ├── js # js&jsx資源 │ │ ├── pageA.js # a頁面入口 │ │ ├── pageB.js # b頁面入口 │ │ ├── pageC.js # c頁面入口 │ │ ├── helpers/ # 業(yè)務(wù)相關(guān)的輔助工具 │ │ ├── lib/ # 沒有存放在npm的第三方庫或者下載存放到本地的基礎(chǔ)庫,如jQuery、Zepto等 │ │ └── utils/ # 業(yè)務(wù)無關(guān)的輔助工具 │ ├── scss/ # scss資源 │ ├── pathmap.json # 手動(dòng)配置某些模塊的路徑,可以加快webpack的編譯速度 ├── webpack.config.allinone.js # webpack配置 ├── webpack.config.js # 正式環(huán)境webpack配置入口 └── webpack-dev.config.js # 開發(fā)環(huán)境webpack配置入口
一:區(qū)分 dev環(huán)境 和 生產(chǎn)環(huán)境
重命名 webpack.config.js => webpack.config.allinone.js
內(nèi)容上,新增options參數(shù),利用options.debug 來控制webpack參數(shù),用來區(qū)分開發(fā)和生產(chǎn)環(huán)境。例如開發(fā)環(huán)境不需要混淆和壓縮js文件,但是開發(fā)環(huán)境需要。通過option.debug來區(qū)分選擇相應(yīng)的loader配置和Plugin:
module.exports = function(options){ options = options || {} var debug = options.debug !==undefined ? options.debug :true; ...... if(debug){ // }else{ // }
新增webpack.config.js webpack-dev.config.js
//webpack.config.js 'use strict'; var webpack_config = require('./webpack.config.allinone') module.exports = webpack_config({debug:false}) //webpack-dev.config.js 'use strict'; var webpack_config = require('./webpack.config.allinone') module.exports = webpack_config({debug:true})
后續(xù)可以根據(jù)gulp 指令 動(dòng)態(tài)選擇 是開發(fā)環(huán)境還是測試環(huán)境,具體見gulpfile.js
//用于gulp傳遞參數(shù) var minimist = require('minimist'); var knownOptions = { string: 'env', default: {env: process.env.NODE_ENV || 'production'} }; var options = minimist(process.argv.slice(2), knownOptions); var webpackConf = require('./webpack.config'); var webpackConfDev = require('./webpack-dev.config'); var _conf = options.env === 'production' ? webpackConf : webpackConfDev; webpack(_conf, function (err, stats) {......
二:集成gulp
利用 gulp 完成以下工作:
代碼檢查
clean操作
run webpack pack
deploy 發(fā)布
安裝gulp
復(fù)制代碼 代碼如下:
$ npm install gulp gulp-clean jshint gulp-jshint jshint-stylish gulp-sftp gulp-util minimist --save-dev
新增gulpfile.js
完整gulpfile.js 如下:
/** * Created by sloong on 2016/6/14. */ 'use strict'; var gulp = require('gulp'); var webpack = require('webpack'); //用于gulp傳遞參數(shù) var minimist = require('minimist'); var gutil = require('gulp-util'); var src = process.cwd() + '/src'; var assets = process.cwd() + '/dist'; var knownOptions = { string: 'env', default: {env: process.env.NODE_ENV || 'production'} }; var options = minimist(process.argv.slice(2), knownOptions); var webpackConf = require('./webpack.config'); var webpackConfDev = require('./webpack-dev.config'); var remoteServer = { host: '192.168.56.129', remotePath: '/data/website/website1', user: 'root', pass: 'password' }; var localServer = { host: '192.168.56.130', remotePath: '/data/website/website1', user: 'root', pass: 'password' } //check code gulp.task('hint', function () { var jshint = require('gulp-jshint') var stylish = require('jshint-stylish') return gulp.src([ '!' + src + '/js/lib/**/*.js', src + '/js/**/*.js' ]) .pipe(jshint()) .pipe(jshint.reporter(stylish)); }) // clean asserts gulp.task('clean', ['hint'], function () { var clean = require('gulp-clean'); return gulp.src(assets, {read: true}).pipe(clean()) }); //run webpack pack gulp.task('pack', ['clean'], function (done) { var _conf = options.env === 'production' ? webpackConf : webpackConfDev; webpack(_conf, function (err, stats) { if (err) throw new gutil.PluginError('webpack', err) gutil.log('[webpack]', stats.toString({colors: true})) done() }); }); //default task gulp.task('default', ['pack']) //deploy assets to remote server gulp.task('deploy', function () { var sftp = require('gulp-sftp'); var _conf = options.env === 'production' ? remoteServer : localServer; return gulp.src(assets + '/**') .pipe(sftp(_conf)) })
三:package.json 配置
scripts 配置 各個(gè)指令
啟動(dòng)webpack調(diào)試server: npm run server
測試環(huán)境打包: npm run build
生產(chǎn)環(huán)境打包: npm run build-online
發(fā)布到測試環(huán)境: npm run deploy
發(fā)布到生產(chǎn)環(huán)境: npm run deploy-online
完整package.json 如下:
{ "name": "webpack-avalon", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "server": "webpack-dev-server --inline", "build": "gulp pack --env debug", "build-online": "gulp pack --env production", "deploy": "gulp deploy --env debug", "deploy-online": "gulp deploy --env production" }, "author": "sloong", "license": "MIT", "devDependencies": { "css-loader": "^0.21.0", "ejs-loader": "^0.3.0", "extract-text-webpack-plugin": "^1.0.1", "file-loader": "^0.8.5", "gulp": "^3.9.1", "gulp-clean": "^0.3.2", "gulp-jshint": "^2.0.1", "gulp-sftp": "^0.1.5", "gulp-util": "^3.0.7", "html-loader": "^0.4.3", "html-webpack-plugin": "^2.19.0", "jshint": "^2.9.2", "jshint-stylish": "^2.2.0", "jsx-loader": "^0.13.2", "minimist": "^1.2.0", "node-sass": "^3.7.0", "sass-loader": "^3.2.0", "style-loader": "^0.13.0", "url-loader": "^0.5.7", "webpack": "^1.13.1", "webpack-dev-server": "^1.14.1" } }
感謝各位的閱讀!關(guān)于“webpack+gulp如何實(shí)現(xiàn)自動(dòng)構(gòu)建部署”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。