@@ -4,6 +4,7 @@ import * as prettier from "prettier"
4
4
import { IProjectInfo } from "./analyse-project-interface"
5
5
import { md5 } from "./md5"
6
6
import { IConfig } from "./project-config-interface"
7
+ import * as _ from "lodash"
7
8
8
9
interface IEntryInfo {
9
10
pageImporter : string
@@ -13,17 +14,21 @@ interface IEntryInfo {
13
14
notFoundRoute : string
14
15
setEnv : string
15
16
setCustomEnv : string
17
+ storesImporter : string
18
+ storesHelper : string
16
19
}
17
20
18
21
// Entry file content
19
- const entryFileContent = ( entryInfo : IEntryInfo , env : string ) => `
22
+ const getEntryContent = ( entryInfo : IEntryInfo , info : IProjectInfo , env : string ) => `
20
23
import { setEnvLocal, setEnvProd, setCustomEnv } from "pri"
21
24
import * as React from "react"
22
25
import * as ReactDOM from "react-dom"
23
26
import Loadable from "react-loadable"
24
27
import { Redirect, Route, Switch, Router } from "react-router-dom"
25
28
import createBrowserHistory from 'history/createBrowserHistory'
26
29
30
+ ${ entryInfo . storesImporter }
31
+
27
32
const customHistory = createBrowserHistory()
28
33
29
34
${ entryInfo . setEnv }
@@ -50,12 +55,14 @@ const entryFileContent = (entryInfo: IEntryInfo, env: string) => `
50
55
51
56
public render() {
52
57
return (
58
+ ${ info . stores . length > 0 ? "<Provider {...stores}>" : "" }
53
59
<Router history={customHistory}>
54
60
<Switch>
55
61
${ entryInfo . pageRoutes }
56
62
${ entryInfo . notFoundRoute }
57
63
</Switch>
58
64
</Router>
65
+ ${ info . stores . length > 0 ? "</Provider>" : "" }
59
66
)
60
67
}
61
68
}
@@ -66,6 +73,15 @@ const entryFileContent = (entryInfo: IEntryInfo, env: string) => `
66
73
)
67
74
`
68
75
76
+ const getHelperContent = ( entryInfo : IEntryInfo , info : IProjectInfo , env : string ) => `
77
+ /**
78
+ * Do not edit this file.
79
+ * This file is automatic generated to get type help.
80
+ */
81
+
82
+ ${ entryInfo . storesHelper }
83
+ `
84
+
69
85
export async function createEntry ( info : IProjectInfo , projectRootPath : string , env : string , config : IConfig ) {
70
86
const entryInfo : IEntryInfo = {
71
87
pageImporter : "" ,
@@ -74,7 +90,9 @@ export async function createEntry(info: IProjectInfo, projectRootPath: string, e
74
90
notFoundImporter : "" ,
75
91
notFoundRoute : "" ,
76
92
setEnv : "" ,
77
- setCustomEnv : ""
93
+ setCustomEnv : "" ,
94
+ storesImporter : "" ,
95
+ storesHelper : ""
78
96
}
79
97
80
98
// Set env
@@ -103,23 +121,61 @@ export async function createEntry(info: IProjectInfo, projectRootPath: string, e
103
121
104
122
if ( info . routes . length < 2 ) {
105
123
// If only one page, don't need code splitting.
106
- entryInfo . pageImporter += `
107
- import ${ componentName } from "${ path . join ( pathInfo . dir , pathInfo . name ) } "
108
- `
124
+ if ( info . stores . length === 0 ) {
125
+ entryInfo . pageImporter += `
126
+ import ${ componentName } from "${ path . join ( pathInfo . dir , pathInfo . name ) } "
127
+ `
128
+ } else {
129
+ entryInfo . pageImporter += `
130
+ import ${ componentName } Temp from "${ path . join ( pathInfo . dir , pathInfo . name ) } "
131
+ ${ componentName } = Connect()(${ componentName } Temp)
132
+ `
133
+ }
109
134
} else {
135
+ const importCode = info . stores . length === 0 ?
136
+ `import("${ path . join ( pathInfo . dir , pathInfo . name ) } ")` :
137
+ `import("${ path . join ( pathInfo . dir , pathInfo . name ) } ").then(res => Connect()(res.default)) `
138
+
110
139
entryInfo . pageImporter += `
111
140
const ${ componentName } = Loadable({
112
- loader: () => import(" ${ path . join ( pathInfo . dir , pathInfo . name ) } ") ,
141
+ loader: () => ${ importCode } ,
113
142
loading: () => null
114
143
})\n
115
144
`
116
145
}
117
146
118
147
const routeComponent = info . layout ? "LayoutRoute" : "Route"
119
148
120
- entryInfo . pageRoutes += `<${ routeComponent } exact path="${ route . path } " component={${ componentName } } />\n`
149
+ entryInfo . pageRoutes += `
150
+ <${ routeComponent } exact path="${ route . path } " component={${ componentName } } />\n
151
+ `
121
152
} )
122
153
154
+ // Set stores
155
+ const safeName = ( str : string ) => _ . upperFirst ( _ . camelCase ( str ) )
156
+ if ( info . stores . length > 0 ) {
157
+ entryInfo . storesImporter += `import { useStrict } from "dob"\n`
158
+ entryInfo . storesImporter += `import { Connect, Provider } from "dob-react"\n`
159
+ entryInfo . storesImporter += `useStrict()\n`
160
+ entryInfo . storesImporter += `import { stores } from "../src/helper"\n`
161
+ entryInfo . storesHelper += `import { combineStores } from "dob"\n`
162
+ entryInfo . storesHelper += info . stores
163
+ . map ( eachStore => {
164
+ const filePath = path . parse ( eachStore . filePath )
165
+ const importAbsolutePath = path . join ( filePath . dir , filePath . name )
166
+ const importRelativePath = path . relative ( path . join ( projectRootPath , "src" ) , importAbsolutePath )
167
+ return `import { ${ safeName ( eachStore . name ) } Action, ${ safeName ( eachStore . name ) } Store } from "./${ importRelativePath } "`
168
+ } )
169
+ . join ( "\n" )
170
+ entryInfo . storesHelper += `
171
+ \nconst stores = combineStores({${ info . stores . map ( eachStore => {
172
+ return `${ safeName ( eachStore . name ) } Action, ${ safeName ( eachStore . name ) } Store`
173
+ } ) . join ( ',' ) } })
174
+
175
+ export { stores }
176
+ `
177
+ }
178
+
123
179
// Set layout
124
180
if ( info . layout ) {
125
181
const layoutPath = path . parse ( info . layout . filePath )
@@ -149,10 +205,21 @@ export async function createEntry(info: IProjectInfo, projectRootPath: string, e
149
205
150
206
// Create entry tsx file
151
207
const entryPath = path . join ( projectRootPath , ".temp/entry.tsx" )
152
- fs . outputFileSync ( entryPath , prettier . format ( entryFileContent ( entryInfo , env ) , {
208
+ fs . outputFileSync ( entryPath , prettier . format ( getEntryContent ( entryInfo , info , env ) , {
153
209
semi : false ,
154
210
parser : "typescript"
155
211
} ) )
156
212
213
+ // If has stores, create helper.ts
214
+ const helperPath = path . join ( projectRootPath , "src/helper.ts" )
215
+ if ( info . stores . length > 0 ) {
216
+ fs . outputFileSync ( helperPath , prettier . format ( getHelperContent ( entryInfo , info , env ) , {
217
+ semi : false ,
218
+ parser : "typescript"
219
+ } ) )
220
+ } else {
221
+ fs . removeSync ( helperPath )
222
+ }
223
+
157
224
return entryPath
158
225
}
0 commit comments