11// for mp
22const compiler = require ( 'mpvue-template-compiler' )
3- const htmlBeautify = require ( 'js-beautify' ) . html
43
5- const path = require ( 'path' )
64const babel = require ( 'babel-core' )
75const { parseConfig, parseComponentsDeps } = require ( './parse' )
86const { genScript, genStyle, genPageWxml } = require ( './templates' )
97
10- const { cacheFileInfo, getFileInfo, getCompNameBySrc, resolveTarget, covertCCVar, cacheSlots, getSlots } = require ( './util' )
11-
12- const htmlBeautifyOptions = {
13- wrap_line_length : '80' ,
14- indent_size : 2 ,
15- preserve_newlines : true ,
16- max_preserve_newlines : 0 ,
17- e4x : true ,
18- unformatted : [ 'a' , 'span' , 'img' , 'code' , 'pre' , 'sub' , 'sup' , 'em' , 'strong' , 'b' , 'i' , 'u' , 'strike' , 'big' , 'small' , 'pre' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ]
19- }
8+ const {
9+ cacheFileInfo,
10+ getFileInfo,
11+ getCompNameBySrc,
12+ resolveTarget,
13+ covertCCVar,
14+ cacheSlots,
15+ getSlots,
16+ htmlBeautify,
17+ getBabelrc,
18+ getPageSrc
19+ } = require ( './util' )
2020
2121function createSlotsWxml ( emitFile , slots ) {
2222 cacheSlots ( slots )
2323 const allSlots = getSlots ( )
2424 const content = Object . keys ( allSlots ) . map ( v => allSlots [ v ] . code ) . join ( '\n' )
2525 if ( content . trim ( ) ) {
26- emitFile ( 'components/slots.wxml' , htmlBeautify ( content , htmlBeautifyOptions ) )
26+ emitFile ( 'components/slots.wxml' , htmlBeautify ( content ) )
2727 }
2828}
2929
@@ -47,7 +47,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
4747 mpTips . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n'
4848 )
4949 }
50- return htmlBeautify ( wxmlCodeStr , htmlBeautifyOptions )
50+ return htmlBeautify ( wxmlCodeStr )
5151}
5252
5353function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
@@ -63,7 +63,7 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone
6363
6464 if ( rootComponent ) {
6565 const componentName = getCompNameBySrc ( rootComponent )
66- wxmlContent = genPageWxml ( componentName )
66+ wxmlContent = genPageWxml ( componentName , src )
6767 wxmlSrc = src
6868 } else {
6969 // TODO, 这儿传 options 进去
@@ -91,8 +91,8 @@ function compileWxml (compiled, html) {
9191
9292// 针对 .vue 单文件的脚本逻辑的处理
9393// 处理出当前单文件组件的子组件依赖
94- function compileMPScript ( script , optioins , moduleId ) {
95- const babelrc = optioins . globalBabelrc ? optioins . globalBabelrc : path . resolve ( './.babelrc' )
94+ function compileMPScript ( script , mpOptioins , moduleId ) {
95+ const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
9696 const { metadata } = babel . transform ( script . content , { extends : babelrc , plugins : [ parseComponentsDeps ] } )
9797
9898 // metadata: importsMap, components
@@ -124,7 +124,7 @@ function compileMPScript (script, optioins, moduleId) {
124124 components . isCompleted = true
125125 }
126126
127- const fileInfo = resolveTarget ( this . resourcePath , optioins . mpInfo )
127+ const fileInfo = resolveTarget ( this . resourcePath , this . options . entry )
128128 cacheFileInfo ( this . resourcePath , fileInfo , { importsMap, components, moduleId } )
129129
130130 return script
@@ -135,16 +135,16 @@ function compileMPScript (script, optioins, moduleId) {
135135
136136const startPageReg = / ^ \^ /
137137
138- function compileMP ( content , optioins ) {
139- const { resourcePath, emitError, emitFile, emitWarning, resolve, context } = this
138+ function compileMP ( content , mpOptioins ) {
139+ const { resourcePath, emitError, emitFile, emitWarning, resolve, context, options } = this
140140
141- const babelrc = optioins . globalBabelrc ? optioins . globalBabelrc : path . resolve ( './.babelrc' )
141+ const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
142142 const { metadata } = babel . transform ( content , { extends : babelrc , plugins : [ parseConfig ] } )
143143
144144 // metadata: config
145145 const { config, rootComponent } = metadata
146146
147- const fileInfo = resolveTarget ( resourcePath , optioins . mpInfo )
147+ const fileInfo = resolveTarget ( resourcePath , options . entry )
148148 cacheFileInfo ( resourcePath , fileInfo )
149149 const { src, name, isApp, isPage } = fileInfo
150150
@@ -155,7 +155,7 @@ function compileMP (content, optioins) {
155155
156156 // 只有 app 才处理 pages
157157 if ( isApp ) {
158- const pages = ( configObj . pages || [ ] ) . concat ( optioins . pages )
158+ const pages = Object . keys ( options . entry ) . concat ( configObj . pages ) . filter ( v => v && v !== 'app' ) . map ( getPageSrc )
159159
160160 // ^ 开头的放在第一个
161161 const startPageIndex = pages . findIndex ( v => startPageReg . test ( v ) )
@@ -170,10 +170,10 @@ function compileMP (content, optioins) {
170170 }
171171
172172 // 生成入口 js
173- emitFile ( `${ src } .js` , genScript ( name , isPage ) )
173+ emitFile ( `${ src } .js` , genScript ( name , isPage , src ) )
174174
175175 // 生成入口 wxss
176- emitFile ( `${ src } .wxss` , genStyle ( name , isPage ) )
176+ emitFile ( `${ src } .wxss` , genStyle ( name , isPage , src ) )
177177
178178 // 这儿应该异步在所有的模块都清晰后再生成
179179 // 生成入口 wxml
0 commit comments