mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs')
const path = require('path')
 
const glob = require('glob')
const loaderUtils = require('loader-utils')
 
const isWin = /^win/.test(process.platform)
const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)
 
module.exports = function loader(source) {
  const options = loaderUtils.getOptions(this)
  const baseDir = options['base']
  const platformDir = options['platform']
  const extendsDir = options['extends']
 
  const components = []
  const extendsFiles = []
  // extends目录均导出
  glob.sync('**/index.vue', {
    cwd: extendsDir
  }).forEach(file => {
    extendsFiles.push(file)
    components.push(normalizePath(path.join(extendsDir, file)))
  })
 
  //base目录中有,extends无的导出
  glob.sync('**/index.vue', {
    cwd: baseDir
  }).forEach(file => {
    if (!extendsFiles.includes(file)) {
      components.push(normalizePath(path.join(baseDir, file)))
    }
  })
  //platform目录中有,extends无的导出
  glob.sync('**/index.vue', {
    cwd: platformDir
  }).forEach(file => {
    if (!extendsFiles.includes(file)) {
      components.push(normalizePath(path.join(platformDir, file)))
    }
  })
 
  const componentsCode = components.map(component => {
    return `require('${component}').default`
  }).join(',')
  return `
import Vue from 'vue'
import baseMixin from 'uni-mixins/base'
import animation from 'uni-mixins/animation'
[${componentsCode}].forEach(component=>{
    component.mixins = component.mixins ? [].concat(baseMixin, component.mixins) : [baseMixin]
    component.mixins.push(animation)
    component.name = 'VUni' + component.name
    component.isReserved = true
    Vue.component(component.name, component)
})
`
}