@@ -23,22 +23,7 @@ const {
2323 getPageSrc
2424} = require ( './util' )
2525
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
4227
4328// 调用 compiler 生成 wxml
4429function genComponentWxml ( compiled , options , emitFile , emitError , emitWarning ) {
@@ -47,7 +32,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
4732 const { mpErrors, mpTips } = cp
4833
4934 // 缓存 slots,延迟编译
50- createSlotsWxml ( emitFile , slots , importCode )
35+ cacheSlots ( slots , importCode )
5136
5237 if ( mpErrors && mpErrors . length ) {
5338 emitError (
@@ -63,47 +48,66 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
6348 return htmlBeautify ( wxmlCodeStr )
6449}
6550
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+ }
6658// 更新全局组件时,需要重新生成wxml,用这个字段保存所有需要更新的页面及其参数
6759const cacheCreateWxmlFns = { }
6860
6961function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
7062 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 } `
10078
10179 emitFile ( `${ wxmlSrc } .wxml` , wxmlContent )
10280}
10381
10482// 编译出 wxml
10583function 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+ } )
107111}
108112
109113// 针对 .vue 单文件的脚本逻辑的处理
@@ -150,7 +154,7 @@ function compileMPScript (script, mpOptioins, moduleId) {
150154const startPageReg = / ^ \^ /
151155let globalComponents
152156function compileMP ( content , mpOptioins ) {
153- const { resourcePath, emitError , emitFile, emitWarning , resolve, context, options } = this
157+ const { resourcePath, emitFile, resolve, context, options } = this
154158
155159 const fileInfo = resolveTarget ( resourcePath , options . entry )
156160 cacheFileInfo ( resourcePath , fileInfo )
@@ -230,7 +234,7 @@ function compileMP (content, mpOptioins) {
230234 resolve ( context , rootComponent , ( err , rootComponentSrc ) => {
231235 if ( err ) return
232236 // 这儿需要搞定 根组件的 路径
233- createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponentSrc )
237+ createAppWxml ( emitFile , resourcePath , rootComponentSrc )
234238 } )
235239 }
236240 }
0 commit comments