componentNormalizer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 =
  17. typeof scriptExports === 'function' ? scriptExports.options : scriptExports
  18. // render functions
  19. if (render) {
  20. options.render = render
  21. options.staticRenderFns = staticRenderFns
  22. options._compiled = true
  23. }
  24. // functional template
  25. if (functionalTemplate) {
  26. options.functional = true
  27. }
  28. // scopedId
  29. if (scopeId) {
  30. options._scopeId = 'data-v-' + scopeId
  31. }
  32. var hook
  33. if (moduleIdentifier) {
  34. // 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 ? [].concat(existing, hook) : [hook]
  82. }
  83. }
  84. return {
  85. exports: scriptExports,
  86. options: options
  87. }
  88. }