@@ -5,22 +5,22 @@ const babel = require('babel-core')
5
5
const path = require ( 'path' )
6
6
const fs = require ( 'fs' )
7
7
const deepEqual = require ( 'deep-equal' )
8
+ const relative = require ( 'relative' )
8
9
9
10
const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require ( './parse' )
10
11
const { parseComponentsDeps : parseComponentsDepsTs } = require ( './parse-ts' )
11
- const { genScript , genStyle , genPageWxml } = require ( './templates' )
12
+ const { genPageWxml } = require ( './templates' )
12
13
13
14
const {
14
15
cacheFileInfo,
15
16
getFileInfo,
16
- getCompNameBySrc ,
17
+ getCompNameAndSrc ,
17
18
resolveTarget,
18
19
covertCCVar,
19
20
cacheSlots,
20
21
getSlots,
21
22
htmlBeautify,
22
- getBabelrc,
23
- getPageSrc
23
+ getBabelrc
24
24
} = require ( './util' )
25
25
26
26
let slotsHookAdded = false
@@ -48,17 +48,16 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
48
48
return htmlBeautify ( wxmlCodeStr )
49
49
}
50
50
51
- function createAppWxml ( emitFile , resourcePath , rootComponent ) {
51
+ function createAppWxml ( emitFile , resourcePath , rootComponent , context ) {
52
52
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 )
57
56
}
58
57
// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
59
58
const cacheCreateWxmlFns = { }
60
59
61
- function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
60
+ function createWxml ( { emitWarning, emitError, emitFile, resourcePath, context , compiled } ) {
62
61
cacheCreateWxmlFns [ resourcePath ] = arguments
63
62
const { pageType, moduleId, components } = getFileInfo ( resourcePath ) || { }
64
63
@@ -71,12 +70,10 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone
71
70
// name: 'comA$hash',
72
71
// moduleId: 'moduleId'
73
72
// }
74
- const name = getCompNameBySrc ( resourcePath )
73
+ const { name, filePath : wxmlSrc } = getCompNameAndSrc ( context , resourcePath )
75
74
const options = { components, pageType, name, moduleId }
76
75
const wxmlContent = genComponentWxml ( compiled , options , emitFile , emitError , emitWarning )
77
- const wxmlSrc = `components/${ name } `
78
-
79
- emitFile ( `${ wxmlSrc } .wxml` , wxmlContent )
76
+ emitFile ( wxmlSrc , wxmlContent )
80
77
}
81
78
82
79
// 编译出 wxml
@@ -106,7 +103,15 @@ function compileWxml (compiled, html) {
106
103
pollComponentsStatus ( )
107
104
} )
108
105
. 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
+ } )
110
115
} )
111
116
}
112
117
@@ -151,14 +156,13 @@ function compileMPScript (script, mpOptioins, moduleId) {
151
156
// checkMPEntry 针对 entry main.js 的入口处理
152
157
// 编译出 app, page 的入口js/wxml/json
153
158
154
- const startPageReg = / ^ \^ /
155
159
let globalComponents
156
160
function compileMP ( content , mpOptioins ) {
157
161
const { resourcePath, emitFile, resolve, context, options } = this
158
162
159
163
const fileInfo = resolveTarget ( resourcePath , options . entry )
160
164
cacheFileInfo ( resourcePath , fileInfo )
161
- const { src , name , isApp, isPage } = fileInfo
165
+ const { isApp, isPage } = fileInfo
162
166
if ( isApp ) {
163
167
// 解析前将可能存在的全局组件清空
164
168
clearGlobalComponents ( )
@@ -169,7 +173,7 @@ function compileMP (content, mpOptioins) {
169
173
const { metadata } = babel . transform ( content , { extends : babelrc , plugins : isApp ? [ parseConfig , parseGlobalComponents ] : [ parseConfig ] } )
170
174
171
175
// metadata: config
172
- const { config , rootComponent, globalComponents : globalComps } = metadata
176
+ const { rootComponent, globalComponents : globalComps } = metadata
173
177
174
178
if ( isApp ) {
175
179
// 保存旧数据,用于对比
@@ -202,39 +206,13 @@ function compileMP (content, mpOptioins) {
202
206
}
203
207
204
208
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
-
231
209
// 这儿应该异步在所有的模块都清晰后再生成
232
210
// 生成入口 wxml
233
211
if ( isPage && rootComponent ) {
234
212
resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
235
213
if ( err ) return
236
214
// 这儿需要搞定 根组件的 路径
237
- createAppWxml ( emitFile , resourcePath , rootComponentSrc )
215
+ createAppWxml ( emitFile , resourcePath , rootComponentSrc , this . options . context )
238
216
} )
239
217
}
240
218
}
@@ -248,8 +226,8 @@ function resolveSrc (originComponents, components, resolveFn, context) {
248
226
resolveFn ( context , originComponents [ k ] , ( err , realSrc ) => {
249
227
if ( err ) return reject ( err )
250
228
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 }
253
231
resolve ( )
254
232
} )
255
233
} )
0 commit comments