// import _Vue from 'vue'
// let Vue = window.Vue
// if (!Vue) {
//   window.Vue = Vue = _Vue
// }
import Vue from 'vue'
// 扫描此文件夹下的所有的`.vue` 文件，并注册
const componentsWatcher = (scanner, _Vue) => {
  // 判断有没有被注册
  // 如果注册过，就直接返回，否则就再注册一遍
  if (componentsWatcher.installed) {
    return
  } else {
    componentsWatcher.installed = true
  }
  let requireComponent = scanner
  console.log(requireComponent.keys(), '注册的组件')
  requireComponent.keys().forEach(fileName => {
    // 拿到第i个
    const reqCom = requireComponent(fileName)
    // 截取路径作为组件名
    const reqComName = fileName.split('/')[2]
    // console.log(reqComName, reqCom.default)
    if (reqComName) {
      if (_Vue) {
        // 组件挂载
        _Vue.component(reqComName, reqCom.default || reqCom)
        return
      }
      // 组件挂载
      Vue.component(reqComName, reqCom.default || reqCom)
    }
  })
}
// 扫描匹配条件
const vueScanner = require.context(
  '../../../components',
  true,
  /\/index\.vue$/
)

// 扫描
componentsWatcher(vueScanner)

let install = function (_Vue) {
  componentsWatcher(vueScanner, _Vue)
}
export default {
  install
}
