Evan You 10 лет назад
Родитель
Сommit
3ca2c92920

+ 0 - 171
src/runtime/global-api.js

@@ -1,171 +0,0 @@
-import config from './config'
-import * as util from './util/index'
-import { createElement } from './vdom/index'
-import {
-  set,
-  del,
-  nextTick,
-  warn,
-  mergeOptions,
-  classify,
-  toArray,
-  isPlainObject,
-  isReservedTag
-} from './util/index'
-
-export function initGlobalAPI (Vue) {
-  Vue.h = Vue.createElement = createElement
-  Vue.config = config
-  Vue.util = util
-  Vue.set = set
-  Vue.delete = del
-  Vue.nextTick = nextTick
-
-  Vue.options = {
-    directives: Object.create(null),
-    filters: Object.create(null),
-    components: Object.create(null),
-    transitions: Object.create(null)
-  }
-
-  /**
-   * Each instance constructor, including Vue, has a unique
-   * cid. This enables us to create wrapped "child
-   * constructors" for prototypal inheritance and cache them.
-   */
-
-  Vue.cid = 0
-  let cid = 1
-
-  /**
-   * Class inheritance
-   *
-   * @param {Object} extendOptions
-   */
-
-  Vue.extend = function (extendOptions) {
-    extendOptions = extendOptions || {}
-    var Super = this
-    var isFirstExtend = Super.cid === 0
-    if (isFirstExtend && extendOptions._Ctor) {
-      return extendOptions._Ctor
-    }
-    var name = extendOptions.name || Super.options.name
-    if (process.env.NODE_ENV !== 'production') {
-      if (!/^[a-zA-Z][\w-]*$/.test(name)) {
-        warn(
-          'Invalid component name: "' + name + '". Component names ' +
-          'can only contain alphanumeric characaters and the hyphen.'
-        )
-        name = null
-      }
-    }
-    var Sub = createClass(name || 'VueComponent')
-    Sub.prototype = Object.create(Super.prototype)
-    Sub.prototype.constructor = Sub
-    Sub.cid = cid++
-    Sub.options = mergeOptions(
-      Super.options,
-      extendOptions
-    )
-    Sub['super'] = Super
-    // allow further extension
-    Sub.extend = Super.extend
-    // create asset registers, so extended classes
-    // can have their private assets too.
-    config._assetTypes.forEach(function (type) {
-      Sub[type] = Super[type]
-    })
-    // enable recursive self-lookup
-    if (name) {
-      Sub.options.components[name] = Sub
-    }
-    // cache constructor
-    if (isFirstExtend) {
-      extendOptions._Ctor = Sub
-    }
-    return Sub
-  }
-
-  /**
-   * A function that returns a sub-class constructor with the
-   * given name. This gives us much nicer output when
-   * logging instances in the console.
-   *
-   * @param {String} name
-   * @return {Function}
-   */
-
-  function createClass (name) {
-    return new Function(
-      `return function ${classify(name)} (options) { this._init(options) }`
-    )()
-  }
-
-  /**
-   * Plugin system
-   *
-   * @param {Object} plugin
-   */
-
-  Vue.use = function (plugin) {
-    /* istanbul ignore if */
-    if (plugin.installed) {
-      return
-    }
-    // additional parameters
-    var args = toArray(arguments, 1)
-    args.unshift(this)
-    if (typeof plugin.install === 'function') {
-      plugin.install.apply(plugin, args)
-    } else {
-      plugin.apply(null, args)
-    }
-    plugin.installed = true
-    return this
-  }
-
-  /**
-   * Apply a global mixin by merging it into the default
-   * options.
-   */
-
-  Vue.mixin = function (mixin) {
-    Vue.options = mergeOptions(Vue.options, mixin)
-  }
-
-  /**
-   * Create asset registration methods with the following
-   * signature:
-   *
-   * @param {String} id
-   * @param {*} definition
-   */
-
-  config._assetTypes.forEach(function (type) {
-    Vue[type] = function (id, definition) {
-      if (!definition) {
-        return this.options[type + 's'][id]
-      } else {
-        /* istanbul ignore if */
-        if (process.env.NODE_ENV !== 'production') {
-          if (type === 'component' && isReservedTag(id)) {
-            warn(
-              'Do not use built-in or reserved HTML elements as component ' +
-              'id: ' + id
-            )
-          }
-        }
-        if (
-          type === 'component' &&
-          isPlainObject(definition)
-        ) {
-          definition.name = id
-          definition = Vue.extend(definition)
-        }
-        this.options[type + 's'][id] = definition
-        return definition
-      }
-    }
-  })
-}

+ 36 - 0
src/runtime/global-api/assets.js

@@ -0,0 +1,36 @@
+import config from '../config'
+import { warn, isPlainObject, isReservedTag } from '../util/index'
+
+export function initAssetRegisters (Vue) {
+  /**
+     * Create asset registration methods with the following
+     * signature:
+     *
+     * @param {String} id
+     * @param {*} definition
+     */
+
+  config._assetTypes.forEach(function (type) {
+    Vue[type] = function (id, definition) {
+      if (!definition) {
+        return this.options[type + 's'][id]
+      } else {
+        /* istanbul ignore if */
+        if (process.env.NODE_ENV !== 'production') {
+          if (type === 'component' && isReservedTag(id)) {
+            warn(
+              'Do not use built-in or reserved HTML elements as component ' +
+              'id: ' + id
+            )
+          }
+        }
+        if (type === 'component' && isPlainObject(definition)) {
+          definition.name = id
+          definition = Vue.extend(definition)
+        }
+        this.options[type + 's'][id] = definition
+        return definition
+      }
+    }
+  })
+}

+ 78 - 0
src/runtime/global-api/extend.js

@@ -0,0 +1,78 @@
+import config from '../config'
+import { warn, classify, mergeOptions } from '../util/index'
+
+export function initExtend (Vue) {
+  /**
+   * Each instance constructor, including Vue, has a unique
+   * cid. This enables us to create wrapped "child
+   * constructors" for prototypal inheritance and cache them.
+   */
+
+  Vue.cid = 0
+  let cid = 1
+
+  /**
+   * Class inheritance
+   *
+   * @param {Object} extendOptions
+   */
+
+  Vue.extend = function (extendOptions) {
+    extendOptions = extendOptions || {}
+    var Super = this
+    var isFirstExtend = Super.cid === 0
+    if (isFirstExtend && extendOptions._Ctor) {
+      return extendOptions._Ctor
+    }
+    var name = extendOptions.name || Super.options.name
+    if (process.env.NODE_ENV !== 'production') {
+      if (!/^[a-zA-Z][\w-]*$/.test(name)) {
+        warn(
+          'Invalid component name: "' + name + '". Component names ' +
+          'can only contain alphanumeric characaters and the hyphen.'
+        )
+        name = null
+      }
+    }
+    var Sub = createClass(name || 'VueComponent')
+    Sub.prototype = Object.create(Super.prototype)
+    Sub.prototype.constructor = Sub
+    Sub.cid = cid++
+    Sub.options = mergeOptions(
+      Super.options,
+      extendOptions
+    )
+    Sub['super'] = Super
+    // allow further extension
+    Sub.extend = Super.extend
+    // create asset registers, so extended classes
+    // can have their private assets too.
+    config._assetTypes.forEach(function (type) {
+      Sub[type] = Super[type]
+    })
+    // enable recursive self-lookup
+    if (name) {
+      Sub.options.components[name] = Sub
+    }
+    // cache constructor
+    if (isFirstExtend) {
+      extendOptions._Ctor = Sub
+    }
+    return Sub
+  }
+
+  /**
+   * A function that returns a sub-class constructor with the
+   * given name. This gives us much nicer output when
+   * logging instances in the console.
+   *
+   * @param {String} name
+   * @return {Function}
+   */
+
+  function createClass (name) {
+    return new Function(
+      `return function ${classify(name)} (options) { this._init(options) }`
+    )()
+  }
+}

+ 29 - 0
src/runtime/global-api/index.js

@@ -0,0 +1,29 @@
+import config from '../config'
+import * as util from '../util/index'
+import { createElement } from '../vdom/index'
+import { initUse } from './use'
+import { initMixin } from './mixin'
+import { initExtend } from './extend'
+import { initAssetRegisters } from './assets'
+import { set, del, nextTick } from '../util/index'
+
+export function initGlobalAPI (Vue) {
+  Vue.config = config
+  Vue.util = util
+  Vue.set = set
+  Vue.delete = del
+  Vue.nextTick = nextTick
+  Vue.h = Vue.createElement = createElement
+
+  Vue.options = {
+    directives: Object.create(null),
+    filters: Object.create(null),
+    components: Object.create(null),
+    transitions: Object.create(null)
+  }
+
+  initUse(Vue)
+  initMixin(Vue)
+  initExtend(Vue)
+  initAssetRegisters(Vue)
+}

+ 7 - 0
src/runtime/global-api/mixin.js

@@ -0,0 +1,7 @@
+import { mergeOptions } from '../util/index'
+
+export function initMixin (Vue) {
+  Vue.mixin = function (mixin) {
+    Vue.options = mergeOptions(Vue.options, mixin)
+  }
+}

+ 26 - 0
src/runtime/global-api/use.js

@@ -0,0 +1,26 @@
+import { toArray } from '../util/index'
+
+export function initUse (Vue) {
+  /**
+   * Plugin system
+   *
+   * @param {Object} plugin
+   */
+
+  Vue.use = function (plugin) {
+    /* istanbul ignore if */
+    if (plugin.installed) {
+      return
+    }
+    // additional parameters
+    var args = toArray(arguments, 1)
+    args.unshift(this)
+    if (typeof plugin.install === 'function') {
+      plugin.install.apply(plugin, args)
+    } else {
+      plugin.apply(null, args)
+    }
+    plugin.installed = true
+    return this
+  }
+}

+ 1 - 1
src/runtime/index.js

@@ -1,5 +1,5 @@
 import Vue from './instance/index'
-import { initGlobalAPI } from './global-api'
+import { initGlobalAPI } from './global-api/index'
 
 initGlobalAPI(Vue)