From 39a4cc0b706d5f8d38bc842de27cee6ad1edb7f6 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Wed, 31 Jan 2018 13:07:26 -0500 Subject: [PATCH 01/10] Modified store to accept alternate folder structure --- lib/app/store.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index 94c398254e..8a98b3171f 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -34,10 +34,21 @@ if (typeof storeData !== 'function') { if (name === 'index') continue let namePath = name.split(/\//) + + name = namePath[namePath.length-1] + console.log(namePath) + if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') { + + let module = getModuleNamespace(storeData, namePath, true) + appendModule(module, filename, name) + continue + } + let module = getModuleNamespace(storeData, namePath) name = namePath.pop() - module[name] = getModule(filename) + module[name] = module[name] || {} + module[name] = Object.assign(module[name], getModule(filename)) module[name].namespaced = true } @@ -65,13 +76,20 @@ function getModule (filename) { return module } -function getModuleNamespace (storeData, namePath) { +function getModuleNamespace (storeData, namePath, forAppend = false) { if (namePath.length === 1) { + if (forAppend) + return storeData return storeData.modules } let namespace = namePath.shift() storeData.modules[namespace] = storeData.modules[namespace] || {} storeData.modules[namespace].namespaced = true storeData.modules[namespace].modules = storeData.modules[namespace].modules || {} - return getModuleNamespace(storeData.modules[namespace], namePath) + return getModuleNamespace(storeData.modules[namespace], namePath, forAppend) +} + +function appendModule(module, filename, name) { + const file = files(filename) + module[name] = file.default || file } From c64ee9031196ce579f0c9f4a6d2c7739a53b001a Mon Sep 17 00:00:00 2001 From: airjp73 Date: Wed, 31 Jan 2018 13:14:13 -0500 Subject: [PATCH 02/10] Removed a console.log --- lib/app/store.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index 8a98b3171f..9b28f8e47c 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -36,9 +36,7 @@ if (typeof storeData !== 'function') { let namePath = name.split(/\//) name = namePath[namePath.length-1] - console.log(namePath) if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') { - let module = getModuleNamespace(storeData, namePath, true) appendModule(module, filename, name) continue From 73c1d2aa631e2f4eea26f0007b844442b19ebba6 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Wed, 31 Jan 2018 14:46:57 -0500 Subject: [PATCH 03/10] Tests --- test/basic.csr.test.js | 1 + test/fixtures/basic/pages/store.vue | 7 ++++++- test/fixtures/basic/store/foo/blarg/getters.js | 5 +++++ test/fixtures/basic/store/foo/blarg/state.js | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/basic/store/foo/blarg/getters.js create mode 100644 test/fixtures/basic/store/foo/blarg/state.js diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index eb430a62a0..cda6890a1e 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -80,6 +80,7 @@ test.serial('/store', async t => { t.is(await page.$text('h1'), 'Vuex Nested Modules') t.is(await page.$text('p'), '1') + t.is(await page.$text('h2'), '4') }) test.serial('/head', async t => { diff --git a/test/fixtures/basic/pages/store.vue b/test/fixtures/basic/pages/store.vue index 32fbf8750a..f9efbd1175 100644 --- a/test/fixtures/basic/pages/store.vue +++ b/test/fixtures/basic/pages/store.vue @@ -3,6 +3,8 @@

{{ baz }}


{{ $store.state.counter }}

+
+

{{ getVal }}

@@ -10,6 +12,9 @@ import { mapGetters } from 'vuex' export default { - computed: mapGetters('foo/bar', ['baz']) + computed: { + ...mapGetters('foo/bar', ['baz']), + ...mapGetters('foo/blarg', ['getVal']) + } } diff --git a/test/fixtures/basic/store/foo/blarg/getters.js b/test/fixtures/basic/store/foo/blarg/getters.js new file mode 100644 index 0000000000..34a4b4236a --- /dev/null +++ b/test/fixtures/basic/store/foo/blarg/getters.js @@ -0,0 +1,5 @@ +export default { + getVal(state) { + return state.val + } +} diff --git a/test/fixtures/basic/store/foo/blarg/state.js b/test/fixtures/basic/store/foo/blarg/state.js new file mode 100644 index 0000000000..b5a088b8e3 --- /dev/null +++ b/test/fixtures/basic/store/foo/blarg/state.js @@ -0,0 +1,3 @@ +export default () => ({ + val: 4 +}) From 865d3ffab9b41b62d1ec09170cc50dd75269f704 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Wed, 31 Jan 2018 15:01:13 -0500 Subject: [PATCH 04/10] Clarifying comment --- lib/app/store.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/app/store.js b/lib/app/store.js index 9b28f8e47c..2b93e69600 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -45,6 +45,9 @@ if (typeof storeData !== 'function') { let module = getModuleNamespace(storeData, namePath) name = namePath.pop() + + //use Object.assign to deal with cases where there's a 'myModule.js' and + //a 'myModule' folder containing state/getters/actions/mutations module[name] = module[name] || {} module[name] = Object.assign(module[name], getModule(filename)) module[name].namespaced = true From 965091c8d4402b90addf67d11421091b10220e84 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Wed, 31 Jan 2018 15:22:02 -0500 Subject: [PATCH 05/10] Added support for index.js files --- lib/app/store.js | 5 +++++ test/basic.csr.test.js | 1 + test/fixtures/basic/pages/store.vue | 5 ++++- test/fixtures/basic/store/bab/index.js | 9 +++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/basic/store/bab/index.js diff --git a/lib/app/store.js b/lib/app/store.js index 2b93e69600..127506e66c 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -42,6 +42,11 @@ if (typeof storeData !== 'function') { continue } + //if file is user/index.js + //it should save as user + if (name === 'index') + namePath.pop() + let module = getModuleNamespace(storeData, namePath) name = namePath.pop() diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index cda6890a1e..e5848df54d 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -81,6 +81,7 @@ test.serial('/store', async t => { t.is(await page.$text('h1'), 'Vuex Nested Modules') t.is(await page.$text('p'), '1') t.is(await page.$text('h2'), '4') + t.is(await page.$text('h3'), '10') }) test.serial('/head', async t => { diff --git a/test/fixtures/basic/pages/store.vue b/test/fixtures/basic/pages/store.vue index f9efbd1175..d9d834cd51 100644 --- a/test/fixtures/basic/pages/store.vue +++ b/test/fixtures/basic/pages/store.vue @@ -5,6 +5,8 @@

{{ $store.state.counter }}


{{ getVal }}

+
+

{{ getBabVal }}

@@ -14,7 +16,8 @@ import { mapGetters } from 'vuex' export default { computed: { ...mapGetters('foo/bar', ['baz']), - ...mapGetters('foo/blarg', ['getVal']) + ...mapGetters('foo/blarg', ['getVal']), + ...mapGetters('bab', ['getBabVal']) } } diff --git a/test/fixtures/basic/store/bab/index.js b/test/fixtures/basic/store/bab/index.js new file mode 100644 index 0000000000..babbad5bf2 --- /dev/null +++ b/test/fixtures/basic/store/bab/index.js @@ -0,0 +1,9 @@ +export const state = () => ({ + babVal: 10 +}) + +export const getters = { + getBabVal(state) { + return state.babVal + } +} From 6dce24cc5398d4471b0142c7830296ca93082a00 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Tue, 6 Feb 2018 11:13:28 -0500 Subject: [PATCH 06/10] Fixed conflict resolution problems --- lib/app/store.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index 127506e66c..592731c6ef 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -44,18 +44,29 @@ if (typeof storeData !== 'function') { //if file is user/index.js //it should save as user - if (name === 'index') + var isIndex = (name === 'index') + if (isIndex) namePath.pop() let module = getModuleNamespace(storeData, namePath) - name = namePath.pop() - - //use Object.assign to deal with cases where there's a 'myModule.js' and - //a 'myModule' folder containing state/getters/actions/mutations module[name] = module[name] || {} - module[name] = Object.assign(module[name], getModule(filename)) - module[name].namespaced = true + var fileModule = getModule(filename) + + if (!isIndex) { + module[name] = Object.assign({}, fileModule, module[name]) + module[name].namespaced = true + continue + } + + var appendedMods = [] + if (module[name].appends) { + fileModule.appends = module[name].appends + for (let append of module[name].appends) { + appendedMods[append] = module[name][append] + } + } + module[name] = Object.assign(module[name], fileModule, appendedMods) } } @@ -97,5 +108,7 @@ function getModuleNamespace (storeData, namePath, forAppend = false) { function appendModule(module, filename, name) { const file = files(filename) + module.appends = module.appends || [] + module.appends.push(name) module[name] = file.default || file } From ce342d528a2f3e50dd02689aab46d8ed7eff6401 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Tue, 6 Feb 2018 11:21:33 -0500 Subject: [PATCH 07/10] Reformated/commented for clarity --- lib/app/store.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index 592731c6ef..2658ac30e4 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -42,29 +42,32 @@ if (typeof storeData !== 'function') { continue } - //if file is user/index.js - //it should save as user - var isIndex = (name === 'index') + //if file is foo/index.js + //it should save as foo + let isIndex = (name === 'index') if (isIndex) namePath.pop() - let module = getModuleNamespace(storeData, namePath) + let module = getModuleNamespace(storeData, namePath) + let fileModule = getModule(filename) name = namePath.pop() module[name] = module[name] || {} - var fileModule = getModule(filename) + //if file is foo.js, existing properties take priority + //because it's the least specific case if (!isIndex) { module[name] = Object.assign({}, fileModule, module[name]) module[name].namespaced = true continue } + //if file is foo/index.js we want to overwrite properties from foo.js + //but not from appended mods like foo/actions.js var appendedMods = [] if (module[name].appends) { fileModule.appends = module[name].appends - for (let append of module[name].appends) { + for (let append of module[name].appends) appendedMods[append] = module[name][append] - } } module[name] = Object.assign(module[name], fileModule, appendedMods) } From 413bce4ce4e0dbe55bf7c522e49a6277dd0df037 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Tue, 6 Feb 2018 11:47:41 -0500 Subject: [PATCH 08/10] Test files --- test/fixtures/basic/store/foo/blarg.js | 3 +++ test/fixtures/basic/store/foo/blarg/index.js | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 test/fixtures/basic/store/foo/blarg.js create mode 100644 test/fixtures/basic/store/foo/blarg/index.js diff --git a/test/fixtures/basic/store/foo/blarg.js b/test/fixtures/basic/store/foo/blarg.js new file mode 100644 index 0000000000..f5ebcba296 --- /dev/null +++ b/test/fixtures/basic/store/foo/blarg.js @@ -0,0 +1,3 @@ +export const state = () => ({ + val: 1 +}) diff --git a/test/fixtures/basic/store/foo/blarg/index.js b/test/fixtures/basic/store/foo/blarg/index.js new file mode 100644 index 0000000000..5cfb15ef4b --- /dev/null +++ b/test/fixtures/basic/store/foo/blarg/index.js @@ -0,0 +1,3 @@ +export const state = () => ({ + val: 2 +}) From 0308f13dab956e038fc8e63fd22d7d473d387388 Mon Sep 17 00:00:00 2001 From: airjp73 Date: Tue, 6 Feb 2018 11:47:55 -0500 Subject: [PATCH 09/10] Fixed error caused by typo --- lib/app/store.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/app/store.js b/lib/app/store.js index 2658ac30e4..4ffb913344 100644 --- a/lib/app/store.js +++ b/lib/app/store.js @@ -63,13 +63,15 @@ if (typeof storeData !== 'function') { //if file is foo/index.js we want to overwrite properties from foo.js //but not from appended mods like foo/actions.js - var appendedMods = [] + var appendedMods = {} if (module[name].appends) { - fileModule.appends = module[name].appends + appendedMods.appends = module[name].appends for (let append of module[name].appends) appendedMods[append] = module[name][append] } - module[name] = Object.assign(module[name], fileModule, appendedMods) + + module[name] = Object.assign({}, module[name], fileModule, appendedMods) + module[name].namespaced = true } } From ebd95280df7a907f0146285ae91e6c34978edc7b Mon Sep 17 00:00:00 2001 From: airjp73 Date: Tue, 6 Feb 2018 11:50:09 -0500 Subject: [PATCH 10/10] Added getters to tests --- test/fixtures/basic/store/foo/blarg.js | 6 ++++++ test/fixtures/basic/store/foo/blarg/index.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/test/fixtures/basic/store/foo/blarg.js b/test/fixtures/basic/store/foo/blarg.js index f5ebcba296..e156a53018 100644 --- a/test/fixtures/basic/store/foo/blarg.js +++ b/test/fixtures/basic/store/foo/blarg.js @@ -1,3 +1,9 @@ export const state = () => ({ val: 1 }) + +export const getters = { + getVal(state) { + return 100 + } +} diff --git a/test/fixtures/basic/store/foo/blarg/index.js b/test/fixtures/basic/store/foo/blarg/index.js index 5cfb15ef4b..1777a87beb 100644 --- a/test/fixtures/basic/store/foo/blarg/index.js +++ b/test/fixtures/basic/store/foo/blarg/index.js @@ -1,3 +1,9 @@ export const state = () => ({ val: 2 }) + +export const getters = { + getVal(state) { + return 99 + } +}