1- import { readFileSync } from 'fs'
2-
1+ import setupRoutes from './setupRoutes'
2+ import setupConfig from './setupConfig'
3+ import setupBuild from './setupBuild'
4+ import setupImages from './setupImages'
35const path = require ( 'path' )
4- let seoConfig = require ( 'vuefront/seo' ) . default
56const _ = require ( 'lodash' )
6- const ApolloClient = require ( 'apollo-boost' ) . default
7- require ( 'isomorphic-fetch' )
87const ampify = require ( './plugins/ampify' )
98var fs = require ( 'fs' ) ;
109
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 = / c s s .* : [ \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 = / t h e m e : \' ( .* ) \' / . 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- }
56-
5710export default async function vuefrontModule ( _moduleOptions ) {
58- const isNuxtVersion2 = this . options . build . transpile
5911 const moduleOptions = { ...this . options . vuefront , ..._moduleOptions }
6012
6113 const theme = process . env . VUEFRONT_THEME || 'default'
@@ -95,85 +47,21 @@ export default async function vuefrontModule(_moduleOptions) {
9547 browserBaseURL = moduleOptions . proxy ? prefix : baseURL
9648 }
9749
98- const client = new ApolloClient ( {
99- uri : baseURL
100- } )
101-
102- let whiteList = [ ]
103- let routes = [ ]
104- for ( const url in seoConfig ) {
105- const pageComponent = seoConfig [ url ]
106- if ( _ . isObject ( pageComponent ) ) {
107- if ( ! _ . isUndefined ( pageComponent . generate ) && pageComponent . generate ) {
108- whiteList = [ ...whiteList , url , '/amp' + url ]
109- } else if ( _ . isUndefined ( pageComponent . generate ) && ! url . includes ( ':' ) ) {
110- whiteList = [ ...whiteList , url , '/amp' + url ]
111- }
112- let result = [ ]
113- if ( ! _ . isUndefined ( pageComponent . seo ) ) {
114- let seoResolver = pageComponent . seo
50+ const themeOptions = setupConfig ( this . options . rootDir )
11551
116- result = await seoResolver ( { client } )
117- }
118- routes . push ( {
119- name : url . replace ( '/' , '_' ) . replace ( ':' , '_' ) ,
120- path : url ,
121- component : pageComponent . component
122- } )
123- routes . push ( {
124- name : 'amp_' + url . replace ( '/' , '_' ) . replace ( ':' , '_' ) ,
125- path : '/amp' + url ,
126- component : pageComponent . component
127- } )
128- if ( ! _ . isUndefined ( pageComponent . seo ) && ! _ . isEmpty ( result ) ) {
129- for ( const urlKey in result ) {
130- if ( result [ urlKey ] . keyword !== '' ) {
131- if (
132- ! _ . isUndefined ( pageComponent . generate ) &&
133- pageComponent . generate
134- ) {
135- whiteList = [
136- ...whiteList ,
137- '/' + result [ urlKey ] . keyword ,
138- '/amp/' + result [ urlKey ] . keyword
139- ]
140- } else if ( _ . isUndefined ( pageComponent . generate ) ) {
141- whiteList = [
142- ...whiteList ,
143- '/' + result [ urlKey ] . keyword ,
144- '/amp/' + result [ urlKey ] . keyword
145- ]
146- }
147- routes . push ( {
148- name : result [ urlKey ] . keyword ,
149- path : '/' + result [ urlKey ] . keyword ,
150- component : pageComponent . component ,
151- props : { ...result [ urlKey ] , url }
152- } )
153- routes . push ( {
154- name : 'amp_' + result [ urlKey ] . keyword ,
155- path : '/amp/' + result [ urlKey ] . keyword ,
156- component : pageComponent . component ,
157- props : { ...result [ urlKey ] , url }
158- } )
159- }
160- }
161- }
162- } else {
163- whiteList = [ ...whiteList , url , '/amp' + url ]
164- routes . push ( {
165- name : url . replace ( '/' , '_' ) . replace ( ':' , '_' ) ,
166- path : url ,
167- component : pageComponent . component
168- } )
169- routes . push ( {
170- name : 'amp_' + url . replace ( '/' , '_' ) . replace ( ':' , '_' ) ,
171- path : '/amp' + url ,
172- component : pageComponent . component
173- } )
52+ if ( ! this . options . css ) {
53+ this . options . css = [ ]
54+ }
55+ if ( themeOptions . css ) {
56+ for ( const key in themeOptions . css ) {
57+ this . options . css . push ( themeOptions . css [ key ] )
17458 }
17559 }
17660
61+ const images = setupImages ( themeOptions )
62+
63+ const { routes, whiteList} = await setupRoutes ( baseURL , themeOptions )
64+
17765 const pages = _ . ceil ( routes . length / 500 )
17866
17967 for ( var i = 0 ; i < pages ; i ++ ) {
@@ -182,7 +70,8 @@ export default async function vuefrontModule(_moduleOptions) {
18270 src : path . resolve ( __dirname , './routes.js' ) ,
18371 options : {
18472 routes : _ . slice ( routes , i * 500 , i * 500 + 500 ) ,
185- theme
73+ theme,
74+ themeOptions
18675 }
18776 } )
18877 }
@@ -208,21 +97,12 @@ export default async function vuefrontModule(_moduleOptions) {
20897 src : defaultRouter
20998 } )
21099
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 )
221100
222101 this . addPlugin ( {
223102 fileName : 'vuefront.js' ,
224103 src : path . resolve ( __dirname , './plugin.js' ) ,
225104 options : {
105+ images,
226106 theme,
227107 debug : this . options . dev ,
228108 browserBaseURL,
@@ -249,6 +129,10 @@ export default async function vuefrontModule(_moduleOptions) {
249129 page . html = ampify ( page . html , url )
250130 } )
251131
132+ this . nuxt . hook ( 'build:before' , ( ) => {
133+ setupBuild . call ( this , moduleOptions ) ;
134+ } )
135+
252136 this . extendBuild ( ( config , { isServer } ) => {
253137 const { rules } = config . module
254138
@@ -277,16 +161,5 @@ export default async function vuefrontModule(_moduleOptions) {
277161 }
278162 rules . push ( blockRules )
279163 }
280-
281- const vuefrontRe = 'vuefront'
282- if ( isNuxtVersion2 ) {
283- this . options . build . transpile . push ( vuefrontRe )
284- } else {
285- config . externals = [
286- nodeExternals ( {
287- whitelist : [ vuefrontRe ]
288- } )
289- ]
290- }
291164 } )
292165}
0 commit comments