feat: custom store directory

This commit is contained in:
Ricardo Gobbo de Souza 2018-02-04 07:31:03 -02:00
parent 856c1bf83c
commit 7786451064
5 changed files with 28 additions and 9 deletions

View File

@ -3,14 +3,14 @@ import Vuex from 'vuex'
Vue.use(Vuex) Vue.use(Vuex)
// Recursive find files in {srcDir}/store // Recursive find files in {srcDir}/{dir.store}
const files = require.context('@/store', true, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/) const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/)
const filenames = files.keys() const filenames = files.keys()
// Store // Store
let storeData = {} let storeData = {}
// Check if store/index.js exists // Check if {dir.store}/index.js exists
let indexFilename let indexFilename
filenames.forEach((filename) => { filenames.forEach((filename) => {
if (filename.indexOf('./index.') !== -1) { if (filename.indexOf('./index.') !== -1) {
@ -57,10 +57,10 @@ function getModule (filename) {
const file = files(filename) const file = files(filename)
const module = file.default || file const module = file.default || file
if (module.commit) { if (module.commit) {
throw new Error('[nuxt] store/' + filename.replace('./', '') + ' should export a method which returns a Vuex instance.') throw new Error('[nuxt] <%= dir.store %>/' + filename.replace('./', '') + ' should export a method which returns a Vuex instance.')
} }
if (module.state && typeof module.state !== 'function') { if (module.state && typeof module.state !== 'function') {
throw new Error('[nuxt] state should be a function in store/' + filename.replace('./', '')) throw new Error('[nuxt] state should be a function in <%= dir.store %>/' + filename.replace('./', ''))
} }
return module return module
} }

View File

@ -641,7 +641,7 @@ module.exports = class Builder {
const src = this.options.srcDir const src = this.options.srcDir
let patterns = [ let patterns = [
r(src, this.options.dir.layouts), r(src, this.options.dir.layouts),
r(src, 'store'), r(src, this.options.dir.store),
r(src, this.options.dir.middleware), r(src, this.options.dir.middleware),
r(src, `${this.options.dir.layouts}/*.{vue,js}`), r(src, `${this.options.dir.layouts}/*.{vue,js}`),
r(src, `${this.options.dir.layouts}/**/*.{vue,js}`) r(src, `${this.options.dir.layouts}/**/*.{vue,js}`)

View File

@ -85,7 +85,7 @@ Options.from = function (_options) {
} }
// If store defined, update store options to true unless explicitly disabled // If store defined, update store options to true unless explicitly disabled
if (options.store !== false && existsSync(join(options.srcDir, 'store'))) { if (options.store !== false && existsSync(join(options.srcDir, options.dir.store))) {
options.store = true options.store = true
} }
@ -289,7 +289,8 @@ Options.defaults = {
layouts: 'layouts', layouts: 'layouts',
middleware: 'middleware', middleware: 'middleware',
pages: 'pages', pages: 'pages',
static: 'static' static: 'static',
store: 'store'
}, },
router: { router: {
mode: 'history', mode: 'history',

View File

@ -0,0 +1,17 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = () => new Vuex.Store({
state: {
counter: 0
},
mutations: {
increment(state) {
state.counter++
}
}
})
export default store

View File

@ -5,6 +5,7 @@ module.exports = {
layouts: 'custom-layouts', layouts: 'custom-layouts',
middleware: 'custom-middleware', middleware: 'custom-middleware',
pages: 'custom-pages', pages: 'custom-pages',
static: 'custom-static' static: 'custom-static',
store: 'custom-store'
} }
} }