@@ -23,22 +23,7 @@ const {
23
23
getPageSrc
24
24
} = require ( './util' )
25
25
26
- let emitFileTimer = null
27
-
28
- function createSlotsWxml ( emitFile , slots , importCode ) {
29
- cacheSlots ( slots , importCode )
30
- const content = getSlots ( )
31
- // 100 delay 比较符合当前策略
32
- const delay = 100
33
- if ( content . trim ( ) ) {
34
- if ( emitFileTimer ) {
35
- clearTimeout ( emitFileTimer )
36
- }
37
- emitFileTimer = setTimeout ( function ( ) {
38
- emitFile ( 'components/slots.wxml' , htmlBeautify ( content ) )
39
- } , delay )
40
- }
41
- }
26
+ let slotsHookAdded = false
42
27
43
28
// 调用 compiler 生成 wxml
44
29
function genComponentWxml ( compiled , options , emitFile , emitError , emitWarning ) {
@@ -47,7 +32,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
47
32
const { mpErrors, mpTips } = cp
48
33
49
34
// 缓存 slots,延迟编译
50
- createSlotsWxml ( emitFile , slots , importCode )
35
+ cacheSlots ( slots , importCode )
51
36
52
37
if ( mpErrors && mpErrors . length ) {
53
38
emitError (
@@ -63,47 +48,66 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
63
48
return htmlBeautify ( wxmlCodeStr )
64
49
}
65
50
51
+ function createAppWxml ( emitFile , resourcePath , rootComponent ) {
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 )
57
+ }
66
58
// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
67
59
const cacheCreateWxmlFns = { }
68
60
69
61
function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
70
62
cacheCreateWxmlFns [ resourcePath ] = arguments
71
- const { pageType, moduleId, components, src } = getFileInfo ( resourcePath ) || { }
72
-
73
- // 这儿一个黑魔法,和 webpack 约定的规范写法有点偏差!
74
- if ( ! pageType || ( components && ! components . isCompleted ) ) {
75
- return setTimeout ( createWxml , 20 , ...arguments )
76
- }
77
-
78
- let wxmlContent = ''
79
- let wxmlSrc = ''
80
-
81
- if ( rootComponent ) {
82
- const componentName = getCompNameBySrc ( rootComponent )
83
- wxmlContent = genPageWxml ( componentName , src )
84
- wxmlSrc = src
85
- } else {
86
- // TODO, 这儿传 options 进去
87
- // {
88
- // components: {
89
- // 'com-a': { src: '../../components/comA$hash', name: 'comA$hash' }
90
- // },
91
- // pageType: 'component',
92
- // name: 'comA$hash',
93
- // moduleId: 'moduleId'
94
- // }
95
- const name = getCompNameBySrc ( resourcePath )
96
- const options = { components, pageType, name, moduleId }
97
- wxmlContent = genComponentWxml ( compiled , options , emitFile , emitError , emitWarning )
98
- wxmlSrc = `components/${ name } `
99
- }
63
+ const { pageType, moduleId, components } = getFileInfo ( resourcePath ) || { }
64
+
65
+ // TODO, 这儿传 options 进去
66
+ // {
67
+ // components: {
68
+ // 'com-a': { src: '../../components/comA$hash', name: 'comA$hash' }
69
+ // },
70
+ // pageType: 'component',
71
+ // name: 'comA$hash',
72
+ // moduleId: 'moduleId'
73
+ // }
74
+ const name = getCompNameBySrc ( resourcePath )
75
+ const options = { components, pageType, name, moduleId }
76
+ const wxmlContent = genComponentWxml ( compiled , options , emitFile , emitError , emitWarning )
77
+ const wxmlSrc = `components/${ name } `
100
78
101
79
emitFile ( `${ wxmlSrc } .wxml` , wxmlContent )
102
80
}
103
81
104
82
// 编译出 wxml
105
83
function compileWxml ( compiled , html ) {
106
- return createWxml ( this . emitWarning , this . emitError , this . emitFile , this . resourcePath , null , compiled , html )
84
+ if ( ! slotsHookAdded ) {
85
+ // avoid add hook several times during compilation
86
+ slotsHookAdded = true
87
+ // TODO: support webpack4
88
+ this . _compilation . plugin ( 'seal' , ( ) => {
89
+ const content = getSlots ( )
90
+ if ( content . trim ( ) ) {
91
+ this . emitFile ( 'components/slots.wxml' , htmlBeautify ( content ) )
92
+ }
93
+ // reset flag after slots file emited
94
+ slotsHookAdded = false
95
+ } )
96
+ }
97
+ return new Promise ( resolve => {
98
+ const pollComponentsStatus = ( ) => {
99
+ const { pageType, components } = getFileInfo ( this . resourcePath ) || { }
100
+ if ( ! pageType || ( components && ! components . isCompleted ) ) {
101
+ setTimeout ( pollComponentsStatus , 20 )
102
+ } else {
103
+ resolve ( )
104
+ }
105
+ }
106
+ pollComponentsStatus ( )
107
+ } )
108
+ . then ( ( ) => {
109
+ createWxml ( this . emitWarning , this . emitError , this . emitFile , this . resourcePath , null , compiled , html )
110
+ } )
107
111
}
108
112
109
113
// 针对 .vue 单文件的脚本逻辑的处理
@@ -150,7 +154,7 @@ function compileMPScript (script, mpOptioins, moduleId) {
150
154
const startPageReg = / ^ \^ /
151
155
let globalComponents
152
156
function compileMP ( content , mpOptioins ) {
153
- const { resourcePath, emitError , emitFile, emitWarning , resolve, context, options } = this
157
+ const { resourcePath, emitFile, resolve, context, options } = this
154
158
155
159
const fileInfo = resolveTarget ( resourcePath , options . entry )
156
160
cacheFileInfo ( resourcePath , fileInfo )
@@ -230,7 +234,7 @@ function compileMP (content, mpOptioins) {
230
234
resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
231
235
if ( err ) return
232
236
// 这儿需要搞定 根组件的 路径
233
- createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponentSrc )
237
+ createAppWxml ( emitFile , resourcePath , rootComponentSrc )
234
238
} )
235
239
}
236
240
}
0 commit comments