1
1
// for mp
2
2
const compiler = require ( 'mpvue-template-compiler' )
3
- const htmlBeautify = require ( 'js-beautify' ) . html
4
3
5
- const path = require ( 'path' )
6
4
const babel = require ( 'babel-core' )
7
5
const { parseConfig, parseComponentsDeps } = require ( './parse' )
8
6
const { genScript, genStyle, genPageWxml } = require ( './templates' )
9
7
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' )
20
20
21
21
function createSlotsWxml ( emitFile , slots ) {
22
22
cacheSlots ( slots )
23
23
const allSlots = getSlots ( )
24
24
const content = Object . keys ( allSlots ) . map ( v => allSlots [ v ] . code ) . join ( '\n' )
25
25
if ( content . trim ( ) ) {
26
- emitFile ( 'components/slots.wxml' , htmlBeautify ( content , htmlBeautifyOptions ) )
26
+ emitFile ( 'components/slots.wxml' , htmlBeautify ( content ) )
27
27
}
28
28
}
29
29
@@ -47,7 +47,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning)
47
47
mpTips . map ( e => ` - ${ e } ` ) . join ( '\n' ) + '\n'
48
48
)
49
49
}
50
- return htmlBeautify ( wxmlCodeStr , htmlBeautifyOptions )
50
+ return htmlBeautify ( wxmlCodeStr )
51
51
}
52
52
53
53
function createWxml ( emitWarning , emitError , emitFile , resourcePath , rootComponent , compiled , html ) {
@@ -63,7 +63,7 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone
63
63
64
64
if ( rootComponent ) {
65
65
const componentName = getCompNameBySrc ( rootComponent )
66
- wxmlContent = genPageWxml ( componentName )
66
+ wxmlContent = genPageWxml ( componentName , src )
67
67
wxmlSrc = src
68
68
} else {
69
69
// TODO, 这儿传 options 进去
@@ -91,8 +91,8 @@ function compileWxml (compiled, html) {
91
91
92
92
// 针对 .vue 单文件的脚本逻辑的处理
93
93
// 处理出当前单文件组件的子组件依赖
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 )
96
96
const { metadata } = babel . transform ( script . content , { extends : babelrc , plugins : [ parseComponentsDeps ] } )
97
97
98
98
// metadata: importsMap, components
@@ -124,7 +124,7 @@ function compileMPScript (script, optioins, moduleId) {
124
124
components . isCompleted = true
125
125
}
126
126
127
- const fileInfo = resolveTarget ( this . resourcePath , optioins . mpInfo )
127
+ const fileInfo = resolveTarget ( this . resourcePath , this . options . entry )
128
128
cacheFileInfo ( this . resourcePath , fileInfo , { importsMap, components, moduleId } )
129
129
130
130
return script
@@ -135,16 +135,16 @@ function compileMPScript (script, optioins, moduleId) {
135
135
136
136
const startPageReg = / ^ \^ /
137
137
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
140
140
141
- const babelrc = optioins . globalBabelrc ? optioins . globalBabelrc : path . resolve ( './.babelrc' )
141
+ const babelrc = getBabelrc ( mpOptioins . globalBabelrc )
142
142
const { metadata } = babel . transform ( content , { extends : babelrc , plugins : [ parseConfig ] } )
143
143
144
144
// metadata: config
145
145
const { config, rootComponent } = metadata
146
146
147
- const fileInfo = resolveTarget ( resourcePath , optioins . mpInfo )
147
+ const fileInfo = resolveTarget ( resourcePath , options . entry )
148
148
cacheFileInfo ( resourcePath , fileInfo )
149
149
const { src, name, isApp, isPage } = fileInfo
150
150
@@ -155,7 +155,7 @@ function compileMP (content, optioins) {
155
155
156
156
// 只有 app 才处理 pages
157
157
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 )
159
159
160
160
// ^ 开头的放在第一个
161
161
const startPageIndex = pages . findIndex ( v => startPageReg . test ( v ) )
@@ -170,10 +170,10 @@ function compileMP (content, optioins) {
170
170
}
171
171
172
172
// 生成入口 js
173
- emitFile ( `${ src } .js` , genScript ( name , isPage ) )
173
+ emitFile ( `${ src } .js` , genScript ( name , isPage , src ) )
174
174
175
175
// 生成入口 wxss
176
- emitFile ( `${ src } .wxss` , genStyle ( name , isPage ) )
176
+ emitFile ( `${ src } .wxss` , genStyle ( name , isPage , src ) )
177
177
178
178
// 这儿应该异步在所有的模块都清晰后再生成
179
179
// 生成入口 wxml
0 commit comments