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