/*
单页应用js构建任务
改动根目录下的js，则编译被改动的js
改动非根目录下js,vue，都只编译根目录下的app.js
 */
const gulp = require('gulp')
const browserSync = require('browser-sync').create()
const webpack = require('webpack')
const webpackDllConfig = require('./build/webpack.dll.conf.js')
const express = require('express')
const proxy = require('http-proxy-middleware')
const config = require('./config/index.js')

let dist = './dist/'
let staticDir  = './static/'



gulp.task('webserver', function (done) {
  const app = express();
  if(config.dev.proxyTable && config.dev.proxyTable.length){
    config.dev.proxyTable.forEach(cfg=>{
      if(cfg.context && cfg.context.length){
        app.use(proxy(cfg.context, {
          target: cfg.target,
          changeOrigin: cfg.changeOrigin
        }))
      }else{
        app.use(proxy(cfg))
      }
    })
  }
  app.get('*', express.static(dist))
  app.get('*', express.static(staticDir))
  app.listen(config.dev.port, function () {
    console.log(`Your application is running here: http://localhost:${config.dev.port}`);
  });

})

gulp.task('buildDll', function (done) {
  webpack(webpackDllConfig, function (err, stats) {
    if (err || stats.hasErrors()) {
      console.error(err, stats)
      return
    }
    console.log(stats.toString({
      colors: true,
      chunks: false,
      hash: false,
      version: false
    }))
    gulp.src([staticDir+'/zcms.dll.js']).pipe(gulp.dest(dist))
    done()
  })

})
