mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat: custom store directory
This commit is contained in:
parent
856c1bf83c
commit
7786451064
@ -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
|
||||
}
|
||||
|
@ -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}`)
|
||||
|
@ -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',
|
||||
|
17
test/fixtures/custom-dirs/custom-store/index.js
vendored
Normal file
17
test/fixtures/custom-dirs/custom-store/index.js
vendored
Normal 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
|
3
test/fixtures/custom-dirs/nuxt.config.js
vendored
3
test/fixtures/custom-dirs/nuxt.config.js
vendored
@ -5,6 +5,7 @@ module.exports = {
|
||||
layouts: 'custom-layouts',
|
||||
middleware: 'custom-middleware',
|
||||
pages: 'custom-pages',
|
||||
static: 'custom-static'
|
||||
static: 'custom-static',
|
||||
store: 'custom-store'
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user