路泰科技体检小程序UI设计新版本
1
wwl
5 天以前 a6cdbcfe28fcc40ebb4919f57d60fb20122e8e57
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * @fileoverview 条件编译
 */
const through2 = require('through2')
 
/**
 * @description 条件编译
 * @param {string} platform 平台
 */
module.exports = function (platform) {
  return through2.obj(function (file, _, callback) {
    if (file.isBuffer()) {
      // 文件夹级别的处理
      if (file.relative.includes('miniprogram')) {
        if (platform !== 'uni-app') {
          // 去掉这一层
          file.path = file.path.replace(/(.*)[/\\]miniprogram/, '$1')
        } else {
          // 不用于本平台的文件
          callback()
          return
        }
      } else if (file.relative.includes('uni-app')) {
        if (platform === 'uni-app') {
          file.path = file.path.replace(/(.*)[/\\]uni-app/, '$1')
        } else {
          callback()
          return
        }
      }
      if (platform === 'uni-app') {
        // uni-app 平台 vue3 要求使用 es6 模块
        let content = file.contents.toString()
        if (file.basename === 'prism.min.js') {
          content = content.replace('"undefined"!=typeof module&&module.exports&&(module.exports=Prism),', 'export default Prism;')
        } else if (file.extname === '.js' && !file.relative.includes('static')) {
          content = content.replace(/module.exports\s*=\s*/, 'export default ')
            .replace(/const\s+([^\s=]+)\s*=\s*require\(([^)]+)\)/g, 'import $1 from $2')
        }
        file.contents = Buffer.from(content)
      } else {
        // 小程序平台进行进一步处理
        let content = file.contents.toString()
        /**
         * 方式1:
         * 在注释 #if(n)def xxx 和 #endif 之间的内容会根据是否定义 xxx 决定是否保留
         */
        const commentReg = /\/\*[\s\S]*?\*\/|\/\/[^\n]*|<!--[\s\S]*?-->/g // 提取所有注释
        const copy = content // 拷贝一个副本用于搜索
        let match = commentReg.exec(copy)
        const stack = []
        while (match) {
          if (match[0].includes('#if')) {
            stack.push([match[0], match.index])
          } else if (match[0].includes('#endif')) {
            const item = stack.pop()
            if (!item) {
              throw Error('条件编译错误:存在多余的 #endif,path:' + file.path + ',content: ' + content.substr(match.index, 100))
            }
            const def = item[0].match(/MP-[A-Z]+/gi) || [] // 取出定义条件
            let hit = false
            for (let i = 0; i < def.length; i++) {
              if (def[i] && platform === def[i].toLowerCase()) {
                hit = true // 命中
                break
              }
            }
            // 不匹配
            if ((item[0].includes('#ifdef') && !hit) || (item[0].includes('#ifndef') && hit)) {
              let fill = ''
              // 用空格填充
              for (let j = item[1] + item[0].length; j < match.index; j++) {
                if (content[j] === '\n') {
                  fill += '\n'
                } else {
                  fill += ' '
                }
              }
              content = content.substr(0, item[1] + item[0].length) + fill + content.substr(match.index)
            }
          }
          match = commentReg.exec(copy)
        }
        if (stack.length) {
          throw Error('条件编译错误:存在未闭合的 #ifdef,path:' + file.path + ',content: ' + content.substr(stack[0][1], 100))
        }
 
        /**
         * 方式2:
         * wxml 中属性前加平台名将仅编译到该平台,如 mp-weixin:attr
         */
        if (file.extname === '.wxml') {
          content = content.replace(/([^:\s]+:[^=\s]+)\s*=\s*"(.*?)"/g, ($, $1, $2) => {
            const platforms = $1.split(':')
            let name = platforms.pop()
            const last = platforms[platforms.length - 1]
            if (last && !last.includes('mp')) {
              name = platforms.pop() + ':' + name
            }
            if (!platforms.length) return $
            let i
            for (i = platforms.length; i--;) {
              if (platform === platforms[i].toLowerCase()) break
            }
            if (i >= 0) return `${name}="${$2}"`
            return ''
          })
        }
        file.contents = Buffer.from(content)
      }
    }
    this.push(file)
    callback()
  })
}