Skip to content

Commit 46a88b2

Browse files
ktsnyyx990803
authored andcommitted
make all assertions/warnings be dev only
1 parent 45c471c commit 46a88b2

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
4444
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
4545
return
4646
}
47-
if (!(val in this.$store.getters)) {
47+
if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
4848
console.error(`[vuex] unknown getter: ${val}`)
4949
return
5050
}
@@ -90,7 +90,7 @@ function normalizeNamespace (fn) {
9090

9191
function getModuleByNamespace (store, helper, namespace) {
9292
const module = store._modulesNamespaceMap[namespace]
93-
if (!module) {
93+
if (process.env.NODE_ENV !== 'production' && !module) {
9494
console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
9595
}
9696
return module

src/module/module-collection.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export default class ModuleCollection {
2626
}
2727

2828
register (path, rawModule, runtime = true) {
29-
assertRawModule(path, rawModule)
29+
if (process.env.NODE_ENV !== 'production') {
30+
assertRawModule(path, rawModule)
31+
}
3032

3133
const newModule = new Module(rawModule, runtime)
3234
if (path.length === 0) {
@@ -54,15 +56,20 @@ export default class ModuleCollection {
5456
}
5557

5658
function update (path, targetModule, newModule) {
57-
assertRawModule(path, newModule)
59+
if (process.env.NODE_ENV !== 'production') {
60+
assertRawModule(path, newModule)
61+
}
5862

5963
// update target module
6064
targetModule.update(newModule)
6165

6266
// update nested modules
6367
if (newModule.modules) {
6468
for (const key in newModule.modules) {
65-
if (!targetModule.getChild(key)) {
69+
if (
70+
process.env.NODE_ENV !== 'production' &&
71+
!targetModule.getChild(key)
72+
) {
6673
console.warn(
6774
`[vuex] trying to add a new module '${key}' on hot reloading, ` +
6875
'manual reload is needed'

src/store.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ let Vue // bind on install
77

88
export class Store {
99
constructor (options = {}) {
10-
assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
11-
assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
12-
assert(this instanceof Store, `Store must be called with the new operator.`)
10+
if (process.env.NODE_ENV !== 'production') {
11+
assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
12+
assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
13+
assert(this instanceof Store, `Store must be called with the new operator.`)
14+
}
1315

1416
const {
1517
plugins = [],
@@ -64,7 +66,9 @@ export class Store {
6466
}
6567

6668
set state (v) {
67-
assert(false, `Use store.replaceState() to explicit replace store state.`)
69+
if (process.env.NODE_ENV !== 'production') {
70+
assert(false, `Use store.replaceState() to explicit replace store state.`)
71+
}
6872
}
6973

7074
commit (_type, _payload, _options) {
@@ -77,7 +81,7 @@ export class Store {
7781

7882
const mutation = { type, payload }
7983
const entry = this._mutations[type]
80-
if (!entry) {
84+
if (process.env.NODE_ENV !== 'production' && !entry) {
8185
console.error(`[vuex] unknown mutation type: ${type}`)
8286
return
8387
}
@@ -88,7 +92,10 @@ export class Store {
8892
})
8993
this._subscribers.forEach(sub => sub(mutation, this.state))
9094

91-
if (options && options.silent) {
95+
if (
96+
process.env.NODE_ENV !== 'production' &&
97+
options && options.silent
98+
) {
9299
console.warn(
93100
`[vuex] mutation type: ${type}. Silent option has been removed. ` +
94101
'Use the filter functionality in the vue-devtools'
@@ -104,7 +111,7 @@ export class Store {
104111
} = unifyObjectStyle(_type, _payload)
105112

106113
const entry = this._actions[type]
107-
if (!entry) {
114+
if (process.env.NODE_ENV !== 'production' && !entry) {
108115
console.error(`[vuex] unknown action type: ${type}`)
109116
return
110117
}
@@ -127,7 +134,9 @@ export class Store {
127134
}
128135

129136
watch (getter, cb, options) {
130-
assert(typeof getter === 'function', `store.watch only accepts a function.`)
137+
if (process.env.NODE_ENV !== 'production') {
138+
assert(typeof getter === 'function', `store.watch only accepts a function.`)
139+
}
131140
return this._watcherVM.$watch(() => getter(this.state, this.getters), cb, options)
132141
}
133142

@@ -139,8 +148,11 @@ export class Store {
139148

140149
registerModule (path, rawModule) {
141150
if (typeof path === 'string') path = [path]
142-
assert(Array.isArray(path), `module path must be a string or an Array.`)
143-
assert(path.length > 0, 'cannot register the root module by using registerModule.')
151+
152+
if (process.env.NODE_ENV !== 'production') {
153+
assert(Array.isArray(path), `module path must be a string or an Array.`)
154+
assert(path.length > 0, 'cannot register the root module by using registerModule.')
155+
}
144156

145157
this._modules.register(path, rawModule)
146158
installModule(this, this.state, path, this._modules.get(path))
@@ -150,7 +162,11 @@ export class Store {
150162

151163
unregisterModule (path) {
152164
if (typeof path === 'string') path = [path]
153-
assert(Array.isArray(path), `module path must be a string or an Array.`)
165+
166+
if (process.env.NODE_ENV !== 'production') {
167+
assert(Array.isArray(path), `module path must be a string or an Array.`)
168+
}
169+
154170
this._modules.unregister(path)
155171
this._withCommit(() => {
156172
const parentState = getNestedState(this.state, path.slice(0, -1))
@@ -285,7 +301,7 @@ function makeLocalContext (store, namespace, path) {
285301

286302
if (!options || !options.root) {
287303
type = namespace + type
288-
if (!store._actions[type]) {
304+
if (process.env.NODE_ENV !== 'production' && !store._actions[type]) {
289305
console.error(`[vuex] unknown local action type: ${args.type}, global type: ${type}`)
290306
return
291307
}
@@ -301,7 +317,7 @@ function makeLocalContext (store, namespace, path) {
301317

302318
if (!options || !options.root) {
303319
type = namespace + type
304-
if (!store._mutations[type]) {
320+
if (process.env.NODE_ENV !== 'production' && !store._mutations[type]) {
305321
console.error(`[vuex] unknown local mutation type: ${args.type}, global type: ${type}`)
306322
return
307323
}
@@ -383,7 +399,7 @@ function registerAction (store, type, handler, local) {
383399
}
384400

385401
function registerGetter (store, type, rawGetter, local) {
386-
if (store._wrappedGetters[type]) {
402+
if (process.env.NODE_ENV !== 'production' && store._wrappedGetters[type]) {
387403
console.error(`[vuex] duplicate getter key: ${type}`)
388404
return
389405
}
@@ -399,7 +415,9 @@ function registerGetter (store, type, rawGetter, local) {
399415

400416
function enableStrictMode (store) {
401417
store._vm.$watch(function () { return this._data.$$state }, () => {
402-
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
418+
if (process.env.NODE_ENV !== 'production') {
419+
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
420+
}
403421
}, { deep: true, sync: true })
404422
}
405423

@@ -416,13 +434,15 @@ function unifyObjectStyle (type, payload, options) {
416434
type = type.type
417435
}
418436

419-
assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
437+
if (process.env.NODE_ENV !== 'production') {
438+
assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
439+
}
420440

421441
return { type, payload, options }
422442
}
423443

424444
export function install (_Vue) {
425-
if (Vue) {
445+
if (process.env.NODE_ENV !== 'production' && Vue) {
426446
console.error(
427447
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
428448
)

0 commit comments

Comments
 (0)