server.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. var gulp = require('gulp'),
  3. config = require('./config'),
  4. path = require('path'),
  5. browserSync = require('browser-sync'),
  6. proxyMiddleware = require('http-proxy-middleware'),
  7. browserSyncSpa = require('browser-sync-spa'),
  8. gulpSequence = require('gulp-sequence');
  9. gulp.task('watch', ['inject', 'vendor'], function () {
  10. //监控index.html,和bower.json文件
  11. gulp.watch([path.join(config.paths.src, '/*.html'), 'bower.json', 'vendor.base.json', 'vendor.json'], ['inject']);
  12. //监控CSS文件
  13. gulp.watch([path.join(config.paths.src, '/app/**/*.scss')], function (event) {
  14. if (event.type === 'changed') {
  15. gulp.start('styles:compass');
  16. } else {
  17. gulp.start('inject');
  18. }
  19. });
  20. //监控插件SCSS文件
  21. gulp.watch([path.join(config.paths.src,'/plug/**/*.scss')], function (event) {
  22. if (event.type === 'changed') {
  23. gulp.start('plug:compass');
  24. }
  25. });
  26. //监控JS文件
  27. gulp.watch([path.join(config.paths.src, '/app/**/*.js')], function (event) {
  28. if (event.type === 'changed') {
  29. gulp.start('jshint');
  30. } else {
  31. gulp.start('inject');
  32. }
  33. });
  34. //监控html文件
  35. gulp.watch([
  36. path.join(config.paths.src, '/app/**/*.html')
  37. ], function (event) {
  38. browserSync.reload(event.path);
  39. });
  40. });
  41. function browserSyncInit(baseDir, open, port) {
  42. var onProxyRes = function (proxyRes, req, res) {
  43. // 重写set-cookie位置
  44. if (proxyRes.headers['set-cookie']) {
  45. proxyRes.headers['set-cookie'][0] = proxyRes.headers['set-cookie'][0].replace('/', '')
  46. }
  47. }
  48. browserSync.use(browserSyncSpa({
  49. selector: '[ng-app]'
  50. }));
  51. browserSync.init({
  52. startPath: '/',
  53. port: port || 3000,
  54. open: open || false,//决定Browsersync启动时自动打开的网址。默认为“本地” false://停止自动打开浏览器
  55. server: {
  56. baseDir: baseDir,
  57. routes: {
  58. "/bower_components": "bower_components"
  59. },
  60. //使用代理 http://localhost:8089 /eolinker_os
  61. middleware: [
  62. proxyMiddleware(['/'], {onProxyRes: onProxyRes, target: 'http://apims.local', changeOrigin: true,secure: false})
  63. ]
  64. }
  65. });
  66. }
  67. exports.browserSyncInit = browserSyncInit;
  68. gulp.task('serve', ['dev-config', 'watch'], function () {
  69. browserSyncInit([path.join(config.paths.tmp, '/serve'), config.paths.src], true);
  70. });
  71. gulp.task('serve:dist', ['build'], function () {
  72. browserSyncInit(config.paths.dist, true);
  73. });