1
- // for mp
2
- const compiler = require ( 'mpvue-template-compiler' )
3
-
4
1
const babel = require ( 'babel-core' )
5
2
const path = require ( 'path' )
6
3
const fs = require ( 'fs' )
7
4
const deepEqual = require ( 'deep-equal' )
5
+ const compiler = require ( 'mpvue-template-compiler' )
8
6
9
7
const { parseConfig, parseComponentsDeps, parseGlobalComponents, clearGlobalComponents } = require ( './parse' )
10
8
const { parseComponentsDeps : parseComponentsDepsTs } = require ( './parse-ts' )
11
- const { genPageWxml } = require ( './templates' )
9
+ const { genPageML } = require ( './templates' )
12
10
13
11
const {
14
12
cacheFileInfo,
15
13
getFileInfo,
16
- getCompNameAndSrc ,
14
+ getCompInfo ,
17
15
resolveTarget,
18
16
covertCCVar,
19
17
cacheSlots,
@@ -22,73 +20,57 @@ const {
22
20
getBabelrc
23
21
} = require ( './util' )
24
22
25
- let slotsHookAdded = false
26
-
27
- // 调用 compiler 生成 wxml
28
- function genComponentWxml ( compiled , options , emitFile , emitError , emitWarning ) {
23
+ function genComponentMPML ( compiled , options , emitFile , emitError , emitWarning , fileExt ) {
29
24
options . components [ 'slots' ] = { src : '/components/slots' , name : 'slots' }
30
- const { code : wxmlCodeStr , compiled : cp , slots, importCode } = compiler . compileToWxml ( compiled , options )
31
- const { mpErrors, mpTips } = cp
25
+ const { code : mpmlContent , compiled : compiledResult , slots, importCode } = compiler . compileToWxml ( compiled , options , fileExt )
26
+ const { mpErrors, mpTips } = compiledResult
32
27
// 缓存 slots,延迟编译
33
28
cacheSlots ( slots , importCode )
34
29
35
30
if ( mpErrors && mpErrors . length ) {
36
- emitError (
37
- `\n Error compiling template:\n` +
38
- mpErrors . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n'
39
- )
31
+ emitError ( '\n Error compiling template:\n' + mpErrors . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n' )
40
32
}
41
33
if ( mpTips && mpTips . length ) {
42
- emitWarning (
43
- mpTips . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n'
44
- )
34
+ emitWarning ( mpTips . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n' )
45
35
}
46
- return htmlBeautify ( wxmlCodeStr )
36
+ return htmlBeautify ( mpmlContent )
47
37
}
48
38
49
- function createAppWxml ( emitFile , resourcePath , rootComponent , context ) {
39
+ function createPageMPML ( emitFile , resourcePath , rootComponent , context , fileExt ) {
50
40
const { src } = getFileInfo ( resourcePath ) || { }
51
- const { name : componentName , filePath : wxmlSrc } = getCompNameAndSrc ( context , rootComponent )
52
- const wxmlContent = genPageWxml ( componentName , wxmlSrc )
53
- emitFile ( `${ src } .wxml ` , wxmlContent )
41
+ const { name, filePath } = getCompInfo ( context , rootComponent , fileExt )
42
+ const MPMLContent = genPageML ( name , filePath , fileExt )
43
+ emitFile ( `${ src } .${ fileExt . template } ` , MPMLContent )
54
44
}
55
- // 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
56
- const cacheCreateWxmlFns = { }
57
45
58
- function createWxml ( { emitWarning, emitError, emitFile, resourcePath, context, compiled } ) {
59
- cacheCreateWxmlFns [ resourcePath ] = arguments
60
- const { pageType, moduleId, components } = getFileInfo ( resourcePath ) || { }
46
+ // 更新全局组件时,需要重新生成 mpml,用这个字段保存所有需要更新的页面及其参数
47
+ const cacheCreateMPMLFns = { }
61
48
62
- // TODO, 这儿传 options 进去
63
- // {
64
- // components: {
65
- // 'com-a': { src: '../../components/comA$hash', name: 'comA$hash' }
66
- // },
67
- // pageType: 'component',
68
- // name: 'comA$hash',
69
- // moduleId: 'moduleId'
70
- // }
71
- const { name, filePath : wxmlSrc } = getCompNameAndSrc ( context , resourcePath )
49
+ function createComponentMPML ( { emitWarning, emitError, emitFile, resourcePath, context, compiled, fileExt } ) {
50
+ cacheCreateMPMLFns [ resourcePath ] = arguments
51
+ const { pageType, moduleId, components } = getFileInfo ( resourcePath ) || { }
52
+ const { name, filePath } = getCompInfo ( context , resourcePath , fileExt )
72
53
const options = { components, pageType, name, moduleId }
73
- const wxmlContent = genComponentWxml ( compiled , options , emitFile , emitError , emitWarning )
74
- emitFile ( wxmlSrc , wxmlContent )
54
+ const MPMLContent = genComponentMPML ( compiled , options , emitFile , emitError , emitWarning , fileExt )
55
+ emitFile ( filePath , MPMLContent )
75
56
}
76
57
77
- // 编译出 wxml
78
- function compileWxml ( compiled , html ) {
58
+ let slotsHookAdded = false
59
+ function compileMPML ( compiled , html , options ) {
60
+ const fileExt = options . fileExt
79
61
if ( ! slotsHookAdded ) {
80
62
// avoid add hook several times during compilation
81
63
slotsHookAdded = true
82
64
// TODO: support webpack4
83
65
this . _compilation . plugin ( 'seal' , ( ) => {
84
66
const content = getSlots ( )
85
67
if ( content . trim ( ) ) {
86
- this . emitFile ( ' components/slots.wxml' , htmlBeautify ( content ) )
68
+ this . emitFile ( ` components/slots.${ fileExt . template } ` , htmlBeautify ( content ) )
87
69
}
88
- // reset flag after slots file emited
89
70
slotsHookAdded = false
90
71
} )
91
72
}
73
+
92
74
return new Promise ( resolve => {
93
75
const pollComponentsStatus = ( ) => {
94
76
const { pageType, components } = getFileInfo ( this . resourcePath ) || { }
@@ -99,66 +81,66 @@ function compileWxml (compiled, html) {
99
81
}
100
82
}
101
83
pollComponentsStatus ( )
102
- } )
103
- . then ( ( ) => {
104
- createWxml ( {
105
- emitWarning : this . emitWarning ,
106
- emitError : this . emitError ,
107
- emitFile : this . emitFile ,
108
- resourcePath : this . resourcePath ,
109
- context : this . options . context ,
110
- rootComponent : null ,
111
- compiled, html
112
- } )
84
+ } ) . then ( ( ) => {
85
+ createComponentMPML ( {
86
+ emitWarning : this . emitWarning ,
87
+ emitError : this . emitError ,
88
+ emitFile : this . emitFile ,
89
+ resourcePath : this . resourcePath ,
90
+ context : this . options . context ,
91
+ rootComponent : null ,
92
+ compiled, html,
93
+ fileExt
113
94
} )
95
+ } )
114
96
}
115
97
116
98
// 针对 .vue 单文件的脚本逻辑的处理
117
99
// 处理出当前单文件组件的子组件依赖
118
100
function compileMPScript ( script , mpOptioins , moduleId ) {
119
101
const { resourcePath, options, resolve, context } = this
120
102
const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
121
- let result , metadata
103
+
122
104
let scriptContent = script . content
123
105
const babelOptions = { extends : babelrc , plugins : [ parseComponentsDeps ] }
124
- if ( script . src ) { // 处理src
106
+ if ( script . src ) {
125
107
const scriptpath = path . join ( path . dirname ( resourcePath ) , script . src )
126
108
scriptContent = fs . readFileSync ( scriptpath ) . toString ( )
127
109
}
128
- if ( script . lang === 'ts' ) { // 处理ts
110
+
111
+ let metadata
112
+ if ( script . lang === 'ts' ) {
129
113
metadata = parseComponentsDepsTs ( scriptContent )
130
114
} else {
131
- result = babel . transform ( scriptContent , babelOptions )
115
+ const result = babel . transform ( scriptContent , babelOptions )
132
116
metadata = result . metadata
133
117
}
134
-
135
118
// metadata: importsMap, components
136
119
const { importsMap, components : originComponents } = metadata
137
120
138
121
// 处理子组件的信息
139
122
const components = { }
140
123
const fileInfo = resolveTarget ( resourcePath , options . entry )
124
+
125
+ const callback = ( ) => resolveComponent ( resourcePath , fileInfo , importsMap , components , moduleId )
141
126
if ( originComponents ) {
142
- resolveSrc ( originComponents , components , resolve , context , options . context ) . then ( ( ) => {
143
- resolveComponent ( resourcePath , fileInfo , importsMap , components , moduleId )
144
- } ) . catch ( err => {
145
- console . error ( err )
146
- resolveComponent ( resourcePath , fileInfo , importsMap , components , moduleId )
147
- } )
127
+ resolveSrc ( originComponents , components , resolve , context , options . context , mpOptioins . fileExt )
128
+ . then ( ( ) => callback ( ) )
129
+ . catch ( err => {
130
+ console . error ( err )
131
+ callback ( )
132
+ } )
148
133
} else {
149
- resolveComponent ( resourcePath , fileInfo , importsMap , components , moduleId )
134
+ callback ( )
150
135
}
151
136
152
137
return script
153
138
}
154
139
155
- // checkMPEntry 针对 entry main.js 的入口处理
156
- // 编译出 app, page 的入口js/wxml/json
157
-
140
+ // checkMPEntry 针对 entry main.js 的入口处理: 编译出 app, page 的入口js、mpml、json
158
141
let globalComponents
159
142
function compileMP ( content , mpOptioins ) {
160
143
const { resourcePath, emitFile, resolve, context, options } = this
161
-
162
144
const fileInfo = resolveTarget ( resourcePath , options . entry )
163
145
cacheFileInfo ( resourcePath , fileInfo )
164
146
const { isApp, isPage } = fileInfo
@@ -182,50 +164,50 @@ function compileMP (content, mpOptioins) {
182
164
183
165
// 解析全局组件的路径
184
166
const components = { }
185
- resolveSrc ( globalComps , components , resolve , context , options . context ) . then ( ( ) => {
167
+ resolveSrc ( globalComps , components , resolve , context , options . context , mpOptioins . fileExt ) . then ( ( ) => {
186
168
handleResult ( components )
187
169
} ) . catch ( err => {
188
170
console . error ( err )
189
171
handleResult ( components )
190
172
} )
191
173
const handleResult = components => {
192
174
globalComponents = components
193
- // 热更时,如果全局组件更新,需要重新生成所有的wxml
175
+ // 热更时,如果全局组件更新,需要重新生成所有的 mpml
194
176
if ( oldGlobalComponents && ! deepEqual ( oldGlobalComponents , globalComponents ) ) {
195
177
// 更新所有页面的组件
196
178
Object . keys ( cacheResolveComponents ) . forEach ( k => {
197
179
resolveComponent ( ...cacheResolveComponents [ k ] )
198
180
} )
199
- // 重新生成所有wxml
200
- Object . keys ( cacheCreateWxmlFns ) . forEach ( k => {
201
- createWxml ( ...cacheCreateWxmlFns [ k ] )
181
+ // 重新生成所有 mpml
182
+ Object . keys ( cacheCreateMPMLFns ) . forEach ( k => {
183
+ createComponentMPML ( ...cacheCreateMPMLFns [ k ] )
202
184
} )
203
185
}
204
186
}
205
187
}
206
188
207
189
if ( isApp || isPage ) {
208
190
// 这儿应该异步在所有的模块都清晰后再生成
209
- // 生成入口 wxml
191
+ // 生成入口 mpml
210
192
if ( isPage && rootComponent ) {
211
193
resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
212
194
if ( err ) return
213
195
// 这儿需要搞定 根组件的 路径
214
- createAppWxml ( emitFile , resourcePath , rootComponentSrc , this . options . context )
196
+ createPageMPML ( emitFile , resourcePath , rootComponentSrc , this . options . context , mpOptioins . fileExt )
215
197
} )
216
198
}
217
199
}
218
200
219
201
return content
220
202
}
221
203
222
- function resolveSrc ( originComponents , components , resolveFn , context , projectRoot ) {
204
+ function resolveSrc ( originComponents , components , resolveFn , context , projectRoot , fileExt ) {
223
205
return Promise . all ( Object . keys ( originComponents ) . map ( k => {
224
206
return new Promise ( ( resolve , reject ) => {
225
207
resolveFn ( context , originComponents [ k ] , ( err , realSrc ) => {
226
208
if ( err ) return reject ( err )
227
209
const com = covertCCVar ( k )
228
- const { filePath, name } = getCompNameAndSrc ( projectRoot , realSrc )
210
+ const { filePath, name } = getCompInfo ( projectRoot , realSrc , fileExt )
229
211
components [ com ] = { src : filePath , name }
230
212
resolve ( )
231
213
} )
@@ -247,4 +229,8 @@ function resolveComponent (resourcePath, fileInfo, importsMap, localComponents,
247
229
}
248
230
}
249
231
250
- module . exports = { compileWxml, compileMPScript, compileMP }
232
+ module . exports = {
233
+ compileMP,
234
+ compileMPML,
235
+ compileMPScript
236
+ }
0 commit comments