componentNormalizer.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* globals __VUE_SSR_CONTEXT__ */
  2. // IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
  3. // This module is a runtime utility for cleaner component module output and will
  4. // be included in the final webpack user bundle.
  5. export default function normalizeComponent (
  6. scriptExports,
  7. render,
  8. staticRenderFns,
  9. functionalTemplate,
  10. injectStyles,
  11. scopeId,
  12. moduleIdentifier, /* server only */
  13. shadowMode /* vue-cli only */
  14. ) {
  15. // Vue.extend constructor export interop
  16. var options = typeof scriptExports === 'function'
  17. ? scriptExports.options
  18. : scriptExports
  19. // render functions
  20. if (render) {
  21. options.render = render
  22. options.staticRenderFns = staticRenderFns
  23. options._compiled = true
  24. }
  25. // functional template
  26. if (functionalTemplate) {
  27. options.functional = true
  28. }
  29. // scopedId
  30. if (scopeId) {
  31. options._scopeId = 'data-v-' + scopeId
  32. }
  33. var hook
  34. if (moduleIdentifier) { // server build
  35. hook = function (context) {
  36. // 2.3 injection
  37. context =
  38. context || // cached call
  39. (this.$vnode && this.$vnode.ssrContext) || // stateful
  40. (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
  41. // 2.2 with runInNewContext: true
  42. if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
  43. context = __VUE_SSR_CONTEXT__
  44. }
  45. // inject component styles
  46. if (injectStyles) {
  47. injectStyles.call(this, context)
  48. }
  49. // register component module identifier for async chunk inferrence
  50. if (context && context._registeredComponents) {
  51. context._registeredComponents.add(moduleIdentifier)
  52. }
  53. }
  54. // used by ssr in case component is cached and beforeCreate
  55. // never gets called
  56. options._ssrRegister = hook
  57. } else if (injectStyles) {
  58. hook = shadowMode
  59. ? function () {
  60. injectStyles.call(
  61. this,
  62. (options.functional ? this.parent : this).$root.$options.shadowRoot
  63. )
  64. }
  65. : injectStyles
  66. }
  67. if (hook) {
  68. if (options.functional) {
  69. // for template-only hot-reload because in that case the render fn doesn't
  70. // go through the normalizer
  71. options._injectStyles = hook
  72. // register for functional component in vue file
  73. var originalRender = options.render
  74. options.render = function renderWithStyleInjection (h, context) {
  75. hook.call(context)
  76. return originalRender(h, context)
  77. }
  78. } else {
  79. // inject component registration as beforeCreate hook
  80. var existing = options.beforeCreate
  81. options.beforeCreate = existing
  82. ? [].concat(existing, hook)
  83. : [hook]
  84. }
  85. }
  86. return {
  87. exports: scriptExports,
  88. options: options
  89. }
  90. }