util.js 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
const path = require('path')
const hash = require('hash-sum')
const crypto = require('crypto')

const isWin = /^win/.test(process.platform)

const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14 15 16
let aboutPkg
try {
  aboutPkg = require(path.resolve(__dirname, '../../../../../about/package.json'))
} catch (e) {}

const isInHBuilderX = !!aboutPkg
const isInHBuilderXAlpha = !!(isInHBuilderX && aboutPkg.alpha)

fxy060608's avatar
fxy060608 已提交
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
function removeExt (str, ext) {
  if (ext) {
    const reg = new RegExp(ext.replace(/\./, '\\.') + '$')
    return normalizePath(str.replace(reg, ''))
  }
  return normalizePath(str.replace(/\.\w+$/g, ''))
}

function hashify (filepath) {
  const relativePath = removeExt(path.relative(process.env.UNI_INPUT_DIR, filepath))
  return hash(relativePath)
}

function md5 (str) {
  const hash = crypto.createHash('md5')
  hash.update(str)
  return hash.digest('hex')
}

function cached (fn) {
  const cache = Object.create(null)
  return function cachedFn (str) {
    const hit = cache[str]
    return hit || (cache[str] = fn(str))
  }
}

const camelizeRE = /-(\w)/g

const camelize = cached((str) => {
  return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
fxy060608's avatar
fxy060608 已提交
48
})
fxy060608's avatar
fxy060608 已提交
49 50 51

function capitalize (str) {
  return str.charAt(0).toUpperCase() + str.slice(1)
fxy060608's avatar
fxy060608 已提交
52
}
fxy060608's avatar
fxy060608 已提交
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

const hyphenateRE = /\B([A-Z])/g

const hyphenate = cached((str) => {
  return str.replace(hyphenateRE, '-$1').toLowerCase()
})

const REGEX_PX = /(:|\s|\(|\/)[+-]?\d+(\.\d+)?u?px/g
const REGEX_UPX = /(:|\s|\(|\/)[+-]?\d+(\.\d+)?upx/g

function convertStaticStyle (styleStr) {
  if (typeof styleStr === 'string') {
    let matches = styleStr.match(REGEX_UPX)
    if (matches && matches.length) {
      matches.forEach(function (match) {
        styleStr = styleStr.replace(match, match.substr(0, match.length - 3) + 'rpx')
      })
    }
    // TODO 不应该再支持 px 转 rpx
    if (process.UNI_TRANSFORM_PX) { // 需要转换 px
      matches = styleStr.match(REGEX_PX)
      if (matches && matches.length) {
        matches.forEach(function (match) {
          styleStr = styleStr.replace(match, match.substr(0, match.length - 2) + 'rpx')
        })
      }
    }
  }
  return styleStr
}

function hasModule (name) {
  try {
    return !!require.resolve(name)
  } catch (e) {}
  return false
}

fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97 98 99 100
const NODE_MODULES_REGEX = /(\.\.\/)?node_modules/g

function normalizeNodeModules (str) {
  str = str.replace(NODE_MODULES_REGEX, 'node-modules')
  if (process.env.UNI_PLATFORM === 'mp-alipay') {
    str = str.replace('node-modules/@', 'node-modules/npm-scope-')
  }
  return str
}

fxy060608's avatar
fxy060608 已提交
101
module.exports = {
fxy060608's avatar
fxy060608 已提交
102 103 104
  isInHBuilderX,
  isInHBuilderXAlpha,
  normalizeNodeModules,
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111
  md5,
  hasOwn (obj, key) {
    return hasOwnProperty.call(obj, key)
  },
  hasModule,
  parseStyle (style = {}) {
    Object.keys(style).forEach(name => {
fxy060608's avatar
fxy060608 已提交
112
      if (global.uniPlugin.platforms.includes(name)) {
fxy060608's avatar
fxy060608 已提交
113 114 115 116 117 118 119 120 121 122
        if (name === process.env.UNI_PLATFORM) {
          Object.assign(style, style[name] || {})
        }
        delete style[name]
      }
    })
    return style
  },
  hashify,
  removeExt,
fxy060608's avatar
fxy060608 已提交
123
  camelize,
fxy060608's avatar
fxy060608 已提交
124
  capitalize,
fxy060608's avatar
fxy060608 已提交
125 126 127 128 129 130 131 132
  hyphenate,
  normalizePath,
  convertStaticStyle,
  getComponentName: cached((str) => {
    if (str.indexOf('wx-') === 0) {
      return str.replace('wx-', 'weixin-')
    }
    return str
Q
qiang 已提交
133 134 135 136
  }),
  getTemplatePath () {
    return path.join(__dirname, '../template')
  }
fxy060608's avatar
fxy060608 已提交
137
}