mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +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)
|
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
|
||||||
}
|
}
|
||||||
|
@ -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}`)
|
||||||
|
@ -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',
|
||||||
|
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',
|
layouts: 'custom-layouts',
|
||||||
middleware: 'custom-middleware',
|
middleware: 'custom-middleware',
|
||||||
pages: 'custom-pages',
|
pages: 'custom-pages',
|
||||||
static: 'custom-static'
|
static: 'custom-static',
|
||||||
|
store: 'custom-store'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user