Skip to content

Commit 72a8c13

Browse files
committed
reduce the amount of methods exposed on Vue.util
1 parent 6c3e6dc commit 72a8c13

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

src/core/global-api/index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
/* @flow */
22

33
import config from '../config'
4-
import * as util from '../util/index'
54
import { initUse } from './use'
65
import { initMixin } from './mixin'
76
import { initExtend } from './extend'
87
import { initAssetRegisters } from './assets'
98
import { set, del } from '../observer/index'
109
import builtInComponents from '../components/index'
1110

11+
import {
12+
warn,
13+
extend,
14+
nextTick,
15+
mergeOptions,
16+
defineReactive
17+
} from '../util/index'
18+
1219
export function initGlobalAPI (Vue: GlobalAPI) {
1320
// config
1421
const configDef = {}
1522
configDef.get = () => config
1623
if (process.env.NODE_ENV !== 'production') {
1724
configDef.set = () => {
18-
util.warn(
25+
warn(
1926
'Do not replace the Vue.config object, set individual fields instead.'
2027
)
2128
}
2229
}
2330
Object.defineProperty(Vue, 'config', configDef)
24-
Vue.util = util
31+
32+
// exposed util methods.
33+
// NOTE: these are not considered part of the public API - avoid relying on
34+
// them unless you are aware of the risk.
35+
Vue.util = {
36+
warn,
37+
extend,
38+
mergeOptions,
39+
defineReactive
40+
}
41+
2542
Vue.set = set
2643
Vue.delete = del
27-
Vue.nextTick = util.nextTick
44+
Vue.nextTick = nextTick
2845

2946
Vue.options = Object.create(null)
3047
config._assetTypes.forEach(type => {
@@ -35,7 +52,7 @@ export function initGlobalAPI (Vue: GlobalAPI) {
3552
// components with in Weex's multi-instance scenarios.
3653
Vue.options._base = Vue
3754

38-
util.extend(Vue.options.components, builtInComponents)
55+
extend(Vue.options.components, builtInComponents)
3956

4057
initUse(Vue)
4158
initMixin(Vue)

src/core/instance/inject.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* @flow */
22

3-
import { isNative } from 'core/util/env'
4-
5-
const hasReflect = typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
3+
import { hasSymbol } from 'core/util/env'
64

75
export function initInjections (vm: Component) {
86
const provide = vm.$options.provide
@@ -18,7 +16,7 @@ export function initInjections (vm: Component) {
1816
const isArray = Array.isArray(inject)
1917
const keys = isArray
2018
? inject
21-
: hasReflect
19+
: hasSymbol
2220
? Reflect.ownKeys(inject)
2321
: Object.keys(inject)
2422

src/core/util/env.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export function isNative (Ctor: Function): boolean {
4141
return /native code/.test(Ctor.toString())
4242
}
4343

44+
export const hasSymbol = typeof Symbol !== 'undefined' && isNative(Symbol)
45+
4446
/**
4547
* Defer a task to execute it asynchronously.
4648
*/

test/unit/features/error-handling.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { formatComponentName } from 'core/util/debug'
23

34
const components = createErrorTestComponents()
45

@@ -102,26 +103,25 @@ describe('Error handling', () => {
102103
})
103104

104105
it('properly format component names', () => {
105-
const format = Vue.util.formatComponentName
106106
const vm = new Vue()
107-
expect(format(vm)).toBe('<Root>')
107+
expect(formatComponentName(vm)).toBe('<Root>')
108108

109109
vm.$root = null
110110
vm.$options.name = 'hello-there'
111-
expect(format(vm)).toBe('<HelloThere>')
111+
expect(formatComponentName(vm)).toBe('<HelloThere>')
112112

113113
vm.$options.name = null
114114
vm.$options._componentTag = 'foo-bar-1'
115-
expect(format(vm)).toBe('<FooBar1>')
115+
expect(formatComponentName(vm)).toBe('<FooBar1>')
116116

117117
vm.$options._componentTag = null
118118
vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
119-
expect(format(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
120-
expect(format(vm, false)).toBe('<SomeThing>')
119+
expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
120+
expect(formatComponentName(vm, false)).toBe('<SomeThing>')
121121

122122
vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
123-
expect(format(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
124-
expect(format(vm, false)).toBe('<WindowsFile>')
123+
expect(formatComponentName(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
124+
expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
125125
})
126126
})
127127

test/unit/features/global-api/config.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { warn } from 'core/util/debug'
23

34
describe('Global config', () => {
45
it('should warn replacing config object', () => {
@@ -10,13 +11,13 @@ describe('Global config', () => {
1011

1112
describe('silent', () => {
1213
it('should be false by default', () => {
13-
Vue.util.warn('foo')
14+
warn('foo')
1415
expect('foo').toHaveBeenWarned()
1516
})
1617

1718
it('should work when set to true', () => {
1819
Vue.config.silent = true
19-
Vue.util.warn('foo')
20+
warn('foo')
2021
expect('foo').not.toHaveBeenWarned()
2122
Vue.config.silent = false
2223
})

test/unit/features/options/mixins.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
const mergeOptions = Vue.util.mergeOptions
2+
import { mergeOptions } from 'core/util/index'
33

44
describe('Options mixins', () => {
55
it('vm should have options from mixin', () => {

0 commit comments

Comments
 (0)