component-normalizer.js 2.8 KB

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