@@ -5,22 +5,22 @@ const babel = require('babel-core')
55const path = require ( 'path' )
66const fs = require ( 'fs' )
77const deepEqual = require ( 'deep-equal' )
8+ const relative = require ( '../utils/relative' )
89
910const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require ( './parse' )
1011const { parseComponentsDeps : parseComponentsDepsTs } = require ( './parse-ts' )
11- const { genScript , genStyle , genPageWxml } = require ( './templates' )
12+ const { genPageWxml } = require ( './templates' )
1213
1314const {
1415 cacheFileInfo,
1516 getFileInfo,
16- getCompNameBySrc ,
17+ getCompNameAndSrc ,
1718 resolveTarget,
1819 covertCCVar,
1920 cacheSlots,
2021 getSlots,
2122 htmlBeautify,
22- getBabelrc,
23- getPageSrc
23+ getBabelrc
2424} = require ( './util' )
2525
2626let slotsHookAdded = false
@@ -48,17 +48,16 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
4848 return htmlBeautify ( wxmlCodeStr )
4949}
5050
51- function createAppWxml ( emitFile , resourcePath , rootComponent ) {
51+ function createAppWxml ( emitFile , resourcePath , rootComponent , context ) {
5252 const { src } = getFileInfo ( resourcePath ) || { }
53- const componentName = getCompNameBySrc ( rootComponent )
54- const wxmlContent = genPageWxml ( componentName , src )
55- const wxmlSrc = src
56- emitFile ( `${ wxmlSrc } .wxml` , wxmlContent )
53+ const { name : componentName , filePath : wxmlSrc } = getCompNameAndSrc ( context , rootComponent )
54+ const wxmlContent = genPageWxml ( componentName , relative ( `/${ src } .wxml` , `/${ wxmlSrc } ` ) )
55+ emitFile ( `${ src } .wxml` , wxmlContent )
5756}
5857// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
5958const cacheCreateWxmlFns = { }
6059
61- function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
60+ function createWxml ( { emitWarning, emitError, emitFile, resourcePath, context , compiled } ) {
6261 cacheCreateWxmlFns [ resourcePath ] = arguments
6362 const { pageType, moduleId, components } = getFileInfo ( resourcePath ) || { }
6463
@@ -71,12 +70,10 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone
7170 // name: 'comA$hash',
7271 // moduleId: 'moduleId'
7372 // }
74- const name = getCompNameBySrc ( resourcePath )
73+ const { name, filePath : wxmlSrc } = getCompNameAndSrc ( context , resourcePath )
7574 const options = { components, pageType, name, moduleId }
7675 const wxmlContent = genComponentWxml ( compiled , options , emitFile , emitError , emitWarning )
77- const wxmlSrc = `components/${ name } `
78-
79- emitFile ( `${ wxmlSrc } .wxml` , wxmlContent )
76+ emitFile ( wxmlSrc , wxmlContent )
8077}
8178
8279// 编译出 wxml
@@ -106,7 +103,15 @@ function compileWxml (compiled, html) {
106103 pollComponentsStatus ( )
107104 } )
108105 . then ( ( ) => {
109- createWxml ( this . emitWarning , this . emitError , this . emitFile , this . resourcePath , null , compiled , html )
106+ createWxml ( {
107+ emitWarning : this . emitWarning ,
108+ emitError : this . emitError ,
109+ emitFile : this . emitFile ,
110+ resourcePath : this . resourcePath ,
111+ context : this . options . context ,
112+ rootComponent : null ,
113+ compiled, html
114+ } )
110115 } )
111116}
112117
@@ -151,14 +156,13 @@ function compileMPScript (script, mpOptioins, moduleId) {
151156// checkMPEntry 针对 entry main.js 的入口处理
152157// 编译出 app, page 的入口js/wxml/json
153158
154- const startPageReg = / ^ \^ /
155159let globalComponents
156160function compileMP ( content , mpOptioins ) {
157161 const { resourcePath, emitFile, resolve, context, options } = this
158162
159163 const fileInfo = resolveTarget ( resourcePath , options . entry )
160164 cacheFileInfo ( resourcePath , fileInfo )
161- const { src , name , isApp, isPage } = fileInfo
165+ const { isApp, isPage } = fileInfo
162166 if ( isApp ) {
163167 // 解析前将可能存在的全局组件清空
164168 clearGlobalComponents ( )
@@ -169,7 +173,7 @@ function compileMP (content, mpOptioins) {
169173 const { metadata } = babel . transform ( content , { extends : babelrc , plugins : isApp ? [ parseConfig , parseGlobalComponents ] : [ parseConfig ] } )
170174
171175 // metadata: config
172- const { config , rootComponent, globalComponents : globalComps } = metadata
176+ const { rootComponent, globalComponents : globalComps } = metadata
173177
174178 if ( isApp ) {
175179 // 保存旧数据,用于对比
@@ -202,39 +206,13 @@ function compileMP (content, mpOptioins) {
202206 }
203207
204208 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 ( options . 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- }
222- emitFile ( `${ src } .json` , JSON . stringify ( configObj , null , ' ' ) )
223- }
224-
225- // 生成入口 js
226- emitFile ( `${ src } .js` , genScript ( name , isPage , src ) )
227-
228- // 生成入口 wxss
229- emitFile ( `${ src } .wxss` , genStyle ( name , isPage , src ) )
230-
231209 // 这儿应该异步在所有的模块都清晰后再生成
232210 // 生成入口 wxml
233211 if ( isPage && rootComponent ) {
234212 resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
235213 if ( err ) return
236214 // 这儿需要搞定 根组件的 路径
237- createAppWxml ( emitFile , resourcePath , rootComponentSrc )
215+ createAppWxml ( emitFile , resourcePath , rootComponentSrc , this . options . context )
238216 } )
239217 }
240218 }
@@ -248,8 +226,8 @@ function resolveSrc (originComponents, components, resolveFn, context) {
248226 resolveFn ( context , originComponents [ k ] , ( err , realSrc ) => {
249227 if ( err ) return reject ( err )
250228 const com = covertCCVar ( k )
251- const comName = getCompNameBySrc ( realSrc )
252- components [ com ] = { src : comName , name : comName }
229+ const { filePath , name } = getCompNameAndSrc ( context , realSrc )
230+ components [ com ] = { src : filePath , name }
253231 resolve ( )
254232 } )
255233 } )
0 commit comments