From 72a8c131226dce387042d1d36a46e94fefa9f353 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sat, 25 Feb 2017 18:06:22 -0500 Subject: [PATCH] reduce the amount of methods exposed on Vue.util --- src/core/global-api/index.js | 27 ++++++++++++++++---- src/core/instance/inject.js | 6 ++--- src/core/util/env.js | 2 ++ test/unit/features/error-handling.spec.js | 16 ++++++------ test/unit/features/global-api/config.spec.js | 5 ++-- test/unit/features/options/mixins.spec.js | 2 +- 6 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/core/global-api/index.js b/src/core/global-api/index.js index 60c44b53..fd53b1d7 100644 --- a/src/core/global-api/index.js +++ b/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) diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index 1940ea20..2b1fd7f0 100644 --- a/src/core/instance/inject.js +++ b/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) diff --git a/src/core/util/env.js b/src/core/util/env.js index 43d091d2..7a96915c 100644 --- a/src/core/util/env.js +++ b/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. */ diff --git a/test/unit/features/error-handling.spec.js b/test/unit/features/error-handling.spec.js index 2aae8744..f582edea 100644 --- a/test/unit/features/error-handling.spec.js +++ b/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('') + expect(formatComponentName(vm)).toBe('') vm.$root = null vm.$options.name = 'hello-there' - expect(format(vm)).toBe('') + expect(formatComponentName(vm)).toBe('') vm.$options.name = null vm.$options._componentTag = 'foo-bar-1' - expect(format(vm)).toBe('') + expect(formatComponentName(vm)).toBe('') vm.$options._componentTag = null vm.$options.__file = '/foo/bar/baz/SomeThing.vue' - expect(format(vm)).toBe(` at ${vm.$options.__file}`) - expect(format(vm, false)).toBe('') + expect(formatComponentName(vm)).toBe(` at ${vm.$options.__file}`) + expect(formatComponentName(vm, false)).toBe('') vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue' - expect(format(vm)).toBe(` at ${vm.$options.__file}`) - expect(format(vm, false)).toBe('') + expect(formatComponentName(vm)).toBe(` at ${vm.$options.__file}`) + expect(formatComponentName(vm, false)).toBe('') }) }) diff --git a/test/unit/features/global-api/config.spec.js b/test/unit/features/global-api/config.spec.js index 68d89963..bd97c40d 100644 --- a/test/unit/features/global-api/config.spec.js +++ b/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 }) diff --git a/test/unit/features/options/mixins.spec.js b/test/unit/features/options/mixins.spec.js index a4883a44..8385edfe 100644 --- a/test/unit/features/options/mixins.spec.js +++ b/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', () => {