Skip to content

Commit 34faa1c

Browse files
committed
Refactoring
1 parent 3be3a00 commit 34faa1c

File tree

4 files changed

+107
-115
lines changed

4 files changed

+107
-115
lines changed

src/index.js

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,16 @@
11
const path = require('path')
2-
let themeOptions = require('vuefront').default
2+
let seoConfig = require('vuefront/seo').default
33
const _ = require('lodash')
44
const ApolloClient = require('apollo-boost').default
55
require('isomorphic-fetch')
66
const ampify = require('./plugins/ampify')
77

8-
const routePath = options => {
9-
if (typeof options['component'] !== 'undefined') {
10-
return `import('${options.component}').then((m) => {
11-
return m.default || m
12-
})`
13-
} else if (typeof options === 'string') {
14-
return `import('${options}').then((m) => {
15-
return m.default || m
16-
})`
17-
} else {
18-
return `import('${options.package}').then((m) => {
19-
return m['${options.el}'].default || m['${options.el}']
20-
})`
21-
}
22-
}
23-
24-
const mergeConfig = (objValue, srcValue) => {
25-
if (_.isArray(objValue)) {
26-
return objValue.concat(srcValue)
27-
} else if (_.isObject(objValue)) {
28-
return _.merge(objValue, srcValue)
29-
} else {
30-
return srcValue
31-
}
32-
}
33-
348
export default async function vuefrontModule(_moduleOptions) {
359
const isNuxtVersion2 = this.options.build.transpile
3610
const moduleOptions = { ...this.options.vuefront, ..._moduleOptions }
11+
12+
const theme = process.env.VUEFRONT_THEME || 'default'
13+
3714
const defaultPort =
3815
process.env.API_PORT ||
3916
process.env.PORT ||
@@ -69,55 +46,35 @@ export default async function vuefrontModule(_moduleOptions) {
6946
browserBaseURL = moduleOptions.proxy ? prefix : baseURL
7047
}
7148

72-
const config = require(this.options.rootDir + '/vuefront.config').default
73-
if (typeof config.theme !== 'undefined') {
74-
const customThemeOptions = require(config.theme).default
75-
themeOptions = _.mergeWith(themeOptions, customThemeOptions, mergeConfig)
76-
}
77-
themeOptions = _.mergeWith(themeOptions, config, mergeConfig)
78-
7949
const client = new ApolloClient({
8050
uri: baseURL
8151
})
8252

8353
let whiteList = []
8454
let routes = []
85-
86-
for (const url in themeOptions.pages) {
87-
const pageComponent = themeOptions.pages[url]
55+
for (const url in seoConfig) {
56+
const pageComponent = seoConfig[url]
8857
if (_.isObject(pageComponent)) {
8958
if (!_.isUndefined(pageComponent.generate) && pageComponent.generate) {
9059
whiteList = [...whiteList, url, '/amp' + url]
91-
} else if(_.isUndefined(pageComponent.generate) && !url.includes(':')) {
60+
} else if (_.isUndefined(pageComponent.generate) && !url.includes(':')) {
9261
whiteList = [...whiteList, url, '/amp' + url]
9362
}
9463
let result = []
9564
if (!_.isUndefined(pageComponent.seo)) {
96-
let seoResolver = ''
97-
if (typeof pageComponent.seo === 'string') {
98-
seoResolver = require(pageComponent.seo).default
99-
} else {
100-
seoResolver = require(pageComponent.seo.package)
101-
if (typeof pageComponent.seo.path === 'string') {
102-
seoResolver = seoResolver[pageComponent.seo.path]
103-
} else {
104-
for (const key in pageComponent.seo.path) {
105-
seoResolver = seoResolver[pageComponent.seo.path[key]]
106-
}
107-
}
108-
}
65+
let seoResolver = pageComponent.seo
10966

11067
result = await seoResolver({ client })
11168
}
11269
routes.push({
11370
name: url.replace('/', '_').replace(':', '_'),
11471
path: url,
115-
component: routePath(pageComponent)
72+
component: pageComponent.component
11673
})
11774
routes.push({
11875
name: 'amp_' + url.replace('/', '_').replace(':', '_'),
11976
path: '/amp' + url,
120-
component: routePath(pageComponent)
77+
component: pageComponent.component
12178
})
12279
if (!_.isUndefined(pageComponent.seo) && !_.isEmpty(result)) {
12380
for (const urlKey in result) {
@@ -141,13 +98,13 @@ export default async function vuefrontModule(_moduleOptions) {
14198
routes.push({
14299
name: result[urlKey].keyword,
143100
path: '/' + result[urlKey].keyword,
144-
component: routePath(pageComponent),
101+
component: pageComponent.component,
145102
props: { ...result[urlKey], url }
146103
})
147104
routes.push({
148105
name: 'amp_' + result[urlKey].keyword,
149106
path: '/amp/' + result[urlKey].keyword,
150-
component: routePath(pageComponent),
107+
component: pageComponent.component,
151108
props: { ...result[urlKey], url }
152109
})
153110
}
@@ -158,12 +115,12 @@ export default async function vuefrontModule(_moduleOptions) {
158115
routes.push({
159116
name: url.replace('/', '_').replace(':', '_'),
160117
path: url,
161-
component: routePath(pageComponent)
118+
component: pageComponent.component
162119
})
163120
routes.push({
164121
name: 'amp_' + url.replace('/', '_').replace(':', '_'),
165122
path: '/amp' + url,
166-
component: routePath(pageComponent)
123+
component: pageComponent.component
167124
})
168125
}
169126
}
@@ -175,7 +132,8 @@ export default async function vuefrontModule(_moduleOptions) {
175132
fileName: `vuefront/routes${i + 1}.js`,
176133
src: path.resolve(__dirname, './routes.js'),
177134
options: {
178-
routes: _.slice(routes, i * 500, i * 500 + 500)
135+
routes: _.slice(routes, i * 500, i * 500 + 500),
136+
theme
179137
}
180138
})
181139
}
@@ -205,7 +163,7 @@ export default async function vuefrontModule(_moduleOptions) {
205163
fileName: 'vuefront.js',
206164
src: path.resolve(__dirname, './plugin.js'),
207165
options: {
208-
vuefrontConfig: themeOptions,
166+
theme,
209167
debug: this.options.dev,
210168
browserBaseURL,
211169
baseURL,
@@ -259,7 +217,7 @@ export default async function vuefrontModule(_moduleOptions) {
259217
rules.push(blockRules)
260218
}
261219

262-
const vuefrontRe = 'vuefront/lib'
220+
const vuefrontRe = 'vuefront'
263221
if (isNuxtVersion2) {
264222
this.options.build.transpile.push(vuefrontRe)
265223
} else {

src/plugin.js

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@ import VueI18n from 'vue-i18n'
33
import ApolloClient from "apollo-boost";
44
import _ from 'lodash'
55
import 'isomorphic-fetch'
6-
<%
7-
const vfresolver = (options) => {
8-
if(typeof options === 'string') {
9-
return `(require('${options}').default ? require('${options}').default:require('${options}'))`
10-
} else if(typeof options.el !== 'undefined') {
11-
return `require('${options.package}')['${options.el}']`
12-
} else if(typeof options.path !== 'undefined') {
13-
let result = `require('${options.package}')`
14-
for (const key in options.path) {
15-
result += `['${options.path[key]}']`
16-
}
17-
return result
6+
import mainConfig from 'vuefront'
7+
import userConfig from '~/vuefront.config'
8+
<% if (options.theme !== 'default' ) { %>
9+
import themeConfig from '<%= options.theme %>'
10+
<% } %>
11+
12+
const mergeConfig = (objValue, srcValue) => {
13+
if (_.isArray(objValue)) {
14+
return objValue.concat(srcValue)
15+
} else if (_.isObject(objValue)) {
16+
return _.merge(objValue, srcValue)
17+
} else {
18+
return srcValue
1819
}
1920
}
20-
%>
21-
<%for (var key in options.vuefrontConfig.css) {%>
22-
import '<%= options.vuefrontConfig.css[key] %>'<%}%>
21+
22+
let themeOptions = mainConfig
23+
24+
if (typeof themeConfig !== 'undefined') {
25+
themeOptions = _.mergeWith(themeOptions, themeConfig, mergeConfig)
26+
}
27+
themeOptions = _.mergeWith(themeOptions, userConfig, mergeConfig)
28+
29+
for (var key in themeOptions.css) {
30+
themeOptions.css[key]()
31+
}
2332

2433
Vue.use(VueI18n)
2534

@@ -54,15 +63,15 @@ export const init = (ctx, inject) => {
5463
function loadLocaleMessages(options) {
5564
const locales = require.context(`~/locales`, true, /\.json$/)
5665
const messages = {}
57-
58-
<% for (var key in options.vuefrontConfig.locales) { %>
59-
if(_.isUndefined(messages['<%=key%>'])) {
60-
messages['<%=key%>'] = {}
66+
67+
for (var key in options.locales) {
68+
if(_.isUndefined(messages[key])) {
69+
messages[key] = {}
70+
}
71+
for (var key2 in options.locales[key]) {
72+
messages[key] = _.merge({}, messages[key], options.locales[key][key2])
73+
}
6174
}
62-
<% for (var key2 in options.vuefrontConfig.locales[key]) { %>
63-
messages['<%=key%>'] = _.merge({}, messages['<%=key%>'], <%= vfresolver(options.vuefrontConfig.locales[key][key2])%>)
64-
<%}%>
65-
<% } %>
6675
locales.keys().forEach(key => {
6776
const local = /^.\/([a-zA-Z-]+)\//.exec(key)[1]
6877
if(_.isUndefined(messages[local])) {
@@ -77,16 +86,10 @@ function loadLocaleMessages(options) {
7786
return messages
7887
}
7988

80-
<%for (var key in options.vuefrontConfig.plugins) {%>
81-
import plugin<%= key %> from '<%= options.vuefrontConfig.plugins[key] %>'
82-
<%}%>
83-
8489
export default async (ctx, inject) => {
90+
8591
init(ctx, inject)
86-
const options = <%= JSON.stringify(options.vuefrontConfig) %>
87-
<%for (var key in options.vuefrontConfig.plugins) {%>
88-
await plugin<%= key %>(ctx)
89-
<%}%>
92+
9093
const components = {
9194
element: {},
9295
template: {},
@@ -101,34 +104,42 @@ export default async (ctx, inject) => {
101104
opts.preserveState = false
102105
}
103106
}
104-
<%for (var key in options.vuefrontConfig.store) {
105-
if (typeof options.vuefrontConfig.store[key].module !== 'undefined') { %>
106-
ctx.store.registerModule(<%= JSON.stringify(options.vuefrontConfig.store[key].path) %>, {namespaced: true, ...<%= vfresolver(options.vuefrontConfig.store[key].module) %>}, opts)<% } else {%>
107-
ctx.store.registerModule(<%= JSON.stringify(options.vuefrontConfig.store[key].path) %>, {namespaced: true}, opts)<% } }%>
108-
109-
<%for (var key in options.vuefrontConfig.atoms) {%>
110-
components['vfA<%= key %>'] = Vue.component('vfA<%= key %>', <%= vfresolver(options.vuefrontConfig.atoms[key]) %>)<%}%>
111-
112-
<%for (var key in options.vuefrontConfig.molecules) {%>
113-
components['vfM<%= key %>'] = Vue.component('vfM<%= key %>', <%= vfresolver(options.vuefrontConfig.molecules[key]) %>)<%}%>
114107

115-
<%for (var key in options.vuefrontConfig.organisms) {%>
116-
components['vfO<%= key %>'] = Vue.component('vfO<%= key %>', <%= vfresolver(options.vuefrontConfig.organisms[key]) %>)<%}%>
108+
for (var key in themeOptions.store) {
109+
if (typeof themeOptions.store[key].module !== 'undefined') {
110+
ctx.store.registerModule(themeOptions.store[key].path, {namespaced: true, ...themeOptions.store[key].module}, opts)
111+
} else {
112+
ctx.store.registerModule(themeOptions.store[key].path, {namespaced: true}, opts)
113+
}
114+
}
117115

118-
<%for (var key in options.vuefrontConfig.templates) {%>
119-
components['vfT<%= key %>'] = Vue.component('vfT<%= key %>', <%= vfresolver(options.vuefrontConfig.templates[key]) %>)<%}%>
116+
for (var key in themeOptions.atoms) {
117+
components[`vfA${key}`] = Vue.component(`vfA${key}`, themeOptions.atoms[key])
118+
}
120119

121-
<%for (var key in options.vuefrontConfig.components) {%>
122-
components['vf<%= key %>'] = Vue.component('vf<%= key %>', <%= vfresolver(options.vuefrontConfig.components[key]) %>)<%}%>
120+
for (var key in themeOptions.molecules) {
121+
components[`vfM${key}`] = Vue.component(`vfM${key}`, themeOptions.molecules[key])
122+
}
123123

124-
<%for (var key in options.vuefrontConfig.modules) {%>
125-
components['vfModule<%= key %>'] = Vue.component('vfModule<%= key %>', <%= vfresolver(options.vuefrontConfig.modules[key]) %>)<%}%>
124+
for (var key in themeOptions.organisms) {
125+
components[`vfO${key}`] = Vue.component(`vfO${key}`, themeOptions.organisms[key])
126+
}
126127

127-
<%for (var key in options.vuefrontConfig.loaders) {%>
128-
components['vfLoader<%= key %>'] = Vue.component('vfLoader<%= key %>', <%= vfresolver(options.vuefrontConfig.loaders[key]) %>)<%}%>
128+
for (var key in themeOptions.templates) {
129+
components[`vfT${key}`] = Vue.component(`vfT${key}`, themeOptions.templates[key])
130+
}
131+
for (var key in themeOptions.components) {
132+
components[`vf${key}`] = Vue.component(`vf${key}`, themeOptions.components[key])
133+
}
134+
for (var key in themeOptions.modules) {
135+
components[`vfModule${key}`] = Vue.component(`vfModule${key}`, themeOptions.modules[key])
136+
}
137+
for (var key in themeOptions.loaders) {
138+
components[`vfLoader${key}`] = Vue.component(`vfLoader${key}`, themeOptions.loaders[key])
139+
}
129140

130141
inject('vuefront', {
131-
options,
142+
options: themeOptions,
132143
components,
133144
baseURL,
134145
get isClient() {
@@ -155,7 +166,7 @@ export default async (ctx, inject) => {
155166

156167
ctx.app.i18n = new VueI18n({
157168
locale: ctx.store.getters['common/language/locale'],
158-
messages: loadLocaleMessages(options)
169+
messages: loadLocaleMessages(themeOptions)
159170
})
160171

161172
}

src/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
23
import Router from 'vue-router'
34
import { createRouter as createDefaultRouter } from './defaultRouter'
45
<% for(var i=0; i < options.pages; i++) {%>

src/routes.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1-
import Vue from 'vue'
1+
import _ from 'lodash'
2+
import mainConfig from 'vuefront'
3+
import userConfig from '~/vuefront.config'
4+
<% if (options.theme != 'default') { %>
5+
import themeConfig from '<%= options.theme %>'
6+
<% } %>
7+
8+
const mergeConfig = (objValue, srcValue) => {
9+
if (_.isArray(objValue)) {
10+
return objValue.concat(srcValue)
11+
} else if (_.isObject(objValue)) {
12+
return _.merge(objValue, srcValue)
13+
} else {
14+
return srcValue
15+
}
16+
}
17+
18+
let themeOptions = mainConfig
19+
if (typeof themeConfig !== 'undefined') {
20+
themeOptions = _.mergeWith(themeOptions, themeConfig, mergeConfig)
21+
}
22+
themeOptions = _.mergeWith(themeOptions, userConfig, mergeConfig)
23+
224
export const getRoutes = () => {
325
return [<% for (var i=0; i < options.routes.length; i++){%>
426
{
527
name: '<%= options.routes[i].name %>',
628
path: '<%= options.routes[i].path %>',<% if(typeof options.routes[i].props !== 'undefined') {%>
729
props: <%= JSON.stringify(options.routes[i].props) %>,<% } %>
830
component: () =>
9-
<%= options.routes[i].component %>
31+
<%= 'themeOptions.pages.'+options.routes[i].component %>
1032
},<% } %>
1133
]
1234
}

0 commit comments

Comments
 (0)