create-basic-renderer.ts 908 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* @flow */
  2. import { createWriteFunction } from './write'
  3. import { createRenderFunction } from './render'
  4. import type { RenderOptions } from './create-renderer'
  5. import type { Component } from 'typescript/component'
  6. export function createBasicRenderer({
  7. modules = [],
  8. directives = {},
  9. isUnaryTag = () => false,
  10. cache,
  11. }: RenderOptions = {}) {
  12. const render = createRenderFunction(modules, directives, isUnaryTag, cache)
  13. return function renderToString(
  14. component: Component,
  15. context: any,
  16. done: any
  17. ): void {
  18. if (typeof context === 'function') {
  19. done = context
  20. context = {}
  21. }
  22. let result = ''
  23. const write = createWriteFunction((text) => {
  24. result += text
  25. return false
  26. }, done)
  27. try {
  28. //@ts-expect-error
  29. render(component, write, context, () => {
  30. done(null, result)
  31. })
  32. } catch (e) {
  33. done(e)
  34. }
  35. }
  36. }