ifdef.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @fileoverview 条件编译
  3. */
  4. const through2 = require('through2')
  5. /**
  6. * @description 条件编译
  7. * @param {string} platform 平台
  8. */
  9. module.exports = function (platform) {
  10. return through2.obj(function (file, _, callback) {
  11. if (file.isBuffer()) {
  12. // 文件夹级别的处理
  13. if (file.relative.includes('miniprogram')) {
  14. if (platform !== 'uni-app') {
  15. // 去掉这一层
  16. file.path = file.path.replace(/(.*)[/\\]miniprogram/, '$1')
  17. } else {
  18. // 不用于本平台的文件
  19. callback()
  20. return
  21. }
  22. } else if (file.relative.includes('uni-app')) {
  23. if (platform === 'uni-app') {
  24. file.path = file.path.replace(/(.*)[/\\]uni-app/, '$1')
  25. } else {
  26. callback()
  27. return
  28. }
  29. }
  30. if (platform === 'uni-app') {
  31. // uni-app 平台 vue3 要求使用 es6 模块
  32. let content = file.contents.toString()
  33. if (file.basename === 'prism.min.js') {
  34. content = content.replace('"undefined"!=typeof module&&module.exports&&(module.exports=Prism),', 'export default Prism;')
  35. } else if (file.extname === '.js' && !file.relative.includes('static')) {
  36. content = content.replace(/module.exports\s*=\s*/, 'export default ')
  37. .replace(/const\s+([^\s=]+)\s*=\s*require\(([^)]+)\)/g, 'import $1 from $2')
  38. }
  39. file.contents = Buffer.from(content)
  40. } else {
  41. // 小程序平台进行进一步处理
  42. let content = file.contents.toString()
  43. /**
  44. * 方式1:
  45. * 在注释 #if(n)def xxx 和 #endif 之间的内容会根据是否定义 xxx 决定是否保留
  46. */
  47. const commentReg = /\/\*[\s\S]*?\*\/|\/\/[^\n]*|<!--[\s\S]*?-->/g // 提取所有注释
  48. const copy = content // 拷贝一个副本用于搜索
  49. let match = commentReg.exec(copy)
  50. const stack = []
  51. while (match) {
  52. if (match[0].includes('#if')) {
  53. stack.push([match[0], match.index])
  54. } else if (match[0].includes('#endif')) {
  55. const item = stack.pop()
  56. if (!item) {
  57. throw Error('条件编译错误:存在多余的 #endif,path:' + file.path + ',content: ' + content.substr(match.index, 100))
  58. }
  59. const def = item[0].match(/MP-[A-Z]+/gi) || [] // 取出定义条件
  60. let hit = false
  61. for (let i = 0; i < def.length; i++) {
  62. if (def[i] && platform === def[i].toLowerCase()) {
  63. hit = true // 命中
  64. break
  65. }
  66. }
  67. // 不匹配
  68. if ((item[0].includes('#ifdef') && !hit) || (item[0].includes('#ifndef') && hit)) {
  69. let fill = ''
  70. // 用空格填充
  71. for (let j = item[1] + item[0].length; j < match.index; j++) {
  72. if (content[j] === '\n') {
  73. fill += '\n'
  74. } else {
  75. fill += ' '
  76. }
  77. }
  78. content = content.substr(0, item[1] + item[0].length) + fill + content.substr(match.index)
  79. }
  80. }
  81. match = commentReg.exec(copy)
  82. }
  83. if (stack.length) {
  84. throw Error('条件编译错误:存在未闭合的 #ifdef,path:' + file.path + ',content: ' + content.substr(stack[0][1], 100))
  85. }
  86. /**
  87. * 方式2:
  88. * wxml 中属性前加平台名将仅编译到该平台,如 mp-weixin:attr
  89. */
  90. if (file.extname === '.wxml') {
  91. content = content.replace(/([^:\s]+:[^=\s]+)\s*=\s*"(.*?)"/g, ($, $1, $2) => {
  92. const platforms = $1.split(':')
  93. let name = platforms.pop()
  94. const last = platforms[platforms.length - 1]
  95. if (last && !last.includes('mp')) {
  96. name = platforms.pop() + ':' + name
  97. }
  98. if (!platforms.length) return $
  99. let i
  100. for (i = platforms.length; i--;) {
  101. if (platform === platforms[i].toLowerCase()) break
  102. }
  103. if (i >= 0) return `${name}="${$2}"`
  104. return ''
  105. })
  106. }
  107. file.contents = Buffer.from(content)
  108. }
  109. }
  110. this.push(file)
  111. callback()
  112. })
  113. }