Skip to content

Commit

Permalink
feat: 替换 PLATFORM_TYPE 常量
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzejia authored and liuzejia committed Jul 25, 2024
1 parent b842b9f commit 12c4264
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/bom/URL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString, isUndefined } from '@tarojs/shared'
import { isString, isUndefined, PLATFORM_TYPE } from '@tarojs/shared'

import env from '../env'
import { URLSearchParams } from './URLSearchParams'
Expand Down Expand Up @@ -176,7 +176,7 @@ class TaroURL {
export type { TaroURL }

// Note: 小程序端 vite 打包成 commonjs,const URL = xxx 会报错,所以把 URL 改为 TaroURLProvider
export const TaroURLProvider: typeof TaroURL = process.env.TARO_PLATFORM === 'web' ? env.window.URL : TaroURL
export const TaroURLProvider: typeof TaroURL = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.URL : TaroURL

export function parseUrl (url = '') {
const result = {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/bom/URLSearchParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from '@tarojs/shared'
import { isArray, PLATFORM_TYPE } from '@tarojs/shared'

import env from '../env'

Expand Down Expand Up @@ -36,7 +36,7 @@ function encode (str: string) {
return encodeURIComponent(str).replace(findReg, replacer)
}

export const URLSearchParams = process.env.TARO_PLATFORM === 'web' ? env.window.URLSearchParams : class {
export const URLSearchParams = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.URLSearchParams : class {
#dict = Object.create(null)

constructor (query) {
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/bom/document.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PLATFORM_TYPE } from '@tarojs/shared'

import {
APP,
BODY,
Expand Down Expand Up @@ -44,4 +46,4 @@ function createDocument (): TaroDocument {
}

// Note: 小程序端 vite 打包成 commonjs,const document = xxx 会报错,所以把 document 改为 taroDocumentProvider
export const taroDocumentProvider: TaroDocument = process.env.TARO_PLATFORM === 'web' ? env.document : (env.document = createDocument())
export const taroDocumentProvider: TaroDocument = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.document : (env.document = createDocument())
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/bom/getComputedStyle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PLATFORM_TYPE } from '@tarojs/shared'

import env from '../env'

import type { TaroElement } from '../dom/element'
Expand All @@ -6,6 +8,6 @@ import type { Style } from '../dom/style'
export type TGetComputedStyle = typeof window.getComputedStyle | ((el: TaroElement) => Style)

// Note: 小程序端 vite 打包成 commonjs,const getComputedStyle = xxx 会报错,所以把 GetComputedStyle 改为 taroGetComputedStyleProvider
export const taroGetComputedStyleProvider: TGetComputedStyle = process.env.TARO_PLATFORM === 'web' ? env.window.getComputedStyle : function (element: TaroElement): Style {
export const taroGetComputedStyleProvider: TGetComputedStyle = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.getComputedStyle : function (element: TaroElement): Style {
return element.style
}
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/bom/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber, isString } from '@tarojs/shared'
import { isNumber, isString, PLATFORM_TYPE } from '@tarojs/shared'

import { CONTEXT_ACTIONS } from '../constants'
import { Events } from '../emitter/emitter'
Expand Down Expand Up @@ -153,4 +153,4 @@ class TaroHistory extends Events {
}

export type { TaroHistory }
export const History: typeof TaroHistory = process.env.TARO_PLATFORM === 'web' ? env.window.History : TaroHistory
export const History: typeof TaroHistory = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.History : TaroHistory
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/bom/location.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber, isString, warn } from '@tarojs/shared'
import { isNumber, isString, PLATFORM_TYPE, warn } from '@tarojs/shared'

import { CONTEXT_ACTIONS } from '../constants'
import { getCurrentInstance } from '../current'
Expand Down Expand Up @@ -312,7 +312,7 @@ class TaroLocation extends Events {
}

export type { TaroLocation }
export const Location: typeof TaroLocation = process.env.TARO_PLATFORM === 'web' ? env.window.Location : TaroLocation
export const Location: typeof TaroLocation = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.Location : TaroLocation

function generateFullUrl (val = '') {
const origin = INIT_URL
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/bom/navigator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PLATFORM_TYPE } from '@tarojs/shared'

import env from '../env'

const machine = 'Macintosh'
Expand All @@ -6,7 +8,7 @@ const engine = 'AppleWebKit/534.36 (KHTML, like Gecko) NodeJS/v4.1.0 Chrome/76.0

const msg = '(' + machine + '; ' + arch + ') ' + engine

export const nav: typeof window.navigator = process.env.TARO_PLATFORM === 'web' ? env.window.navigator : {
export const nav: typeof window.navigator = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window.navigator : {
appCodeName: 'Mozilla',
appName: 'Netscape',
appVersion: '5.0 ' + msg,
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-runtime/src/bom/raf.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PLATFORM_TYPE } from '@tarojs/shared'

// https://github.com/myrne/performance-now
export let now: () => number

Expand All @@ -18,13 +20,13 @@ let lastTime = 0

// https://gist.github.com/paulirish/1579671
// https://gist.github.com/jalbam/5fe05443270fa6d8136238ec72accbc0
const _raf = process.env.TARO_PLATFORM === 'web' ? requestAnimationFrame : function (callback) {
const _raf = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? requestAnimationFrame : function (callback) {
const _now = now()
const nextTime = Math.max(lastTime + 16, _now) // First time will execute it immediately but barely noticeable and performance is gained.
return setTimeout(function () { callback(lastTime = nextTime) }, nextTime - _now)
}

const _caf = process.env.TARO_PLATFORM === 'web'
const _caf = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB
? cancelAnimationFrame
: function (seed) {
// fix https://github.com/NervJS/taro/issues/7749
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/bom/window.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from '@tarojs/shared'
import { isString, PLATFORM_TYPE } from '@tarojs/shared'

import { CONTEXT_ACTIONS } from '../constants'
import { Events } from '../emitter/emitter'
Expand Down Expand Up @@ -109,6 +109,6 @@ class TaroWindow extends Events {
export type { TaroWindow }

// Note: 小程序端 vite 打包成 commonjs,const window = xxx 会报错,所以把 window 改为 taroWindowProvider,location 和 history 同理
export const taroWindowProvider: TaroWindow = process.env.TARO_PLATFORM === 'web' ? env.window : (env.window = new TaroWindow())
export const taroWindowProvider: TaroWindow = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? env.window : (env.window = new TaroWindow())
export const taroLocationProvider = taroWindowProvider.location
export const taroHistoryProvider = taroWindowProvider.history
23 changes: 12 additions & 11 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import {
EMPTY_OBJ, ensure, EventChannel,
getComponentsAlias, hooks, internalComponents,
isArray, isFunction, isString, isUndefined, Shortcuts
isArray, isFunction, isString, isUndefined, PLATFORM_TYPE,
Shortcuts,
} from '@tarojs/shared'

import { raf } from '../bom/raf'
Expand Down Expand Up @@ -69,7 +70,7 @@ export function stringify (obj?: Record<string, unknown>) {

export function getPath (id: string, options?: Record<string, unknown>): string {
const idx = id.indexOf('?')
if (process.env.TARO_PLATFORM === 'web') {
if (process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB) {
return `${idx > -1 ? id.substring(0, idx) : id}${stringify(options?.stamp ? { stamp: options.stamp } : {})}`
} else {
return `${idx > -1 ? id.substring(0, idx) : id}${stringify(options)}`
Expand Down Expand Up @@ -105,7 +106,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
let prepareMountList: (() => void)[] = []

function setCurrentRouter (page: MpInstance) {
const router = process.env.TARO_PLATFORM === 'web' ? page.$taroPath : page.route || page.__route__ || page.$taroPath
const router = process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? page.$taroPath : page.route || page.__route__ || page.$taroPath
Current.router = {
params: page.$taroParams!,
path: addLeadingSlash(router),
Expand All @@ -132,7 +133,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
// this.$taroPath 是页面唯一标识
const uniqueOptions = Object.assign({}, options, { $taroTimestamp: Date.now() })
const $taroPath = this.$taroPath = getPath(id, uniqueOptions)
if (process.env.TARO_PLATFORM === 'web') {
if (process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB) {
config.path = $taroPath
}
// this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
Expand All @@ -143,7 +144,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
setCurrentRouter(this)

// 初始化当前页面的上下文信息
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
taroWindowProvider.trigger(CONTEXT_ACTIONS.INIT, $taroPath)
}

Expand All @@ -154,7 +155,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
ensure(pageElement !== null, '没有找到页面实例。')
safeExecute($taroPath, ON_LOAD, this.$taroParams)
loadResolver()
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
pageElement.ctx = this
pageElement.performUpdate(true, cb)
} else {
Expand All @@ -171,7 +172,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
[ONUNLOAD] () {
const $taroPath = this.$taroPath
// 销毁当前页面的上下文信息
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
taroWindowProvider.trigger(CONTEXT_ACTIONS.DESTORY, $taroPath)
}
// 触发onUnload生命周期
Expand Down Expand Up @@ -205,7 +206,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
Current.page = this as any
setCurrentRouter(this)
// 恢复上下文信息
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
taroWindowProvider.trigger(CONTEXT_ACTIONS.RECOVER, this.$taroPath)
}
// 触发生命周期
Expand All @@ -216,7 +217,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
},
[ONHIDE] () {
// 缓存当前页面上下文信息
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
taroWindowProvider.trigger(CONTEXT_ACTIONS.RESTORE, this.$taroPath)
}
// 设置 Current 的 page 和 router
Expand All @@ -231,7 +232,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
}
}

if (process.env.TARO_PLATFORM === 'web') {
if (process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB) {
config.getOpenerEventChannel = () => {
return EventChannel.pageChannel
}
Expand Down Expand Up @@ -302,7 +303,7 @@ export function createComponentConfig (component: React.ComponentClass, componen
ensure(componentElement !== null, '没有找到组件实例。')
this.$taroInstances = instances.get(path)
safeExecute(path, ON_LOAD)
if (process.env.TARO_PLATFORM !== 'web') {
if (process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
componentElement.ctx = this
componentElement.performUpdate(true)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-runtime/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EMPTY_OBJ } from '@tarojs/shared'
import { EMPTY_OBJ, PLATFORM_TYPE } from '@tarojs/shared'

import type { TaroDocument } from './dom/document'

Expand All @@ -8,8 +8,8 @@ interface Env {
}

const env: Env = {
window: process.env.TARO_PLATFORM === 'web' ? window : EMPTY_OBJ,
document: process.env.TARO_PLATFORM === 'web' ? document : EMPTY_OBJ
window: process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? window : EMPTY_OBJ,
document: process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB ? document : EMPTY_OBJ
}

export default env
4 changes: 3 additions & 1 deletion packages/taro-runtime/src/next-tick.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PLATFORM_TYPE } from '@tarojs/shared'

import { Current } from './current'
import { TaroRootElement } from './dom/root'
import env from './env'
Expand Down Expand Up @@ -29,7 +31,7 @@ export const nextTick = (cb: TFunc, ctx?: Record<string, any>) => {
function next () {
const pageElement: TaroRootElement | null = env.document.getElementById<TaroRootElement>(path)
if (pageElement?.pendingUpdate) {
if (process.env.TARO_PLATFORM === 'web') {
if (process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB) {
// eslint-disable-next-line dot-notation
pageElement.firstChild?.['componentOnReady']?.().then(() => {
timerFunc()
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-runtime/src/polyfill/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from '@tarojs/shared'
import { isObject, PLATFORM_TYPE } from '@tarojs/shared'

import { handleArrayFindPolyfill, handleArrayIncludesPolyfill } from './array'
import { handleIntersectionObserverPolyfill } from './intersection-observer'
Expand All @@ -21,14 +21,14 @@ function handlePolyfill () {
handleArrayIncludesPolyfill()
}
// Exit early if we're not running in a browser.
if (process.env.TARO_PLATFORM === 'web' && isObject(window)) {
if (process.env.TARO_PLATFORM === PLATFORM_TYPE.WEB && isObject(window)) {
if (process.env.SUPPORT_TARO_POLYFILL === 'enabled' || process.env.SUPPORT_TARO_POLYFILL === 'IntersectionObserver') {
handleIntersectionObserverPolyfill()
}
}
}

if (process.env.SUPPORT_TARO_POLYFILL !== 'disabled' && process.env.TARO_PLATFORM !== 'web') {
if (process.env.SUPPORT_TARO_POLYFILL !== 'disabled' && process.env.TARO_PLATFORM !== PLATFORM_TYPE.WEB) {
handlePolyfill()
}

Expand Down

0 comments on commit 12c4264

Please sign in to comment.