Skip to content

Commit

Permalink
fix(runtime): 修复 H5 编译报错,fix #10124
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Aug 27, 2021
1 parent 477e0dd commit d065654
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
51 changes: 26 additions & 25 deletions packages/taro-runtime/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,37 @@ import processPluginHooks from './plugin-hooks'

const container = new Container()

container.bind<TaroElement>(SERVICE_IDENTIFIER.TaroElement).to(TaroElement).whenTargetNamed(ElementNames.Element)
container.bind<TaroDocument>(SERVICE_IDENTIFIER.TaroElement).to(TaroDocument).inSingletonScope().whenTargetNamed(ElementNames.Document)
container.bind<TaroRootElement>(SERVICE_IDENTIFIER.TaroElement).to(TaroRootElement).whenTargetNamed(ElementNames.RootElement)
container.bind<FormElement>(SERVICE_IDENTIFIER.TaroElement).to(FormElement).whenTargetNamed(ElementNames.FormElement)
container.bind<InstanceNamedFactory>(SERVICE_IDENTIFIER.TaroElementFactory).toFactory<TaroElement>((context: interfaces.Context) => {
return (named: ElementNames) => (nodeName?: string) => {
const el = context.container.getNamed<TaroElement>(SERVICE_IDENTIFIER.TaroElement, named)
if (nodeName) {
el.nodeName = nodeName
if (process.env.TARO_ENV !== 'h5') {
container.bind<TaroElement>(SERVICE_IDENTIFIER.TaroElement).to(TaroElement).whenTargetNamed(ElementNames.Element)
container.bind<TaroDocument>(SERVICE_IDENTIFIER.TaroElement).to(TaroDocument).inSingletonScope().whenTargetNamed(ElementNames.Document)
container.bind<TaroRootElement>(SERVICE_IDENTIFIER.TaroElement).to(TaroRootElement).whenTargetNamed(ElementNames.RootElement)
container.bind<FormElement>(SERVICE_IDENTIFIER.TaroElement).to(FormElement).whenTargetNamed(ElementNames.FormElement)
container.bind<InstanceNamedFactory>(SERVICE_IDENTIFIER.TaroElementFactory).toFactory<TaroElement>((context: interfaces.Context) => {
return (named: ElementNames) => (nodeName?: string) => {
const el = context.container.getNamed<TaroElement>(SERVICE_IDENTIFIER.TaroElement, named)
if (nodeName) {
el.nodeName = nodeName
}
el.tagName = el.nodeName.toUpperCase()
return el
}
el.tagName = el.nodeName.toUpperCase()
return el
}
})
})

container.bind<TaroText>(SERVICE_IDENTIFIER.TaroText).to(TaroText)
container.bind<InstanceFactory<TaroText>>(SERVICE_IDENTIFIER.TaroTextFactory).toFactory<TaroText>((context: interfaces.Context) => {
return (text: string) => {
const textNode = context.container.get<TaroText>(SERVICE_IDENTIFIER.TaroText)
textNode._value = text
return textNode
}
})
container.bind<TaroText>(SERVICE_IDENTIFIER.TaroText).to(TaroText)
container.bind<InstanceFactory<TaroText>>(SERVICE_IDENTIFIER.TaroTextFactory).toFactory<TaroText>((context: interfaces.Context) => {
return (text: string) => {
const textNode = context.container.get<TaroText>(SERVICE_IDENTIFIER.TaroText)
textNode._value = text
return textNode
}
})

container.bind<TaroNodeImpl>(SERVICE_IDENTIFIER.TaroNodeImpl).to(TaroNodeImpl).inSingletonScope()
container.bind<TaroElementImpl>(SERVICE_IDENTIFIER.TaroElementImpl).to(TaroElementImpl).inSingletonScope()
container.bind<Hooks>(SERVICE_IDENTIFIER.Hooks).to(Hooks).inSingletonScope()
container.bind<TaroNodeImpl>(SERVICE_IDENTIFIER.TaroNodeImpl).to(TaroNodeImpl).inSingletonScope()
container.bind<TaroElementImpl>(SERVICE_IDENTIFIER.TaroElementImpl).to(TaroElementImpl).inSingletonScope()
}

container.bind<Hooks>(SERVICE_IDENTIFIER.Hooks).to(Hooks).inSingletonScope()
container.load(DefaultHooksContainer)

processPluginHooks(container)

export default container
11 changes: 8 additions & 3 deletions packages/taro-runtime/src/dom/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ import {
import type { TaroElement } from './element'
import type { InstanceNamedFactory, EventOptions, MpEvent, TaroDocumentInstance, IHooks } from '../interface'

const hooks = container.get<IHooks>(SERVICE_IDENTIFIER.Hooks)
const getElement = container.get<InstanceNamedFactory>(SERVICE_IDENTIFIER.TaroElementFactory)
const document = getElement(ElementNames.Document)() as TaroDocumentInstance
let hooks
let getElement
let document
if (process.env.TARO_ENV !== 'h5') {
hooks = container.get<IHooks>(SERVICE_IDENTIFIER.Hooks)
getElement = container.get<InstanceNamedFactory>(SERVICE_IDENTIFIER.TaroElementFactory)
document = getElement(ElementNames.Document)() as TaroDocumentInstance
}

// Taro 事件对象。以 Web 标准的事件对象为基础,加入小程序事件对象中携带的部分信息,并模拟实现事件冒泡。
export class TaroEvent {
Expand Down

0 comments on commit d065654

Please sign in to comment.