Skip to content

Commit 216c4cd

Browse files
committed
fix: fix breadcrumbs
1 parent 7c276e9 commit 216c4cd

File tree

5 files changed

+438
-378
lines changed

5 files changed

+438
-378
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"typings": "types/index.d.ts",
1919
"peerDependencies": {},
2020
"dependencies": {
21-
"@types/node": "^12.7.2",
22-
"apollo-boost": "^0.4.4",
23-
"cookie-universal": "^2.0.16",
24-
"graphql": "^14.5.3",
21+
"@types/node": "^13.7.4",
22+
"apollo-boost": "^0.4.7",
23+
"cookie-universal": "^2.1.1",
24+
"graphql": "^14.6.0",
2525
"isomorphic-fetch": "^2.2.1",
2626
"lodash": "^4.17.15",
27-
"vue": "^2.6.10",
27+
"vue": "^2.6.11",
2828
"vue-graphql-loader": "^0.3.3",
29-
"vue-i18n": "^8.14.0",
30-
"vuefront": "^0.2.2"
29+
"vue-i18n": "^8.15.3",
30+
"vuefront": "^0.2.4"
3131
}
3232
}

src/index.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
1+
import { readFileSync } from 'fs'
2+
13
const path = require('path')
24
let seoConfig = require('vuefront/seo').default
35
const _ = require('lodash')
46
const ApolloClient = require('apollo-boost').default
57
require('isomorphic-fetch')
68
const ampify = require('./plugins/ampify')
9+
var fs = require('fs');
10+
11+
const readConfigFile = (path) => {
12+
const result = {}
13+
try {
14+
var filename = require.resolve(path);
15+
const configContent = fs.readFileSync(filename, 'utf8');
16+
let matched = /css.*:[\s\n\r]+\{([^{]+)\}/.exec(configContent)
17+
if(!_.isEmpty(matched)) {
18+
const cssConfig =
19+
matched[1]
20+
.replace(/\s\n/, '')
21+
.split(',')
22+
.map((str) =>
23+
str
24+
.split(':')
25+
.map(strV =>
26+
strV
27+
.trim()
28+
.replace(/\'/g, '')
29+
)
30+
).reduce((accumulator, currentValue) => {
31+
accumulator[currentValue[0]] = currentValue[1]
32+
return accumulator
33+
}, {})
34+
35+
result.css = cssConfig
36+
}
37+
matched = /theme: \'(.*)\'/.exec(configContent)
38+
if(!_.isEmpty(matched)) {
39+
result.theme = matched[1]
40+
}
41+
} catch (e) {
42+
}
43+
44+
return result
45+
}
46+
47+
const mergeConfig = (objValue, srcValue) => {
48+
if (_.isArray(objValue)) {
49+
return objValue.concat(srcValue)
50+
} else if (_.isObject(objValue)) {
51+
return _.merge(objValue, srcValue)
52+
} else {
53+
return srcValue
54+
}
55+
}
756

857
export default async function vuefrontModule(_moduleOptions) {
958
const isNuxtVersion2 = this.options.build.transpile
@@ -159,6 +208,17 @@ export default async function vuefrontModule(_moduleOptions) {
159208
src: defaultRouter
160209
})
161210

211+
let themeOptions = readConfigFile('vuefront')
212+
213+
const config = readConfigFile(this.options.rootDir + '/vuefront.config.js')
214+
215+
if (typeof config.theme !== 'undefined') {
216+
const customThemeOptions = readConfigFile(config.theme)
217+
themeOptions = _.mergeWith(themeOptions, customThemeOptions, mergeConfig)
218+
}
219+
220+
themeOptions = _.mergeWith(themeOptions, config, mergeConfig)
221+
162222
this.addPlugin({
163223
fileName: 'vuefront.js',
164224
src: path.resolve(__dirname, './plugin.js'),
@@ -167,7 +227,8 @@ export default async function vuefrontModule(_moduleOptions) {
167227
debug: this.options.dev,
168228
browserBaseURL,
169229
baseURL,
170-
pages
230+
pages,
231+
themeOptions
171232
}
172233
})
173234

src/plugin.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import userConfig from '~/vuefront.config'
88
<% if (options.theme !== 'default' ) { %>
99
import themeConfig from '<%= options.theme %>'
1010
<% } %>
11+
<% if (typeof options.themeOptions.css !== 'undefined' ) { %>
12+
<% for (const key in options.themeOptions.css) { %>
13+
<% if (options.themeOptions.css[key] !== 'null' ) { %>
14+
import '<%= options.themeOptions.css[key] %>'
15+
<% } %>
16+
<% } %>
17+
<% } %>
18+
1119

1220
const mergeConfig = (objValue, srcValue) => {
1321
if (_.isArray(objValue)) {
@@ -26,10 +34,6 @@ if (typeof themeConfig !== 'undefined') {
2634
}
2735
themeOptions = _.mergeWith(themeOptions, userConfig, mergeConfig)
2836

29-
for (var key in themeOptions.css) {
30-
themeOptions.css[key]()
31-
}
32-
3337
Vue.use(VueI18n)
3438

3539
const baseURL = process.browser
@@ -43,6 +47,16 @@ export const init = (ctx, inject) => {
4347
accept: 'application/json; charset=UTF-8',
4448
'content-type': 'application/json; charset=UTF-8'
4549
},
50+
onError: (error) => {
51+
if (error.graphQLErrors) {
52+
console.log('ApolloClient graphQLErrors')
53+
console.log(error.graphQLErrors)
54+
}
55+
if (error.networkError) {
56+
console.log('ApolloClient networkError')
57+
console.log(error.networkError.bodyText)
58+
}
59+
},
4660
request: (operation) => {
4761
operation.setContext({
4862
fetchOptions: {

src/routes.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ const mergeConfig = (objValue, srcValue) => {
1515
}
1616
}
1717

18+
const breadcrumbsLoad = (component) => {
19+
component.serverPrefetch = function() {
20+
return new Promise(async (resolve) => {
21+
this.$store.dispatch('common/breadcrumbs/init');
22+
if(this.handleLoadData) {
23+
await this.handleLoadData(this)
24+
}
25+
await this.$store.dispatch('common/breadcrumbs/load');
26+
resolve()
27+
})
28+
}
29+
component.created = function() {
30+
if (typeof this.loaded !== 'undefined') {
31+
if(!this.loaded) {
32+
this.$store.dispatch('common/breadcrumbs/init');
33+
this.$watch('loaded', () => {
34+
this.$store.dispatch('common/breadcrumbs/load');
35+
})
36+
}
37+
} else {
38+
this.$store.dispatch('common/breadcrumbs/load');
39+
}
40+
}
41+
}
42+
1843
let themeOptions = mainConfig
1944
if (typeof themeConfig !== 'undefined') {
2045
themeOptions = _.mergeWith(themeOptions, themeConfig, mergeConfig)
@@ -29,18 +54,9 @@ export const getRoutes = () => {
2954
props: <%= JSON.stringify(options.routes[i].props) %>,
3055
<% } %>
3156
component: () => {
32-
<%= 'themeOptions.pages.'+options.routes[i].component %>.created = function() {
33-
if (typeof this.loaded !== 'undefined') {
34-
this.$store.dispatch('common/breadcrumbs/init');
35-
this.$watch('loaded', () => {
36-
this.$store.dispatch('common/breadcrumbs/load');
37-
})
38-
} else {
39-
this.$store.dispatch('common/breadcrumbs/load');
40-
41-
}
42-
}
43-
return <%= 'themeOptions.pages.'+options.routes[i].component %>
57+
const component = <%= 'themeOptions.pages.'+options.routes[i].component %>
58+
breadcrumbsLoad(component)
59+
return component
4460
}
4561

4662
}, <% } %>]

0 commit comments

Comments
 (0)