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)
// Recursive find files in {srcDir}/store
const files = require.context('@/store', true, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/)
// Recursive find files in {srcDir}/{dir.store}
const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/)
const filenames = files.keys()
// Store
let storeData = {}
// Check if store/index.js exists
// Check if {dir.store}/index.js exists
let indexFilename
filenames.forEach((filename) => {
if (filename.indexOf('./index.') !== -1) {
@ -57,10 +57,10 @@ function getModule (filename) {
const file = files(filename)
const module = file.default || file
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') {
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
}

View File

@ -641,7 +641,7 @@ module.exports = class Builder {
const src = this.options.srcDir
let patterns = [
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.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 (options.store !== false && existsSync(join(options.srcDir, 'store'))) {
if (options.store !== false && existsSync(join(options.srcDir, options.dir.store))) {
options.store = true
}
@ -289,7 +289,8 @@ Options.defaults = {
layouts: 'layouts',
middleware: 'middleware',
pages: 'pages',
static: 'static'
static: 'static',
store: 'store'
},
router: {
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',
middleware: 'custom-middleware',
pages: 'custom-pages',
static: 'custom-static'
static: 'custom-static',
store: 'custom-store'
}
}