Browse Source

reduce the amount of methods exposed on Vue.util

dev
Evan You 8 years ago
parent
commit
72a8c13122
  1. 27
      src/core/global-api/index.js
  2. 6
      src/core/instance/inject.js
  3. 2
      src/core/util/env.js
  4. 16
      test/unit/features/error-handling.spec.js
  5. 5
      test/unit/features/global-api/config.spec.js
  6. 2
      test/unit/features/options/mixins.spec.js

27
src/core/global-api/index.js

@ -1,7 +1,6 @@
/* @flow */
import config from '../config'
import * as util from '../util/index'
import { initUse } from './use'
import { initMixin } from './mixin'
import { initExtend } from './extend'
@ -9,22 +8,40 @@ import { initAssetRegisters } from './assets'
import { set, del } from '../observer/index'
import builtInComponents from '../components/index'
import {
warn,
extend,
nextTick,
mergeOptions,
defineReactive
} from '../util/index'
export function initGlobalAPI (Vue: GlobalAPI) {
// config
const configDef = {}
configDef.get = () => config
if (process.env.NODE_ENV !== 'production') {
configDef.set = () => {
util.warn(
warn(
'Do not replace the Vue.config object, set individual fields instead.'
)
}
}
Object.defineProperty(Vue, 'config', configDef)
Vue.util = util
// exposed util methods.
// NOTE: these are not considered part of the public API - avoid relying on
// them unless you are aware of the risk.
Vue.util = {
warn,
extend,
mergeOptions,
defineReactive
}
Vue.set = set
Vue.delete = del
Vue.nextTick = util.nextTick
Vue.nextTick = nextTick
Vue.options = Object.create(null)
config._assetTypes.forEach(type => {
@ -35,7 +52,7 @@ export function initGlobalAPI (Vue: GlobalAPI) {
// components with in Weex's multi-instance scenarios.
Vue.options._base = Vue
util.extend(Vue.options.components, builtInComponents)
extend(Vue.options.components, builtInComponents)
initUse(Vue)
initMixin(Vue)

6
src/core/instance/inject.js

@ -1,8 +1,6 @@
/* @flow */
import { isNative } from 'core/util/env'
const hasReflect = typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
import { hasSymbol } from 'core/util/env'
export function initInjections (vm: Component) {
const provide = vm.$options.provide
@ -18,7 +16,7 @@ export function initInjections (vm: Component) {
const isArray = Array.isArray(inject)
const keys = isArray
? inject
: hasReflect
: hasSymbol
? Reflect.ownKeys(inject)
: Object.keys(inject)

2
src/core/util/env.js

@ -41,6 +41,8 @@ export function isNative (Ctor: Function): boolean {
return /native code/.test(Ctor.toString())
}
export const hasSymbol = typeof Symbol !== 'undefined' && isNative(Symbol)
/**
* Defer a task to execute it asynchronously.
*/

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

@ -1,4 +1,5 @@
import Vue from 'vue'
import { formatComponentName } from 'core/util/debug'
const components = createErrorTestComponents()
@ -102,26 +103,25 @@ describe('Error handling', () => {
})
it('properly format component names', () => {
const format = Vue.util.formatComponentName
const vm = new Vue()
expect(format(vm)).toBe('<Root>')
expect(formatComponentName(vm)).toBe('<Root>')
vm.$root = null
vm.$options.name = 'hello-there'
expect(format(vm)).toBe('<HelloThere>')
expect(formatComponentName(vm)).toBe('<HelloThere>')
vm.$options.name = null
vm.$options._componentTag = 'foo-bar-1'
expect(format(vm)).toBe('<FooBar1>')
expect(formatComponentName(vm)).toBe('<FooBar1>')
vm.$options._componentTag = null
vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
expect(format(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
expect(format(vm, false)).toBe('<SomeThing>')
expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
expect(formatComponentName(vm, false)).toBe('<SomeThing>')
vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
expect(format(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
expect(format(vm, false)).toBe('<WindowsFile>')
expect(formatComponentName(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
})
})

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

@ -1,4 +1,5 @@
import Vue from 'vue'
import { warn } from 'core/util/debug'
describe('Global config', () => {
it('should warn replacing config object', () => {
@ -10,13 +11,13 @@ describe('Global config', () => {
describe('silent', () => {
it('should be false by default', () => {
Vue.util.warn('foo')
warn('foo')
expect('foo').toHaveBeenWarned()
})
it('should work when set to true', () => {
Vue.config.silent = true
Vue.util.warn('foo')
warn('foo')
expect('foo').not.toHaveBeenWarned()
Vue.config.silent = false
})

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

@ -1,5 +1,5 @@
import Vue from 'vue'
const mergeOptions = Vue.util.mergeOptions
import { mergeOptions } from 'core/util/index'
describe('Options mixins', () => {
it('vm should have options from mixin', () => {

Loading…
Cancel
Save