Skip to content

Commit 3463fbc

Browse files
authored
refactor: rename createComponent as defineComponent (#230)
Since Vue `3.0.0-alpha.1`, `createComponent` has been renamed `defineComponent`. See vuejs/core@1c4cdd8
1 parent 1b58a67 commit 3463fbc

File tree

11 files changed

+94
-27
lines changed

11 files changed

+94
-27
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ After installing the plugin you can use the [Composition API](https://vue-compos
5757

5858
**This plugin requires TypeScript version >3.5.1. If you are using vetur, make sure to set `vetur.useWorkspaceDependencies` to `true`.**
5959

60-
To let TypeScript properly infer types inside Vue component options, you need to define components with `createComponent`:
60+
To let TypeScript properly infer types inside Vue component options, you need to define components with `defineComponent`:
6161

6262
```ts
63-
import { createComponent } from '@vue/composition-api';
63+
import { defineComponent } from '@vue/composition-api';
6464

65-
const Component = createComponent({
65+
const Component = defineComponent({
6666
// type inference enabled
6767
});
6868

README.zh-CN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ Vue.use(VueCompositionApi);
5757

5858
**请使用最新版的 TypeScript,如果你使用了 `vetur`,请将 `vetur.useWorkspaceDependencies` 设为 `true`**
5959

60-
为了让 TypeScript 正确的推导类型,我们必须使用 `createComponent` 来定义组件:
60+
为了让 TypeScript 正确的推导类型,我们必须使用 `defineComponent` 来定义组件:
6161

6262
```ts
63-
import { createComponent } from '@vue/composition-api';
63+
import { defineComponent } from '@vue/composition-api';
6464

65-
const Component = createComponent({
65+
const Component = defineComponent({
6666
// 启用类型推断
6767
});
6868

src/apis/computed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCurrentVue, getCurrentVM } from '../runtimeContext';
22
import { createRef, Ref } from '../reactivity';
3-
import { createComponentInstance } from '../helper';
3+
import { defineComponentInstance } from '../helper';
44
import { warn } from '../utils';
55

66
interface Option<T> {
@@ -25,7 +25,7 @@ export function computed<T>(
2525
set = options.set;
2626
}
2727

28-
const computedHost = createComponentInstance(getCurrentVue(), {
28+
const computedHost = defineComponentInstance(getCurrentVue(), {
2929
computed: {
3030
$$state: {
3131
get,

src/apis/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ComponentInstance } from '../component';
22
import { Ref, isRef } from '../reactivity';
33
import { assert, logError, noopFn } from '../utils';
4-
import { createComponentInstance } from '../helper';
4+
import { defineComponentInstance } from '../helper';
55
import { getCurrentVM, getCurrentVue } from '../runtimeContext';
66
import { WatcherPreFlushQueueKey, WatcherPostFlushQueueKey } from '../symbols';
77

@@ -262,7 +262,7 @@ export function watch(
262262
let vm = getCurrentVM();
263263
if (!vm) {
264264
if (!fallbackVM) {
265-
fallbackVM = createComponentInstance(getCurrentVue());
265+
fallbackVM = defineComponentInstance(getCurrentVue());
266266
}
267267
vm = fallbackVM;
268268
} else if (!hasWatchEnv(vm)) {

src/component/component.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ interface ComponentOptionsWithoutProps<Props = unknown, RawBindings = Data> {
7070
setup?: SetupFunction<Props, RawBindings>;
7171
}
7272

73+
// overload 1: object format with no props
74+
export function defineComponent<RawBindings>(
75+
options: ComponentOptionsWithoutProps<unknown, RawBindings>
76+
): VueProxy<unknown, RawBindings>;
77+
// overload 2: object format with object props declaration
78+
// see `ExtractPropTypes` in ./componentProps.ts
79+
export function defineComponent<
80+
Props,
81+
RawBindings = Data,
82+
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
83+
>(
84+
// prettier-ignore
85+
options: (
86+
// prefer the provided Props, otherwise infer it from PropsOptions
87+
HasDefined<Props> extends true
88+
? ComponentOptionsWithProps<PropsOptions, RawBindings, Props>
89+
: ComponentOptionsWithProps<PropsOptions, RawBindings>) &
90+
Omit<Vue2ComponentOptions<Vue>, keyof ComponentOptionsWithProps<never, never>>
91+
): VueProxy<PropsOptions, RawBindings>;
92+
// implementation, close to no-op
93+
export function defineComponent(options: any) {
94+
return options as any;
95+
}
96+
97+
// createComponent is kept around for retro-compatibility
7398
// overload 1: object format with no props
7499
export function createComponent<RawBindings>(
75100
options: ComponentOptionsWithoutProps<unknown, RawBindings>
@@ -89,7 +114,10 @@ export function createComponent<
89114
: ComponentOptionsWithProps<PropsOptions, RawBindings>) &
90115
Omit<Vue2ComponentOptions<Vue>, keyof ComponentOptionsWithProps<never, never>>
91116
): VueProxy<PropsOptions, RawBindings>;
92-
// implementation, close to no-op
117+
// implementation, deferring to defineComponent, but logging a warning in dev mode
93118
export function createComponent(options: any) {
94-
return options as any;
119+
if (process.env.NODE_ENV !== 'production') {
120+
Vue.util.warn('`createComponent` has been renamed to `defineComponent`.');
121+
}
122+
return defineComponent(options);
95123
}

src/component/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {
22
Data,
33
createComponent,
4+
defineComponent,
45
SetupFunction,
56
SetupContext,
67
ComponentInstance,

src/createElement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue';
22
import { currentVM, getCurrentVue } from './runtimeContext';
3-
import { createComponentInstance } from './helper';
3+
import { defineComponentInstance } from './helper';
44
import { warn } from './utils';
55

66
type CreateElement = Vue['$createElement'];
@@ -11,7 +11,7 @@ const createElement: CreateElement = function createElement(...args: any) {
1111
if (!currentVM) {
1212
warn('`createElement()` has been called outside of render function.');
1313
if (!fallbackCreateElement) {
14-
fallbackCreateElement = createComponentInstance(getCurrentVue()).$createElement;
14+
fallbackCreateElement = defineComponentInstance(getCurrentVue()).$createElement;
1515
}
1616

1717
return fallbackCreateElement.apply(fallbackCreateElement, args);

src/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function ensureCurrentVMInFn(hook: string): ComponentInstance {
1111
return vm!;
1212
}
1313

14-
export function createComponentInstance<V extends Vue = Vue>(
14+
export function defineComponentInstance<V extends Vue = Vue>(
1515
Ctor: VueConstructor<V>,
1616
options: ComponentOptions<V> = {}
1717
) {

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) {
2323
export default plugin;
2424
export { default as createElement } from './createElement';
2525
export { SetupContext };
26-
export { createComponent, ComponentRenderProxy, PropType, PropOptions } from './component';
26+
export {
27+
createComponent,
28+
defineComponent,
29+
ComponentRenderProxy,
30+
PropType,
31+
PropOptions,
32+
} from './component';
2733
// For getting a hold of the interal instance in setup() - useful for advanced
2834
// plugins
2935
export { getCurrentVM as getCurrentInstance } from './runtimeContext';

src/reactivity/reactive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AnyObject } from '../types/basic';
22
import { getCurrentVue } from '../runtimeContext';
33
import { isPlainObject, def, hasOwn, warn } from '../utils';
4-
import { isComponentInstance, createComponentInstance } from '../helper';
4+
import { isComponentInstance, defineComponentInstance } from '../helper';
55
import {
66
AccessControlIdentifierKey,
77
ReactiveIdentifierKey,
@@ -113,7 +113,7 @@ function observe<T>(obj: T): T {
113113
if (Vue.observable) {
114114
observed = Vue.observable(obj);
115115
} else {
116-
const vm = createComponentInstance(Vue, {
116+
const vm = defineComponentInstance(Vue, {
117117
data: {
118118
$$state: obj,
119119
},

0 commit comments

Comments
 (0)