├── .editorconfig ├── README.md ├── bin └── devs ├── certs ├── server.cert └── server.key ├── index.js ├── iproxy.js ├── lib ├── __ds_console.js └── __ds_livereload.js ├── package.json └── utils.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [Makefile] 16 | indent_style = tab 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-devserver [![stars](https://img.shields.io/github/stars/huanz/gulp-devserver.svg?style=flat-square)](https://github.com/huanz/gulp-devserver/stargazers) [![npm package](https://img.shields.io/npm/v/gulp-devserver.svg?style=flat-square)](https://www.npmjs.com/package/gulp-devserver) 2 | 3 | [![npm](https://nodei.co/npm/gulp-devserver.png)](https://www.npmjs.com/package/gulp-devserver) 4 | 5 | 一个本地调试`gulp`插件,具备如下功能: 6 | 7 | - 随时随地启动一个静态文件服务器 8 | - 监听文件变化,自动重新加载 9 | - 代理接口,让 ajax 跨域不再是个事儿 10 | - 假数据数据生成,轻松调试 11 | - console 劫持,轻量级远程调试助手 12 | 13 | # Installation 14 | 15 | 作为`gulp`插件使用: 16 | 17 | ```bash 18 | $ npm install --save-dev gulp-devserver 19 | ``` 20 | 21 | 单独使用: 22 | 23 | ```bash 24 | $ npm install gulp-devserver -g 25 | ``` 26 | 27 | # Usage 28 | 29 | 作为`gulp`插件使用: 30 | 31 | ```javascript 32 | var gulp = require('gulp'); 33 | var server = require('gulp-devserver'); 34 | 35 | gulp.task('devserver', function() { 36 | gulp.src('./app').pipe( 37 | server({ 38 | livereload: { 39 | clientConsole: true 40 | }, 41 | proxy: { 42 | enable: true, 43 | host: 'http://w3cboy.com', 44 | urls: /^\/api\// 45 | } 46 | }) 47 | ); 48 | }); 49 | ``` 50 | 51 | 单独使用: 52 | 53 | ```bash 54 | $ devs --help 55 | 56 | Usage: devs [options] 57 | 58 | Options: 59 | 60 | -h, --help output usage information 61 | -V, --version output the version number 62 | -c, --config The option config.js file path 63 | -d, --dir The option static files dir 64 | -n, --no-browser do not open the localhost server in a browser 65 | -b, --debug open debug (default: false) 66 | -p, --port the port to run on 67 | ``` 68 | 69 | 下面是一个`config.js`配置文件模板: 70 | 71 | ```javascript 72 | module.exports = { 73 | livereload: { 74 | clientConsole: true 75 | }, 76 | proxy: { 77 | enable: true, 78 | host: 'https://noonme.com', 79 | urls: '/api/list' 80 | } 81 | }; 82 | ``` 83 | 84 | # Options 85 | 86 | **port** 87 | 88 | 静态服务器端口。`default`: `3000` 89 | 90 | **defaultFile** 91 | 92 | 启动服务器默认打开的文件,当设置`listdir`为`true`时将不会生效。`default`: `index.html` 93 | 94 | **https** 95 | 96 | 静态服务器是否使用`https`协议。`default`: `false` 97 | 98 | **open** 99 | 100 | 是否启动服务器同时打开浏览器。`default`: `true` 101 | 102 | **debug** 103 | 104 | 在控制台打印日志,当为`true`会答打印每条请求的日志。`default`: `false` 105 | 106 | **livereload.enable** 107 | 108 | 是否开启 livereload 功能,监听文件变化自动重新加载。`default`: `true` 109 | 110 | **livereload.port** 111 | 112 | livereload 所需文件服务器端口。`default`: `35729` 113 | 114 | **livereload.filter** 115 | 116 | 过滤不需要重新加载的文件。`default`: 117 | 118 | ```javascript 119 | // 过滤掉node_modules目录下文件 120 | filter: function(filename) { 121 | return !/\/\.svn\/|\/\.git\/|\/node_modules\//.test(filename); 122 | } 123 | ``` 124 | 125 | **livereload.clientConsole** 126 | 127 | 是否劫持`console`,开启之后将会把每一条`console`信息发送到 server 控制台。当在 webview 远程调试的时候,你可以开启此功能,它会把你的 js 错误信息发送到 server 控制台。如果你想在浏览器控制台使用原生的`console`功能,请使用`__console`。`default`: `false` 128 | 129 | **listdir** 130 | 131 | 启动服务器的时候是否列出当前文件夹文件列表。`default`: `true` 132 | 133 | **proxy.enable** 134 | 135 | 是否开启接口代理功能。`default`: `false` 136 | 137 | **proxy.host** 138 | 139 | 通过`proxy.urls`匹配(只匹配请求 url 里面的 path 部分)到的`url`都会到这个`host`下面去请求。 140 | 141 | **proxy.urls** 142 | 143 | 可以是一个数组,每一项都可以是一个正则对象或者字符串;也可以是一个单独的正则对象或者字符串,用来匹配相关的请求 url,匹配到的 url 都会去`proxy.host`请求数据。eg: 144 | 145 | ```javascript 146 | // server config 147 | proxy: { 148 | enable: true, 149 | host: 'https://noonme.com', 150 | urls: '/api/list' 151 | } 152 | 153 | // client 154 | $.getJSON('/api/list', function (data) { 155 | console.log(data); 156 | }); 157 | ``` 158 | 159 | 那么收到如上 ajax 请求服务器会去`https://noonme.com/api/list`请求数据返回,于是头痛的跨域问题没有了。 160 | 161 | **proxy.mock** 162 | 163 | 开发中经常遇到的问题是接口还没出来,没数据怎么办?你需要写一堆假数据,现在不需要了,我们只需要配置 mock 项。 164 | 165 | ```javascript 166 | proxy: { 167 | enable: true, 168 | mock: { 169 | '/api/list': { 170 | 'list|20': [{ 171 | 'id|+1': 1, 172 | 'name': '@cname', 173 | 'email': '@email', 174 | 'title': '@ctitle', 175 | 'url': '@url', 176 | 'image': '@image(150x150)', 177 | 'date': '@now("T")' 178 | }] 179 | } 180 | } 181 | } 182 | ``` 183 | 184 | 关于 mock 数据模板的详细用法请参考:[Mock.js](http://mockjs.com/examples.html) 185 | 186 | `proxy.mock`的优先级比`proxy.urls`要高,因此匹配到 mock 了就会去走 mock,匹配不到的依然走`proxy.urls`。 187 | -------------------------------------------------------------------------------- /bin/devs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('vinyl-fs'); 4 | var program = require('commander'); 5 | 6 | var server = require('../index'); 7 | 8 | var verstr = [ 9 | '', 10 | ' v' + require('../package.json').version, 11 | ' __', 12 | ' ____/ /__ _ __________ ______ _____ _____', 13 | ' / __ / _ \\ | / / ___/ _ \\/ ___/ | / / _ \\/ ___/', 14 | '/ /_/ / __/ |/ (__ ) __/ / | |/ / __/ /', 15 | '\\__,_/\\___/|___/____/\\___/_/ |___/\\___/_/', 16 | '' 17 | ]; 18 | 19 | program 20 | .version(verstr.join('\n')) 21 | .option('-c, --config [value]', 'The option config json file path') 22 | .option('-d, --dir [value]', 'The option static files dir') 23 | .option('-n, --no-browser', 'Do not open in a Browser') 24 | .option('-b, --debug', 'open debug (default: false)') 25 | .option('-p, --port ', 'The port to run on', parseInt) 26 | .parse(process.argv); 27 | 28 | var opts = {}; 29 | 30 | if (program.config) { 31 | try { 32 | opts = require(program.config); 33 | } catch (e) {} 34 | } else { 35 | if (program.debug) { 36 | opts.debug = true; 37 | } 38 | if (program.noBrowser) { 39 | opts.open = false; 40 | } 41 | if (program.port) { 42 | opts.port = program.port; 43 | } 44 | } 45 | 46 | var src = process.cwd(); 47 | if (program.dir) { 48 | try { 49 | var stats = fs.statSync(program.dir); 50 | if (stats.isDirectory()) { 51 | src = program.dir; 52 | } 53 | } catch (e) {} 54 | } 55 | 56 | fs.src(src).pipe(server(opts)); 57 | -------------------------------------------------------------------------------- /certs/server.cert: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIFtjCCA54CCQC7B795l1j76zANBgkqhkiG9w0BAQUFADCBnDELMAkGA1UEBhMC 3 | VVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28x 4 | FzAVBgNVBAoTDmd1bHAtd2Vic2VydmVyMQwwCgYDVQQLEwNkZXYxEjAQBgNVBAMT 5 | CWxvY2FsaG9zdDElMCMGCSqGSIb3DQEJARYWZ3VscC13ZWJzZXJ2ZXJAZGV2LmNv 6 | bTAeFw0xNDA4MDIxNjAyNTVaFw00MTEyMTcxNjAyNTVaMIGcMQswCQYDVQQGEwJV 7 | UzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZyYW5jaXNjbzEX 8 | MBUGA1UEChMOZ3VscC13ZWJzZXJ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URrFYMjxccPs46XcmjLMnYXE6sUL9bqHBh5u+ufV5cl7ihBRl9t 25 | YTKWHLywNTMC9iPMvzOYYVrpRl75GUp//VHMWxWeY7QMA6r1I3MuoGz3DVJY5/X7 26 | NR2pGTf3zeQJZ4/pHWJEHxRZLGhB1FLB/o66GnYhCpHCKRBdzG47S1FXzeSSf1wq 27 | xAZqx8qv65MZcl42i+NWUHsMRuMUsELl/MmGnRVGshMxRUVgm5IkFIQ7DVBGGHI9 28 | UMWpkJfAIq2xhwgGiy9m5SMSwBqKdT27ObECssUXQomfM4+2py9JGv3wSojwKo49 29 | PyUjw+RfExxHeyck071F1p2dG79Ug3/gqHHSzRCyXs/sZvOm/t/Qjlos7aoGnY8Q 30 | X2k2JjWSD+SPm9qccO2WQVLGUxy4gDqJGz1Z3UyqDu8hJHfdCY5ZlS5GvLfzCItk 31 | +iCqKz/sn9dGSsnwPQ8txENf0UXJemr+9N/WF671NHqF7Rsxi0zmGBjHn1CalAvS 32 | SFGM03YndjuPsJRYj68CR6/8O6yX73ko34Q= 33 | -----END CERTIFICATE----- 34 | -------------------------------------------------------------------------------- /certs/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIJKQIBAAKCAgEAqMlnYTEXjwjD4+cTyZG7VXKPtDg6U01uxrIv2IpjPJST/88O 3 | XMeT6/45hrfNOWcfaoeUWW+jeq5/2kwV2bIZ84TGS70kPDMZqSH+G1hcMDjh0RLD 4 | fGqBym//GOmBJfIMqEboZ8tE8QGrTyPpYCAignCOm2C2KSamNnDSYls6+Z0KmtIA 5 | pBqPcaVSo5xwIhNHUL/FRBSz0gLZ8ZefXBJaY7I+uGpibrbNz7KdxsrrzepbkxQD 6 | 6fIIPKG6PfAKftAd1nzJsSx+qCHLvykOpsZrKgU7T78OqlxiFRKe3Rg1kWii7mTV 7 | 2YytpKY41XLw0CF/kiartziZKxPXQlTnWhjMfHiI1Ii5P9mY8dei3J1P0B1ne2M9 8 | cBW3My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v1JrhqvWbhG7koHwITe 40 | pHNkOQKCAQB+sFYpC0FYMftX10bvIPx9w75rKTEWurxypZuvoIosanUJfoAxkCYT 41 | 1kzKpYk2PfVmFGf2aZdJl9tvM5FbPlIb343E2AvXJP1wjqZFHpfVQK5873bEP6Vu 42 | qFPv+uXRL/dLaG3H5CbSecwQtcMbqqW9DGgMBVnKXHj99n9gDJEeaWji7PcNywib 43 | s4ZdgqlXG9NfJ5VwOAHNPsCFXxLf9DwQFvk7Y/HZ+nI0y49jyevR9d4WUQVLvxSs 44 | rn7GysMw5b7VKDEY+G1GNFUMqzueUuuRZCs/iDNmfshJCykv96pB8nvQpuYSO13V 45 | wP2ApvAH0bUkJ6Ezxr63EiYEAV1Ykl1nAoIBAQDQUeo2APE27luCeoOMLqNDhqNg 46 | irjr5OyO6RcXoM63KcCbiMDtFN8y96h+GjDU2fPrpZunmrKcApOjfj7oCIWwDeca 47 | c/YOkyOrULG8RI45G3aA+AWvx1LTDXIq436MmA7vuMkEFNSf0GfUgiejAT4/KSrU 48 | +INaVP9+0JkZx80hnVehuRgA2a4LZuK1Do4G2KamVNzgzEN8U0h7MBRdkMVCZuBI 49 | 2gCyVB5VB4vPNdeMvLdBVo5kra/y+FLdfF6g4wlob2/sCA9EkkN78hXT0tZE4OF0 50 | 0q9PZMk0YEsZ64KurFDJXafZuTZxP8wzDsa4IXlANnO2S7pGP+gJhDE2Fyfl 51 | -----END RSA PRIVATE KEY----- 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var os = require('os'); 4 | var http = require('http'); 5 | var https = require('https'); 6 | var fs = require('fs'); 7 | var path = require('path'); 8 | var url = require('url'); 9 | var exec = require('child_process').exec; 10 | var spawn = require('child_process').spawn; 11 | var socket = require('socket.io'); 12 | var watch = require('node-watch'); 13 | var connect = require('connect'); 14 | var bodyParser = require('body-parser'); 15 | var multipart = require('connect-multiparty'); 16 | var inject = require('connect-inject'); 17 | var serveIndex = require('serve-index'); 18 | var serveStatic = require('serve-static'); 19 | var through = require('through2'); 20 | 21 | var utils = require('./utils'); 22 | var iProxy = require('./iproxy'); 23 | 24 | var openURL = function(url) { 25 | switch (process.platform) { 26 | case 'darwin': 27 | exec('open ' + url); 28 | break; 29 | case 'win32': 30 | exec('start ' + url); 31 | break; 32 | default: 33 | spawn('xdg-open', [url]); 34 | } 35 | }; 36 | 37 | var getIPAddress = function() { 38 | var ifaces = os.networkInterfaces(); 39 | var ip = ''; 40 | for (var dev in ifaces) { 41 | ifaces[dev].forEach(function(details) { 42 | if (ip === '' && details.family === 'IPv4' && !details.internal) { 43 | ip = details.address; 44 | return; 45 | } 46 | }); 47 | } 48 | return ip || '127.0.0.1'; 49 | }; 50 | 51 | var BROWSER_SCIPTS_DIR = path.join(__dirname, 'lib'); 52 | var defaults = { 53 | port: 3000, 54 | defaultFile: 'index.html', 55 | https: false, 56 | open: true, 57 | debug: false, 58 | livereload: { 59 | enable: true, 60 | port: 35729, 61 | filter: function(filename) { 62 | return !/\/\.svn\/|\/\.git\/|\/node_modules\//.test(filename); 63 | }, 64 | delay: 1000, 65 | clientConsole: false 66 | }, 67 | listdir: true, 68 | proxy: { 69 | enable: false, 70 | host: '', 71 | urls: '', 72 | mock: {} 73 | } 74 | }; 75 | 76 | module.exports = function(options) { 77 | var config = utils.extend({}, defaults, options); 78 | if (!config.host) { 79 | config.host = getIPAddress(); 80 | } 81 | 82 | // 自动打开浏览器 83 | var openInBrowser = function() { 84 | if (!config.open) { 85 | return; 86 | } 87 | openURL('http' + (config.https ? 's' : '') + '://' + config.host + ':' + config.port); 88 | openInBrowser = null; 89 | }; 90 | 91 | var app = connect(); 92 | 93 | app.use(bodyParser.urlencoded({ extended: false })); 94 | app.use(bodyParser.json()); 95 | app.use(multipart()); 96 | 97 | app.use(function(req, res, next) { 98 | res.setHeader('Access-Control-Allow-Origin', '*'); 99 | res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); 100 | next(); 101 | }); 102 | 103 | // http server 104 | var webserver = null; 105 | if (config.https) { 106 | var options = { 107 | key: fs.readFileSync(config.https.key || __dirname + '/certs/server.key'), 108 | cert: fs.readFileSync(config.https.cert || __dirname + '/certs/server.cert') 109 | }; 110 | webserver = https.createServer(options, app); 111 | } else { 112 | webserver = http.createServer(app); 113 | } 114 | 115 | // livereload服务相关 116 | if (config.livereload.enable) { 117 | // 插入livereload相关script至body 118 | var injectRules = []; 119 | var preLoad = config.livereload.clientConsole ? '' : ' async defer'; 120 | var ioServerOrigin = 'http://' + config.host + ':' + config.livereload.port; 121 | var snippet = ''; 122 | snippet += ''; 123 | if (config.livereload.clientConsole) { 124 | snippet += ''; 125 | injectRules.push({ 126 | snippet: ' crossorigin="anonymous"', 127 | match: /]*>/i, 141 | fn: function(m, s) { 142 | return m + s; 143 | } 144 | }); 145 | app.use( 146 | inject({ 147 | runAll: true, 148 | rules: injectRules 149 | }) 150 | ); 151 | // 创建script服务器 152 | var ioApp = connect(); 153 | ioApp.use( 154 | serveStatic(BROWSER_SCIPTS_DIR, { 155 | index: false 156 | }) 157 | ); 158 | var ioServer = (config.livereload.ioServer = http.createServer(ioApp).listen(config.livereload.port)); 159 | // 启动socket服务 160 | var io = (config.livereload.io = socket(ioServer)); 161 | io.on('connection', function(socket) { 162 | utils.log('Livereload client connected'); 163 | if (config.livereload.clientConsole) { 164 | socket.on('console:log', function(args) { 165 | args.unshift(utils.colors.green('log')); 166 | utils.log.apply(null, args); 167 | }); 168 | socket.on('console:warn', function(args) { 169 | args.unshift(utils.colors.yellow('warn')); 170 | utils.log.apply(null, args); 171 | }); 172 | socket.on('console:info', function(args) { 173 | args.unshift(utils.colors.cyan('info')); 174 | utils.log.apply(null, args); 175 | }); 176 | socket.on('console:error', function(args) { 177 | args.unshift(utils.colors.red('err')); 178 | utils.log.apply(null, args); 179 | }); 180 | } 181 | }); 182 | } 183 | // gulp入口 184 | var stream = through.obj(function(file, enc, callback) { 185 | config.path = file.path; 186 | if (config.debug) { 187 | app.use(function(req, res, next) { 188 | utils.log(req.method + ' ' + req.url); 189 | next(); 190 | }); 191 | } 192 | // 代理 193 | if (config.proxy.enable) { 194 | app.use(iProxy(config.proxy)); 195 | } 196 | 197 | // 静态文件服务器 198 | app.use( 199 | serveStatic(config.path, { 200 | index: config.listdir ? false : config.defaultFile, 201 | lastModified: false 202 | }) 203 | ); 204 | 205 | // 列出目录文件列表 206 | if (config.listdir) { 207 | app.use( 208 | serveIndex(path.resolve(config.path), { 209 | icons: true 210 | }) 211 | ); 212 | } 213 | 214 | // 监听文件变化,reload 215 | var changedQueue = {}; 216 | var boardcastChange; 217 | if (config.livereload.enable) { 218 | boardcastChange = function(filename) { 219 | utils.log('change: ' + utils.colors.yellow(path.relative(config.path, filename))); 220 | config.livereload.io.emit('file:change', { 221 | path: filename, 222 | name: path.basename(filename), 223 | ext: path.extname(filename).replace(/^\./, '') 224 | }); 225 | changedQueue[filename].stamp = Date.now(); 226 | }; 227 | watch(config.path, { recursive: true }, function(e, filename) { 228 | if (config.livereload.filter(filename)) { 229 | if (!changedQueue[filename]) { 230 | changedQueue[filename] = {}; 231 | boardcastChange(filename); 232 | } else if (Date.now() - changedQueue[filename].stamp > config.livereload.delay) { 233 | boardcastChange(filename); 234 | } else { 235 | if (!changedQueue[filename].timer) { 236 | setTimeout(function() { 237 | boardcastChange(filename); 238 | }, Date.now() - changedQueue[filename].stamp); 239 | changedQueue[filename].timer = 1; 240 | } 241 | } 242 | } 243 | }); 244 | } 245 | 246 | this.push(file); 247 | 248 | callback(); 249 | }); 250 | 251 | var startServer = function() { 252 | webserver.listen(config.port, openInBrowser); 253 | utils.log('Webserver started at', utils.colors.cyan('http' + (config.https ? 's' : '') + '://' + config.host + ':' + config.port)); 254 | }; 255 | 256 | var stopServer = function(err) { 257 | webserver.close(); 258 | if (config.livereload.enable) { 259 | config.livereload.ioServer.close(); 260 | } 261 | utils.log(err); 262 | process.exit(-1); 263 | }; 264 | 265 | stream.on('data', startServer); 266 | stream.on('kill', stopServer); 267 | // 意外错误关闭服务器 268 | process.on('uncaughtException', stopServer); 269 | 270 | return stream; 271 | }; 272 | -------------------------------------------------------------------------------- /iproxy.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var url = require('url'); 4 | var request = require('request'); 5 | var Mock = require('mockjs'); 6 | 7 | var utils = require('./utils'); 8 | 9 | function iProxy(opt) { 10 | // 是否需要代理此接口 11 | var proxyUrls = utils.isArray(opt.urls) ? opt.urls : [opt.urls]; 12 | var isProxy = function(u) { 13 | return proxyUrls.some(function(proxyUrl) { 14 | return utils.isRegExp(proxyUrl) ? proxyUrl.test(u) : proxyUrl === u; 15 | }); 16 | }; 17 | return function(req, res, next) { 18 | var parsed = url.parse(req.url); 19 | var m = opt.mock[parsed.pathname]; 20 | var opts = null; 21 | if (m) { 22 | res.end(Mock.mock(m)); 23 | } else if (opt.host && isProxy(parsed.pathname)) { 24 | req.headers['Host'] = opt.host.replace(/^https?:\/\//, ''); 25 | opts = { 26 | method: req.method, 27 | url: opt.host + parsed.path, 28 | headers: req.headers 29 | }; 30 | 31 | if (req.headers['content-type'] && req.headers['content-type'].indexOf('multipart/form-data') === -1) { 32 | if (req.body && Object.keys(req.body).length) { 33 | opts.form = req.body; 34 | } 35 | request(opts).pipe(res); 36 | } else { 37 | var requestBody = ''; 38 | req 39 | .on('data', function(chunk) { 40 | requestBody += chunk; 41 | }) 42 | .on('end', function() { 43 | opts.body = requestBody; 44 | request(opts).pipe(res); 45 | }); 46 | } 47 | } else { 48 | next(); 49 | } 50 | }; 51 | } 52 | 53 | module.exports = iProxy; 54 | -------------------------------------------------------------------------------- /lib/__ds_console.js: -------------------------------------------------------------------------------- 1 | (function(window) { 2 | var socket = window.__ds__ && window.__ds__.socket; 3 | // 保存原始的console 4 | var __console = window.console; 5 | var console = window.console; 6 | var methods = ['info', 'log', 'error', 'warn']; 7 | var slice = Array.prototype.slice; 8 | var i = 0; 9 | if (!socket) { 10 | return; 11 | } 12 | for (; i < methods.length; i++) { 13 | console[methods[i]] = (function(func, method) { 14 | return function() { 15 | var args = slice.call(arguments); 16 | try { 17 | socket.emit('console:' + method, args); 18 | } catch (e) { 19 | console.error(e); 20 | } 21 | func.apply(console, args); 22 | }; 23 | })(console[methods[i]], methods[i]); 24 | } 25 | // 捕获错误信息 26 | window.onerror = function(message, filename, lineno, colno, error) { 27 | console.error(message, filename + ':' + lineno); 28 | }; 29 | window.console = console; 30 | window.__console = __console; 31 | })(window); 32 | -------------------------------------------------------------------------------- /lib/__ds_livereload.js: -------------------------------------------------------------------------------- 1 | (function(window) { 2 | 'use strict'; 3 | var options = { 4 | tagNames: { 5 | css: 'link', 6 | js: 'script' 7 | }, 8 | attrs: { 9 | link: 'href', 10 | script: 'src' 11 | } 12 | }; 13 | var __ds__ = (window.__ds__ = { 14 | doc: window.document, 15 | init: function() { 16 | var _this = this; 17 | var socket = (this.socket = window.io.connect(this.getServerUrl())); 18 | socket.on('connect', function() { 19 | console.log('successfully connected'); 20 | }); 21 | socket.on('connect_error', function(err) { 22 | console.log('failed to connect: ' + err); 23 | }); 24 | socket.on('file:change', function(file) { 25 | try { 26 | _this.reload(file); 27 | } catch (err) { 28 | console.error(err); 29 | } 30 | }); 31 | }, 32 | getServerUrl: function() { 33 | var url = this.doc.getElementById('__ds_socket__').src; 34 | var parser = this.parseUrl(url); 35 | return parser.protocol + '//' + parser.host; 36 | }, 37 | reload: function(file) { 38 | if (!file) { 39 | return; 40 | } 41 | var tagName = this.getTagName(file.ext); 42 | if (tagName) { 43 | var loader = file.ext === 'js' ? this.reloadJs : this.reloadCss; 44 | var els = this.doc.getElementsByTagName(tagName); 45 | var len = els.length; 46 | var attr = this.getAttr(tagName); 47 | for (var i = 0; i < len; i++) { 48 | if (els[i][attr].indexOf(file.name) !== -1) { 49 | loader.call(this, els[i], attr); 50 | } 51 | } 52 | } else { 53 | this.reloadBrowser(); 54 | } 55 | }, 56 | reloadCss: function(el, attr) { 57 | var _this = this; 58 | el[attr] = el[attr]; 59 | setTimeout(function() { 60 | if (_this.hackEl) { 61 | _this.hackEl.style.display = 'none'; 62 | _this.hackEl.style.display = 'block'; 63 | } else { 64 | _this.hackEl = _this.doc.createElement('DIV'); 65 | _this.doc.body.appendChild(_this.hackEl); 66 | } 67 | }, 200); 68 | }, 69 | reloadJs: function(el, attr) { 70 | var _this = this; 71 | var url = el[attr]; 72 | var script = this.doc.createElement('script'); 73 | script.setAttribute('crossorigin', 'anonymous'); 74 | script.onload = function() { 75 | script.onload = null; 76 | _this.doc.head.removeChild(script); 77 | script = null; 78 | }; 79 | script.defer = true; 80 | script.async = true; 81 | script.src = url; 82 | this.doc.head.appendChild(script); 83 | }, 84 | reloadBrowser: function() { 85 | window.location.reload(true); 86 | }, 87 | getTagName: function(ext) { 88 | return options.tagNames[ext]; 89 | }, 90 | getAttr: function(tagName) { 91 | return options.attrs[tagName]; 92 | }, 93 | parseUrl: function(url) { 94 | var parser = this.doc.createElement('a'); 95 | parser.href = url; 96 | return parser; 97 | } 98 | }); 99 | __ds__.init(); 100 | })(window); 101 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-devserver", 3 | "description": "Gulp plugin to run a local webserver with LiveReload and debug for ajax cross domain and data mock", 4 | "version": "0.6.4", 5 | "main": "index.js", 6 | "bin": { 7 | "devs": "./bin/devs" 8 | }, 9 | "scripts": { 10 | "start": "node index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/huanz/gulp-devserver.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/huanz/gulp-devserver/issues" 18 | }, 19 | "keywords": [ 20 | "gulpplugin", 21 | "webserver", 22 | "Static", 23 | "server", 24 | "livereload", 25 | "debug", 26 | "ajax", 27 | "mock" 28 | ], 29 | "author": { 30 | "name": "bukas (https://github.com/huanz)" 31 | }, 32 | "engines": { 33 | "node": ">= 0.10.0" 34 | }, 35 | "homepage": "https://github.com/huanz/gulp-devserver", 36 | "license": "MIT", 37 | "dependencies": { 38 | "body-parser": "^1.19.0", 39 | "chalk": "^2.4.2", 40 | "commander": "^3.0.2", 41 | "connect": "^3.7.0", 42 | "connect-inject": "^0.4.0", 43 | "connect-multiparty": "^2.2.0", 44 | "mockjs": "^1.0.1-beta3", 45 | "node-watch": "^0.6.3", 46 | "request": "^2.88.0", 47 | "serve-index": "^1.9.1", 48 | "serve-static": "^1.14.1", 49 | "socket.io": "^2.3.0", 50 | "through2": "^3.0.1", 51 | "vinyl-fs": "^3.0.3" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chalk = require('chalk'); 4 | var toString = Object.prototype.toString; 5 | var isType = function(type) { 6 | return function(obj) { 7 | return toString.call(obj) === '[object ' + type + ']'; 8 | }; 9 | }; 10 | 11 | var isObject = isType('Object'); 12 | exports.isRegExp = isType('RegExp'); 13 | 14 | exports.isArray = function(value) { 15 | return Array.isArray(value); 16 | }; 17 | 18 | function extend() { 19 | var i = 1; 20 | var target = arguments[0] || {}; 21 | var len = arguments.length; 22 | var obj, keys, j; 23 | for (; i < len; i++) { 24 | obj = arguments[i]; 25 | if (isObject(obj)) { 26 | keys = Object.keys(obj); 27 | j = keys.length; 28 | while (j--) { 29 | target[keys[j]] = isObject(obj[keys[j]]) ? extend(target[keys[j]], obj[keys[j]]) : obj[keys[j]]; 30 | } 31 | } 32 | } 33 | return target; 34 | } 35 | 36 | exports.extend = extend; 37 | 38 | var logPrefix = function() { 39 | var now = new Date(); 40 | var str = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds(); 41 | return '[' + chalk.cyan(str) + ']'; 42 | }; 43 | 44 | exports.log = function() { 45 | process.stdout.write(logPrefix() + ' '); 46 | console.log.apply(console, arguments); 47 | return this; 48 | }; 49 | 50 | exports.colors = chalk; 51 | --------------------------------------------------------------------------------