gulp-turbo
Version:
前端工作流(requirejs/jade/stylus/coffee),完整强缩进工作流,同步异步模板复用,高效率适合初期前端团队
110 lines (89 loc) • 3.69 kB
text/coffeescript
gulp = require 'gulp'
pkg = global.pkg
util = require 'gulp-util'
fs = require 'fs'
url = require 'url'
path = require 'path'
webserver = require 'gulp-webserver'
chalk = require 'chalk'
through = require 'through2'
forceLivereload = if typeof(pkg.forceLivereload != 'undefined') then !!pkg.forceLivereload else distMode=='dev'
# webserver
gulp.task 'server', ()->
util.log 'approot',pkg.approot
{base,approot,vhost,routerPath,distPath,wwwroot} = pkg
# dist
if pkg.distMode is 'dist'
forceLivereload = false
util.log 'current webroot:',distPath
gulp.src distPath
.pipe webserver
livereload : forceLivereload
host : '0.0.0.0'
path : routerPath
port : pkg.httpPort
proxies : pkg.serverProxies
directoryListing :
enable:true
path:distPath
middleware: (req, res, next)->
urlObj = url.parse(req.url, true)
urlObj.protocol or= 'http'
orginUrl = urlObj.protocol+wwwroot+req.url
util.log 'Received request-->'+orginUrl
#replace to file path
disk_path = url.parse( path.normalize(base+req.url.replace(routerPath, '/'+distPath+'/')) ).pathname
urlObj = url.parse(req.url, true)
method = req.method
# mock
mockfile = approot+'/mock'+urlObj.pathname+'.json'
if fs.existsSync mockfile
res.setHeader('Content-Type', 'application/json')
res.end fs.readFileSync mockfile
return
try
stats = fs.statSync disk_path
# if is a file
if stats.isFile disk_path
res.end fs.readFileSync disk_path
return
#if local not found
try
fs.readdirSync disk_path
next()
return
catch err
proxyURL = vhost+req.url
oProxyURL = url.parse proxyURL, true
oProxyURL.protocol or= 'http'
httpLib = oProxyURL.protocol.replace(/\:/,'')
request = require(httpLib).request
#make proxy url
proxyURL = httpLib+'://'+proxyURL.replace(/^.*\/\/(.*)$/, '$1')
util.log chalk.magenta '访问本地失败,启用透明代理 : '+proxyURL
# vhost
myReq = request proxyURL, (myRes)->
{statusCode,headers} = myRes
res.writeHead(myRes.statusCode, myRes.headers, myRes.host)
myRes.on 'error', (err)->
next(err)
myRes.pipe(res)
myReq.on 'error', (err)->
next err
if !req.readable
myReq.end()
return
else
req.pipe myReq
return
#skip favicon.ico
if req.url.search /favicon\.ico$/ >-1
next()
fallback : ()->
util.log 'fallback', arguments
request vhost+req.url, (error, response, body)->
if (!error && response.statusCode == 200)
next(body)
.pipe through.obj (file, enc, cb)->
util.log chalk.magenta 'running at '+wwwroot
cb()