@@ -14,6 +14,7 @@ const {
1414 cacheFileInfo,
1515 getFileInfo,
1616 getCompNameBySrc,
17+ resolveEntry,
1718 resolveTarget,
1819 covertCCVar,
1920 cacheSlots,
@@ -129,17 +130,19 @@ function compileMPScript (script, mpOptioins, moduleId) {
129130
130131 // 处理子组件的信息
131132 const components = { }
132- const fileInfo = resolveTarget ( this . resourcePath , this . options . entry )
133- if ( originComponents ) {
134- resolveSrc ( originComponents , components , this . resolve , this . context ) . then ( ( ) => {
135- resolveComponent ( this . resourcePath , fileInfo , importsMap , components , moduleId )
136- } ) . catch ( err => {
137- console . error ( err )
133+ resolveEntry ( this . options . entry ) . then ( ( entry ) => {
134+ const fileInfo = resolveTarget ( this . resourcePath , entry )
135+ if ( originComponents ) {
136+ resolveSrc ( originComponents , components , this . resolve , this . context ) . then ( ( ) => {
137+ resolveComponent ( this . resourcePath , fileInfo , importsMap , components , moduleId )
138+ } ) . catch ( err => {
139+ console . error ( err )
140+ resolveComponent ( this . resourcePath , fileInfo , importsMap , components , moduleId )
141+ } )
142+ } else {
138143 resolveComponent ( this . resourcePath , fileInfo , importsMap , components , moduleId )
139- } )
140- } else {
141- resolveComponent ( this . resourcePath , fileInfo , importsMap , components , moduleId )
142- }
144+ }
145+ } )
143146
144147 return script
145148}
@@ -152,88 +155,90 @@ let globalComponents
152155function compileMP ( content , mpOptioins ) {
153156 const { resourcePath, emitError, emitFile, emitWarning, resolve, context, options } = this
154157
155- const fileInfo = resolveTarget ( resourcePath , options . entry )
156- cacheFileInfo ( resourcePath , fileInfo )
157- const { src, name, isApp, isPage } = fileInfo
158- if ( isApp ) {
159- // 解析前将可能存在的全局组件清空
160- clearGlobalComponents ( )
161- }
158+ resolveEntry ( options . entry ) . then ( ( entry ) => {
159+ const fileInfo = resolveTarget ( resourcePath , entry )
160+ cacheFileInfo ( resourcePath , fileInfo )
161+ const { src, name, isApp, isPage } = fileInfo
162+ if ( isApp ) {
163+ // 解析前将可能存在的全局组件清空
164+ clearGlobalComponents ( )
165+ }
162166
163- const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
164- // app入口进行全局component解析
165- const { metadata } = babel . transform ( content , { extends : babelrc , plugins : isApp ? [ parseConfig , parseGlobalComponents ] : [ parseConfig ] } )
166-
167- // metadata: config
168- const { config, rootComponent, globalComponents : globalComps } = metadata
169-
170- if ( isApp ) {
171- // 保存旧数据,用于对比
172- const oldGlobalComponents = globalComponents
173- // 开始解析组件路径时把全局组件清空,解析完成后再进行赋值,标志全局组件解析完成
174- globalComponents = null
175-
176- // 解析全局组件的路径
177- const components = { }
178- resolveSrc ( globalComps , components , resolve , context ) . then ( ( ) => {
179- handleResult ( components )
180- } ) . catch ( err => {
181- console . error ( err )
182- handleResult ( components )
183- } )
184- const handleResult = components => {
185- globalComponents = components
186- // 热更时,如果全局组件更新,需要重新生成所有的wxml
187- if ( oldGlobalComponents && ! deepEqual ( oldGlobalComponents , globalComponents ) ) {
188- // 更新所有页面的组件
189- Object . keys ( cacheResolveComponents ) . forEach ( k => {
190- resolveComponent ( ...cacheResolveComponents [ k ] )
191- } )
192- // 重新生成所有wxml
193- Object . keys ( cacheCreateWxmlFns ) . forEach ( k => {
194- createWxml ( ...cacheCreateWxmlFns [ k ] )
195- } )
167+ const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
168+ // app入口进行全局component解析
169+ const { metadata } = babel . transform ( content , { extends : babelrc , plugins : isApp ? [ parseConfig , parseGlobalComponents ] : [ parseConfig ] } )
170+
171+ // metadata: config
172+ const { config, rootComponent, globalComponents : globalComps } = metadata
173+
174+ if ( isApp ) {
175+ // 保存旧数据,用于对比
176+ const oldGlobalComponents = globalComponents
177+ // 开始解析组件路径时把全局组件清空,解析完成后再进行赋值,标志全局组件解析完成
178+ globalComponents = null
179+
180+ // 解析全局组件的路径
181+ const components = { }
182+ resolveSrc ( globalComps , components , resolve , context ) . then ( ( ) => {
183+ handleResult ( components )
184+ } ) . catch ( err => {
185+ console . error ( err )
186+ handleResult ( components )
187+ } )
188+ const handleResult = components => {
189+ globalComponents = components
190+ // 热更时,如果全局组件更新,需要重新生成所有的wxml
191+ if ( oldGlobalComponents && ! deepEqual ( oldGlobalComponents , globalComponents ) ) {
192+ // 更新所有页面的组件
193+ Object . keys ( cacheResolveComponents ) . forEach ( k => {
194+ resolveComponent ( ...cacheResolveComponents [ k ] )
195+ } )
196+ // 重新生成所有wxml
197+ Object . keys ( cacheCreateWxmlFns ) . forEach ( k => {
198+ createWxml ( ...cacheCreateWxmlFns [ k ] )
199+ } )
200+ }
196201 }
197202 }
198- }
199203
200- if ( isApp || isPage ) {
201- // 生成入口 json
202- if ( config ) {
203- const configObj = config . value
204-
205- // 只有 app 才处理 pages
206- if ( isApp ) {
207- const pages = Object . keys ( options . entry ) . concat ( configObj . pages ) . filter ( v => v && v !== 'app' ) . map ( getPageSrc )
208-
209- // ^ 开头的放在第一个
210- const startPageIndex = pages . findIndex ( v => startPageReg . test ( v ) )
211- if ( startPageIndex !== - 1 ) {
212- const startPage = pages [ startPageIndex ] . slice ( 1 )
213- pages . splice ( startPageIndex , 1 )
214- pages . unshift ( startPage )
204+ if ( isApp || isPage ) {
205+ // 生成入口 json
206+ if ( config ) {
207+ const configObj = config . value
208+
209+ // 只有 app 才处理 pages
210+ if ( isApp ) {
211+ const pages = Object . keys ( entry ) . concat ( configObj . pages ) . filter ( v => v && v !== 'app' ) . map ( getPageSrc )
212+
213+ // ^ 开头的放在第一个
214+ const startPageIndex = pages . findIndex ( v => startPageReg . test ( v ) )
215+ if ( startPageIndex !== - 1 ) {
216+ const startPage = pages [ startPageIndex ] . slice ( 1 )
217+ pages . splice ( startPageIndex , 1 )
218+ pages . unshift ( startPage )
219+ }
220+ configObj . pages = [ ...new Set ( pages ) ]
221+ emitFile ( `${ src } .json` , JSON . stringify ( configObj , null , ' ' ) )
215222 }
216- configObj . pages = [ ...new Set ( pages ) ]
217223 }
218- emitFile ( `${ src } .json` , JSON . stringify ( configObj , null , ' ' ) )
219- }
220224
221- // 生成入口 js
222- emitFile ( `${ src } .js` , genScript ( name , isPage , src ) )
225+ // 生成入口 js
226+ emitFile ( `${ src } .js` , genScript ( name , isPage , src ) )
223227
224- // 生成入口 wxss
225- emitFile ( `${ src } .wxss` , genStyle ( name , isPage , src ) )
228+ // 生成入口 wxss
229+ emitFile ( `${ src } .wxss` , genStyle ( name , isPage , src ) )
226230
227- // 这儿应该异步在所有的模块都清晰后再生成
228- // 生成入口 wxml
229- if ( isPage && rootComponent ) {
230- resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
231- if ( err ) return
232- // 这儿需要搞定 根组件的 路径
233- createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponentSrc )
234- } )
231+ // 这儿应该异步在所有的模块都清晰后再生成
232+ // 生成入口 wxml
233+ if ( isPage && rootComponent ) {
234+ resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
235+ if ( err ) return
236+ // 这儿需要搞定 根组件的 路径
237+ createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponentSrc )
238+ } )
239+ }
235240 }
236- }
241+ } )
237242
238243 return content
239244}
0 commit comments