Skip to content

Commit 9d453fd

Browse files
committed
feat: refactoring
1 parent d41435d commit 9d453fd

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuefront-nuxt",
3-
"version": "0.3.6",
3+
"version": "0.3.7",
44
"description": "vuefront-nuxt",
55
"repository": {
66
"type": "git",

src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ export default async function vuefrontModule(_moduleOptions) {
113113

114114
this.options.generate.routes = whiteList
115115

116+
this.nuxt.hook('generate:routeCreated', async ({route, path, errors}) => {
117+
if(errors.length) {
118+
for (const key in errors) {
119+
const regex = /^Error:\s+([^.]+)/gm;
120+
const m = regex.exec(errors[key].error)
121+
if(m.length) {
122+
console.error(m[1])
123+
} else {
124+
console.error(errors[key].error)
125+
}
126+
}
127+
}
128+
})
129+
116130
this.nuxt.hook('generate:extendRoutes', async routes => {
117131
const routesToGenerate = routes.filter(page =>
118132
whiteList.includes(page.route)

src/setupConfig.js

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const _ = require('lodash')
22

3+
let rootPath = ''
4+
35
const mergeConfig = (objValue, srcValue) => {
46
if (_.isArray(objValue)) {
57
return objValue.concat(srcValue)
@@ -10,6 +12,17 @@ const mergeConfig = (objValue, srcValue) => {
1012
}
1113
}
1214

15+
const checkPath = (path) => {
16+
const newPath = _.replace(path, /^(~)/, rootPath)
17+
try {
18+
require.resolve(newPath)
19+
return true
20+
} catch (e) {
21+
22+
}
23+
return false
24+
}
25+
1326
const convertPath = (config) => {
1427
let result = {}
1528
const keys = ['atoms', 'molecules', 'organisms', 'templates', 'pages', 'loaders', 'extensions']
@@ -18,26 +31,23 @@ const convertPath = (config) => {
1831
continue
1932
}
2033
result[keys[type]] = {}
21-
for(const key in config[keys[type]]) {
22-
try {
23-
require.resolve(config[keys[type]][key])
34+
const category = config[keys[type]]
35+
for(const key in category) {
36+
if(checkPath(category[key])) {
2437
result[keys[type]][key] = {
2538
type: 'full',
26-
path: config[keys[type]][key]
39+
path: category[key]
2740
}
28-
} catch(e) {
29-
try {
30-
require.resolve(config.root.components + '/' +config[keys[type]][key])
31-
result[keys[type]][key] = {
32-
type: 'full',
33-
path: config.root.components + '/' +config[keys[type]][key]
34-
}
35-
} catch(e) {
36-
result[keys[type]][key] = {
37-
type: 'inside',
38-
path: config.root.components,
39-
component: config[keys[type]][key]
40-
}
41+
} else if(checkPath(config.root.components + '/' +category[key])) {
42+
result[keys[type]][key] = {
43+
type: 'full',
44+
path: config.root.components + '/' +category[key]
45+
}
46+
} else {
47+
result[keys[type]][key] = {
48+
type: 'inside',
49+
path: config.root.components,
50+
component: category[key]
4151
}
4252
}
4353
}
@@ -52,33 +62,29 @@ const convertPath = (config) => {
5262
}
5363
continue
5464
}
55-
try {
56-
require.resolve(config.store[key].module)
65+
if(checkPath(config.store[key].module)) {
5766
result.store[key] = {
5867
...config.store[key],
5968
module: {
6069
type: 'full',
6170
path: config.store[key].module
6271
}
6372
}
64-
} catch(e) {
65-
try {
66-
require.resolve(config.root.store + '/' + config.store[key].module)
67-
result.store[key] = {
68-
...config.store[key],
69-
module: {
70-
type: 'full',
71-
path: config.root.store + '/' + config.store[key].module
72-
}
73+
} else if (checkPath(config.root.store + '/' + config.store[key].module)) {
74+
result.store[key] = {
75+
...config.store[key],
76+
module: {
77+
type: 'full',
78+
path: config.root.store + '/' + config.store[key].module
7379
}
74-
} catch(e) {
75-
result.store[key] = {
76-
...config.store[key],
77-
module: {
78-
type: 'inside',
79-
path: config.root.store,
80-
component: config.store[key].module
81-
}
80+
}
81+
} else {
82+
result.store[key] = {
83+
...config.store[key],
84+
module: {
85+
type: 'inside',
86+
path: config.root.store,
87+
component: config.store[key].module
8288
}
8389
}
8490
}
@@ -89,26 +95,23 @@ const convertPath = (config) => {
8995
result.locales = {}
9096
for (const key in config.locales) {
9197
result.locales[key] = []
92-
for (const key2 in config.locales[key]) {
93-
try {
94-
require.resolve(config.locales[key][key2])
98+
const locale = config.locales[key]
99+
for (const key2 in locale) {
100+
if(checkPath(locale[key2])) {
95101
result.locales[key][key2] = {
96102
type: 'full',
97103
path: config.locales[key][key2]
98104
}
99-
} catch(e) {
100-
try {
101-
require.resolve(config.root.locales + '/' + config.locales[key][key2])
102-
result.locales[key][key2] = {
103-
type: 'full',
104-
path: config.root.locales + '/' + config.locales[key][key2]
105-
}
106-
} catch(e) {
107-
result.locales[key][key2] = {
108-
type: 'inside',
109-
path: config.root.locales,
110-
component: config.locales[key][key2]
111-
}
105+
} else if (checkPath(config.root.locales + '/' + config.locales[key][key2])) {
106+
result.locales[key][key2] = {
107+
type: 'full',
108+
path: config.root.locales + '/' + config.locales[key][key2]
109+
}
110+
} else {
111+
result.locales[key][key2] = {
112+
type: 'inside',
113+
path: config.root.locales,
114+
component: config.locales[key][key2]
112115
}
113116
}
114117
}
@@ -121,6 +124,8 @@ const convertPath = (config) => {
121124
export default (rootDir) => {
122125
let themeOptions = require('vuefront').default
123126

127+
rootPath = rootDir
128+
124129
themeOptions = {...themeOptions, ...convertPath(themeOptions)}
125130
let config = require(rootDir + '/vuefront.config').default
126131
config = {...config, ...convertPath(config)}

0 commit comments

Comments
 (0)