test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import Vue, { VNode, VNodeDirective } from '../../../types/index'
  2. import VueSSRClientPlugin from '../client-plugin'
  3. import VueSSRServerPlugin from '../server-plugin'
  4. import webpack from 'webpack'
  5. import { readFileSync } from 'fs'
  6. import { createRenderer, createBundleRenderer } from '.'
  7. function createApp(context: any) {
  8. return new Vue({
  9. data: {
  10. url: context.url
  11. },
  12. template: `<div>The visited URL is: {{ url }}</div>`
  13. })
  14. }
  15. // Renderer test
  16. const app = createApp({ url: 'http://localhost:8000/' })
  17. const renderer = createRenderer({
  18. template: readFileSync('./index.template.html', 'utf-8')
  19. })
  20. const context = {
  21. title: 'Hello',
  22. meta: `
  23. <meta name="description" content="Vue.js SSR Example">
  24. `
  25. }
  26. renderer.renderToString(app, (err, html) => {
  27. if (err) throw err
  28. const res: string = html
  29. })
  30. renderer.renderToString(app, context, (err, html) => {
  31. if (err) throw err
  32. const res: string = html
  33. })
  34. renderer
  35. .renderToString(app)
  36. .then(html => {
  37. const res: string = html
  38. })
  39. .catch(err => {
  40. throw err
  41. })
  42. renderer
  43. .renderToString(app, context)
  44. .then(html => {
  45. const res: string = html
  46. })
  47. .catch(err => {
  48. throw err
  49. })
  50. renderer.renderToStream(app, context).on('data', chunk => {
  51. const html = chunk.toString()
  52. })
  53. // Bundle renderer test
  54. declare const cacheClient: { [key: string]: string }
  55. const bundleRenderer = createBundleRenderer(
  56. '/path/to/vue-ssr-server-bundle.json',
  57. {
  58. inject: false,
  59. runInNewContext: 'once',
  60. basedir: '/path/to/base',
  61. shouldPreload: (file, type) => {
  62. if (type === 'script' || type === 'style') {
  63. return true
  64. }
  65. if (type === 'font') {
  66. return /\.woff2$/.test(file)
  67. }
  68. if (type === 'image') {
  69. return file === 'hero.jpg'
  70. }
  71. return false
  72. },
  73. cache: {
  74. get: key => {
  75. return cacheClient[key]
  76. },
  77. set: (key, val) => {
  78. cacheClient[key] = val
  79. },
  80. has: key => {
  81. return !!cacheClient[key]
  82. }
  83. },
  84. directives: {
  85. example(vnode: VNode, directiveMeta: VNodeDirective) {
  86. // transform vnode based on directive binding metadata
  87. }
  88. }
  89. }
  90. )
  91. bundleRenderer.renderToString(context, (err, html) => {
  92. if (err) throw err
  93. const res: string = html
  94. })
  95. bundleRenderer.renderToString().then(html => {
  96. const res: string = html
  97. })
  98. bundleRenderer.renderToString(context).then(html => {
  99. const res: string = html
  100. })
  101. bundleRenderer.renderToStream(context).on('data', chunk => {
  102. const html = chunk.toString()
  103. })
  104. // webpack plugins
  105. webpack({
  106. plugins: [
  107. new VueSSRClientPlugin({
  108. filename: 'client-manifest.json'
  109. }),
  110. new VueSSRServerPlugin({
  111. filename: 'server-bundle.json'
  112. })
  113. ]
  114. })