|
| 1 | +import { reactive } from '@vue/reactivity' |
| 2 | +import { |
| 3 | + createApp, |
| 4 | + defineComponent, |
| 5 | + nextTick, |
| 6 | + App, |
| 7 | + AppConfig, |
| 8 | + Plugin, |
| 9 | + Component, |
| 10 | + ComponentOptions, |
| 11 | + ComponentPublicInstance, |
| 12 | + Directive, |
| 13 | + RenderFunction, |
| 14 | + isRuntimeOnly |
| 15 | +} from '@vue/runtime-dom' |
| 16 | + |
| 17 | +// TODO make these getter/setters and trigger deprecation warnings |
| 18 | +export type LegacyConfig = AppConfig & { |
| 19 | + /** |
| 20 | + * @deprecated `config.silent` option has been removed |
| 21 | + */ |
| 22 | + silent?: boolean |
| 23 | + /** |
| 24 | + * @deprecated use __VUE_PROD_DEVTOOLS__ compile-time feature flag instead |
| 25 | + * https://github.com/vuejs/vue-next/tree/master/packages/vue#bundler-build-feature-flags |
| 26 | + */ |
| 27 | + devtools?: boolean |
| 28 | + /** |
| 29 | + * @deprecated use `config.isCustomElement` instead |
| 30 | + * https://v3.vuejs.org/guide/migration/global-api.html#config-ignoredelements-is-now-config-iscustomelement |
| 31 | + */ |
| 32 | + ignoredElements?: (string | RegExp)[] |
| 33 | + /** |
| 34 | + * @deprecated |
| 35 | + * https://v3.vuejs.org/guide/migration/keycode-modifiers.html |
| 36 | + */ |
| 37 | + keyCodes?: Record<string, number | number[]> |
| 38 | + /** |
| 39 | + * @deprecated |
| 40 | + * https://v3.vuejs.org/guide/migration/global-api.html#config-productiontip-removed |
| 41 | + */ |
| 42 | + productionTip?: boolean |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @deprecated the default `Vue` export has been removed in Vue 3. The type for |
| 47 | + * the default export is provided only for migration purposes. Please use |
| 48 | + * named imports instead - e.g. `import { createApp } from 'vue'`. |
| 49 | + */ |
| 50 | +export type GlobalVue = Pick<App, 'version' | 'component' | 'directive'> & { |
| 51 | + // no inference here since these types are not meant for actual use - they |
| 52 | + // are merely here to provide type checks for internal implementation and |
| 53 | + // information for migration. |
| 54 | + new (options?: ComponentOptions): ComponentPublicInstance |
| 55 | + |
| 56 | + version: string |
| 57 | + config: LegacyConfig |
| 58 | + |
| 59 | + extend: typeof defineComponent |
| 60 | + nextTick: typeof nextTick |
| 61 | + |
| 62 | + use(plugin: Plugin, ...options: any[]): GlobalVue |
| 63 | + mixin(mixin: ComponentOptions): GlobalVue |
| 64 | + |
| 65 | + component(name: string): Component | undefined |
| 66 | + component(name: string, component: Component): GlobalVue |
| 67 | + directive(name: string): Directive | undefined |
| 68 | + directive(name: string, directive: Directive): GlobalVue |
| 69 | + |
| 70 | + compile(template: string): RenderFunction |
| 71 | + |
| 72 | + /** |
| 73 | + * @deprecated Vue 3 no longer needs set() for adding new properties. |
| 74 | + */ |
| 75 | + set(target: any, key: string | number | symbol, value: any): void |
| 76 | + /** |
| 77 | + * @deprecated Vue 3 no longer needs delete() for property deletions. |
| 78 | + */ |
| 79 | + delete(target: any, key: string | number | symbol): void |
| 80 | + /** |
| 81 | + * @deprecated use `reactive` instead. |
| 82 | + */ |
| 83 | + observable: typeof reactive |
| 84 | + /** |
| 85 | + * @deprecated filters have been removed from Vue 3. |
| 86 | + */ |
| 87 | + filter(name: string, arg: any): null |
| 88 | +} |
| 89 | + |
| 90 | +export const Vue: GlobalVue = function Vue(options: ComponentOptions = {}) { |
| 91 | + const app = createApp(options) |
| 92 | + // copy over global config mutations |
| 93 | + for (const key in singletonApp.config) { |
| 94 | + if ( |
| 95 | + key !== 'isNativeTag' && |
| 96 | + !(key === 'isCustomElement' && isRuntimeOnly()) |
| 97 | + ) { |
| 98 | + // @ts-ignore |
| 99 | + app.config[key] = singletonApp.config[key] |
| 100 | + } |
| 101 | + } |
| 102 | + if (options.el) { |
| 103 | + return app.mount(options.el) |
| 104 | + } |
| 105 | +} as any |
| 106 | + |
| 107 | +const singletonApp = createApp({}) |
| 108 | + |
| 109 | +Vue.version = __VERSION__ |
| 110 | +Vue.config = singletonApp.config |
| 111 | + |
| 112 | +Vue.extend = defineComponent |
| 113 | +Vue.nextTick = nextTick |
| 114 | + |
| 115 | +Vue.set = (target, key, value) => { |
| 116 | + // TODO deprecation warnings |
| 117 | + target[key] = value |
| 118 | +} |
| 119 | +Vue.delete = (target, key) => { |
| 120 | + // TODO deprecation warnings |
| 121 | + delete target[key] |
| 122 | +} |
| 123 | +// TODO wrap with deprecation warning |
| 124 | +Vue.observable = reactive |
| 125 | + |
| 126 | +Vue.use = (p, ...options) => { |
| 127 | + singletonApp.use(p, ...options) |
| 128 | + return Vue |
| 129 | +} |
| 130 | + |
| 131 | +Vue.mixin = m => { |
| 132 | + singletonApp.mixin(m) |
| 133 | + return Vue |
| 134 | +} |
| 135 | + |
| 136 | +Vue.component = ((name: string, comp: any) => { |
| 137 | + if (comp) { |
| 138 | + singletonApp.component(name, comp) |
| 139 | + return Vue |
| 140 | + } else { |
| 141 | + return singletonApp.component(name) |
| 142 | + } |
| 143 | +}) as any |
| 144 | + |
| 145 | +Vue.directive = ((name: string, dir: any) => { |
| 146 | + if (dir) { |
| 147 | + singletonApp.directive(name, dir) |
| 148 | + return Vue |
| 149 | + } else { |
| 150 | + return singletonApp.directive(name) |
| 151 | + } |
| 152 | +}) as any |
| 153 | + |
| 154 | +Vue.filter = ((name: string, filter: any) => { |
| 155 | + // TODO deprecation warning |
| 156 | + // TODO compiler warning for filters (maybe behavior compat?) |
| 157 | +}) as any |
0 commit comments