Bläddra i källkod

make all assertions/warnings be dev only

ktsn 8 år sedan
förälder
incheckning
46a88b208d
3 ändrade filer med 49 tillägg och 22 borttagningar
  1. 2 2
      src/helpers.js
  2. 10 3
      src/module/module-collection.js
  3. 37 17
      src/store.js

+ 2 - 2
src/helpers.js

@@ -44,7 +44,7 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
       if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
         return
       }
-      if (!(val in this.$store.getters)) {
+      if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
         console.error(`[vuex] unknown getter: ${val}`)
         return
       }
@@ -90,7 +90,7 @@ function normalizeNamespace (fn) {
 
 function getModuleByNamespace (store, helper, namespace) {
   const module = store._modulesNamespaceMap[namespace]
-  if (!module) {
+  if (process.env.NODE_ENV !== 'production' && !module) {
     console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
   }
   return module

+ 10 - 3
src/module/module-collection.js

@@ -26,7 +26,9 @@ export default class ModuleCollection {
   }
 
   register (path, rawModule, runtime = true) {
-    assertRawModule(path, rawModule)
+    if (process.env.NODE_ENV !== 'production') {
+      assertRawModule(path, rawModule)
+    }
 
     const newModule = new Module(rawModule, runtime)
     if (path.length === 0) {
@@ -54,7 +56,9 @@ export default class ModuleCollection {
 }
 
 function update (path, targetModule, newModule) {
-  assertRawModule(path, newModule)
+  if (process.env.NODE_ENV !== 'production') {
+    assertRawModule(path, newModule)
+  }
 
   // update target module
   targetModule.update(newModule)
@@ -62,7 +66,10 @@ function update (path, targetModule, newModule) {
   // update nested modules
   if (newModule.modules) {
     for (const key in newModule.modules) {
-      if (!targetModule.getChild(key)) {
+      if (
+        process.env.NODE_ENV !== 'production' &&
+        !targetModule.getChild(key)
+      ) {
         console.warn(
           `[vuex] trying to add a new module '${key}' on hot reloading, ` +
           'manual reload is needed'

+ 37 - 17
src/store.js

@@ -7,9 +7,11 @@ let Vue // bind on install
 
 export class Store {
   constructor (options = {}) {
-    assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
-    assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
-    assert(this instanceof Store, `Store must be called with the new operator.`)
+    if (process.env.NODE_ENV !== 'production') {
+      assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
+      assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
+      assert(this instanceof Store, `Store must be called with the new operator.`)
+    }
 
     const {
       plugins = [],
@@ -64,7 +66,9 @@ export class Store {
   }
 
   set state (v) {
-    assert(false, `Use store.replaceState() to explicit replace store state.`)
+    if (process.env.NODE_ENV !== 'production') {
+      assert(false, `Use store.replaceState() to explicit replace store state.`)
+    }
   }
 
   commit (_type, _payload, _options) {
@@ -77,7 +81,7 @@ export class Store {
 
     const mutation = { type, payload }
     const entry = this._mutations[type]
-    if (!entry) {
+    if (process.env.NODE_ENV !== 'production' && !entry) {
       console.error(`[vuex] unknown mutation type: ${type}`)
       return
     }
@@ -88,7 +92,10 @@ export class Store {
     })
     this._subscribers.forEach(sub => sub(mutation, this.state))
 
-    if (options && options.silent) {
+    if (
+      process.env.NODE_ENV !== 'production' &&
+      options && options.silent
+    ) {
       console.warn(
         `[vuex] mutation type: ${type}. Silent option has been removed. ` +
         'Use the filter functionality in the vue-devtools'
@@ -104,7 +111,7 @@ export class Store {
     } = unifyObjectStyle(_type, _payload)
 
     const entry = this._actions[type]
-    if (!entry) {
+    if (process.env.NODE_ENV !== 'production' && !entry) {
       console.error(`[vuex] unknown action type: ${type}`)
       return
     }
@@ -127,7 +134,9 @@ export class Store {
   }
 
   watch (getter, cb, options) {
-    assert(typeof getter === 'function', `store.watch only accepts a function.`)
+    if (process.env.NODE_ENV !== 'production') {
+      assert(typeof getter === 'function', `store.watch only accepts a function.`)
+    }
     return this._watcherVM.$watch(() => getter(this.state, this.getters), cb, options)
   }
 
@@ -139,8 +148,11 @@ export class Store {
 
   registerModule (path, rawModule) {
     if (typeof path === 'string') path = [path]
-    assert(Array.isArray(path), `module path must be a string or an Array.`)
-    assert(path.length > 0, 'cannot register the root module by using registerModule.')
+
+    if (process.env.NODE_ENV !== 'production') {
+      assert(Array.isArray(path), `module path must be a string or an Array.`)
+      assert(path.length > 0, 'cannot register the root module by using registerModule.')
+    }
 
     this._modules.register(path, rawModule)
     installModule(this, this.state, path, this._modules.get(path))
@@ -150,7 +162,11 @@ export class Store {
 
   unregisterModule (path) {
     if (typeof path === 'string') path = [path]
-    assert(Array.isArray(path), `module path must be a string or an Array.`)
+
+    if (process.env.NODE_ENV !== 'production') {
+      assert(Array.isArray(path), `module path must be a string or an Array.`)
+    }
+
     this._modules.unregister(path)
     this._withCommit(() => {
       const parentState = getNestedState(this.state, path.slice(0, -1))
@@ -285,7 +301,7 @@ function makeLocalContext (store, namespace, path) {
 
       if (!options || !options.root) {
         type = namespace + type
-        if (!store._actions[type]) {
+        if (process.env.NODE_ENV !== 'production' && !store._actions[type]) {
           console.error(`[vuex] unknown local action type: ${args.type}, global type: ${type}`)
           return
         }
@@ -301,7 +317,7 @@ function makeLocalContext (store, namespace, path) {
 
       if (!options || !options.root) {
         type = namespace + type
-        if (!store._mutations[type]) {
+        if (process.env.NODE_ENV !== 'production' && !store._mutations[type]) {
           console.error(`[vuex] unknown local mutation type: ${args.type}, global type: ${type}`)
           return
         }
@@ -383,7 +399,7 @@ function registerAction (store, type, handler, local) {
 }
 
 function registerGetter (store, type, rawGetter, local) {
-  if (store._wrappedGetters[type]) {
+  if (process.env.NODE_ENV !== 'production' && store._wrappedGetters[type]) {
     console.error(`[vuex] duplicate getter key: ${type}`)
     return
   }
@@ -399,7 +415,9 @@ function registerGetter (store, type, rawGetter, local) {
 
 function enableStrictMode (store) {
   store._vm.$watch(function () { return this._data.$$state }, () => {
-    assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
+    if (process.env.NODE_ENV !== 'production') {
+      assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
+    }
   }, { deep: true, sync: true })
 }
 
@@ -416,13 +434,15 @@ function unifyObjectStyle (type, payload, options) {
     type = type.type
   }
 
-  assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
+  if (process.env.NODE_ENV !== 'production') {
+    assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
+  }
 
   return { type, payload, options }
 }
 
 export function install (_Vue) {
-  if (Vue) {
+  if (process.env.NODE_ENV !== 'production' && Vue) {
     console.error(
       '[vuex] already installed. Vue.use(Vuex) should be called only once.'
     )