env.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* @flow */
  2. declare var WXEnvironment: any;
  3. // can we use __proto__?
  4. export const hasProto = '__proto__' in {}
  5. // Browser environment sniffing
  6. export const inBrowser = typeof window !== 'undefined'
  7. export const inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform
  8. export const weexPlatform = inWeex && WXEnvironment.platform.toLowerCase()
  9. export const UA = inBrowser && window.navigator.userAgent.toLowerCase()
  10. export const isIE = UA && /msie|trident/.test(UA)
  11. export const isIE9 = UA && UA.indexOf('msie 9.0') > 0
  12. export const isEdge = UA && UA.indexOf('edge/') > 0
  13. export const isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android')
  14. export const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios')
  15. export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
  16. // Firefox has a "watch" function on Object.prototype...
  17. export const nativeWatch = ({}).watch
  18. export let supportsPassive = false
  19. if (inBrowser) {
  20. try {
  21. const opts = {}
  22. Object.defineProperty(opts, 'passive', ({
  23. get () {
  24. /* istanbul ignore next */
  25. supportsPassive = true
  26. }
  27. }: Object)) // https://github.com/facebook/flow/issues/285
  28. window.addEventListener('test-passive', null, opts)
  29. } catch (e) {}
  30. }
  31. // this needs to be lazy-evaled because vue may be required before
  32. // vue-server-renderer can set VUE_ENV
  33. let _isServer
  34. export const isServerRendering = () => {
  35. if (_isServer === undefined) {
  36. /* istanbul ignore if */
  37. if (!inBrowser && typeof global !== 'undefined') {
  38. // detect presence of vue-server-renderer and avoid
  39. // Webpack shimming the process
  40. _isServer = global['process'].env.VUE_ENV === 'server'
  41. } else {
  42. _isServer = false
  43. }
  44. }
  45. return _isServer
  46. }
  47. // detect devtools
  48. export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__
  49. /* istanbul ignore next */
  50. export function isNative (Ctor: any): boolean {
  51. return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
  52. }
  53. export const hasSymbol =
  54. typeof Symbol !== 'undefined' && isNative(Symbol) &&
  55. typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
  56. let _Set
  57. /* istanbul ignore if */ // $flow-disable-line
  58. if (typeof Set !== 'undefined' && isNative(Set)) {
  59. // use native Set when available.
  60. _Set = Set
  61. } else {
  62. // a non-standard Set polyfill that only works with primitive keys.
  63. _Set = class Set implements ISet {
  64. set: Object;
  65. constructor () {
  66. this.set = Object.create(null)
  67. }
  68. has (key: string | number) {
  69. return this.set[key] === true
  70. }
  71. add (key: string | number) {
  72. this.set[key] = true
  73. }
  74. clear () {
  75. this.set = Object.create(null)
  76. }
  77. }
  78. }
  79. interface ISet {
  80. has(key: string | number): boolean;
  81. add(key: string | number): mixed;
  82. clear(): void;
  83. }
  84. export { _Set }
  85. export type { ISet }