/
index.ts
775 lines (731 loc) · 29.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
import * as fs from 'fs-extra'
import * as path from 'path'
import { AppConfig, TabBar } from '@tarojs/taro'
import chalk from 'chalk'
import * as prettier from 'prettier'
import traverse, { NodePath } from 'babel-traverse'
import * as t from 'babel-types'
import * as taroize from '@tarojs/taroize'
import * as wxTransformer from '@tarojs/transformer-wx'
import * as postcss from 'postcss'
import * as unitTransform from 'postcss-taro-unit-transform'
import {
printLog,
promoteRelativePath,
resolveScriptPath,
processStyleImports,
getPkgVersion,
pascalCase,
emptyDirectory
} from '../util'
import {
BUILD_TYPES,
MINI_APP_FILES,
processTypeEnum,
REG_TYPESCRIPT,
REG_URL,
REG_IMAGE,
IMINI_APP_FILE_TYPE
} from '../util/constants'
import { generateMinimalEscapeCode } from '../util/astConvert'
import Creator from '../create/creator'
import babylonConfig from '../config/babylon'
import { IPrettierConfig } from '../util/types'
import { analyzeImportUrl, incrementId } from './helper'
const template = require('babel-template')
const prettierJSConfig: IPrettierConfig = {
semi: false,
singleQuote: true,
parser: 'babel'
}
const OUTPUT_STYLE_EXTNAME = '.scss'
const WX_GLOBAL_FN = new Set<string>(['getApp', 'getCurrentPages', 'requirePlugin'])
interface IComponent {
name: string,
path: string
}
interface IImport {
ast: t.File,
name: string,
wxs?: boolean
}
interface IParseAstOptions {
ast: t.File,
sourceFilePath: string,
outputFilePath: string,
importStylePath?: string | null,
depComponents?: Set<IComponent>,
imports?: IImport[],
isApp?: boolean
}
interface ITaroizeOptions {
json?: string,
script?: string,
wxml?: string,
path?: string,
rootPath?: string,
type?: string
}
export default class Convertor {
root: string
type: string
convertRoot: string
convertDir: string
importsDir: string
fileTypes: IMINI_APP_FILE_TYPE
pages: Set<string>
components: Set<IComponent>
hadBeenCopyedFiles: Set<string>
hadBeenBuiltComponents: Set<string>
hadBeenBuiltImports: Set<string>
entryJSPath: string
entryJSONPath: string
entryStylePath: string
entryJSON: AppConfig
entryStyle: string
constructor ({ root, type }) {
this.root = root
this.type = type || 'weapp'
this.convertRoot = path.join(this.root, 'taroConvert')
this.convertDir = path.join(this.convertRoot, 'src')
this.importsDir = path.join(this.convertDir, 'imports')
this.fileTypes = MINI_APP_FILES[this.type]
this.pages = new Set<string>()
this.components = new Set<IComponent>()
this.hadBeenCopyedFiles = new Set<string>()
this.hadBeenBuiltComponents = new Set<string>()
this.hadBeenBuiltImports = new Set<string>()
this.init()
}
init () {
console.log(chalk.green('开始代码转换...'))
this.initConvert()
this.getApp()
this.getPages()
this.getSitemapLocation()
this.getSubPackages()
}
initConvert () {
if (fs.existsSync(this.convertRoot)) {
emptyDirectory(this.convertRoot, { excludes: ['node_modules'] })
} else {
fs.ensureDirSync(this.convertRoot)
}
}
wxsIncrementId = incrementId()
parseAst ({
ast,
sourceFilePath,
outputFilePath,
importStylePath,
depComponents,
imports = [],
isApp = false
}: IParseAstOptions): { ast: t.File, scriptFiles: Set<string> } {
const scriptFiles = new Set<string>()
const self = this
let componentClassName: string
let needInsertImportTaro = false
traverse(ast, {
Program: {
enter (astPath) {
astPath.traverse({
ClassDeclaration (astPath) {
const node = astPath.node
let isTaroComponent = false
if (node.superClass) {
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
componentClassName = node.id.name
}
}
},
ClassExpression (astPath) {
const node = astPath.node
if (node.superClass) {
let isTaroComponent = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
if (node.id === null) {
const parentNode = astPath.parentPath.node as t.VariableDeclarator
if (t.isVariableDeclarator(astPath.parentPath)) {
componentClassName = (parentNode.id as t.Identifier).name
}
} else {
componentClassName = node.id.name
}
}
}
},
ExportDefaultDeclaration (astPath) {
const node = astPath.node
const declaration = node.declaration
if (
declaration &&
(declaration.type === 'ClassDeclaration' || declaration.type === 'ClassExpression')
) {
const superClass = declaration.superClass
if (superClass) {
let isTaroComponent = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: 'render' })) {
astPath.traverse({
JSXElement () {
isTaroComponent = true
}
})
}
}
})
if (isTaroComponent) {
componentClassName = declaration.id.name
}
}
}
},
ImportDeclaration (astPath) {
const node = astPath.node
const source = node.source
const value = source.value
analyzeImportUrl(self.root, sourceFilePath, scriptFiles, source, value)
},
CallExpression (astPath) {
const node = astPath.node
const calleePath = astPath.get('callee')
const callee = calleePath.node
if (callee.type === 'Identifier') {
if (callee.name === 'require') {
const args = node.arguments as Array<t.StringLiteral>
const value = args[0].value
analyzeImportUrl(self.root, sourceFilePath, scriptFiles, args[0], value)
} else if (WX_GLOBAL_FN.has(callee.name)) {
calleePath.replaceWith(
t.memberExpression(t.identifier('Taro'), callee as t.Identifier)
)
needInsertImportTaro = true
}
}
},
MemberExpression (astPath) {
const node = astPath.node
const object = node.object
if (t.isIdentifier(object) && (object.name === 'wx' || object.name === 'jd')) {
node.object = t.identifier('Taro')
needInsertImportTaro = true
}
}
})
},
exit (astPath) {
const bodyNode = astPath.get('body') as NodePath<t.Node>[]
const lastImport = bodyNode.filter(p => p.isImportDeclaration()).pop()
const hasTaroImport = bodyNode.some(p => p.isImportDeclaration() && p.node.source.value === '@tarojs/taro')
if (needInsertImportTaro && !hasTaroImport) {
(astPath.node as t.Program).body.unshift(
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier('Taro'))],
t.stringLiteral('@tarojs/taro')
)
)
}
astPath.traverse({
StringLiteral (astPath) {
const value = astPath.node.value
const extname = path.extname(value)
if (extname && REG_IMAGE.test(extname) && !REG_URL.test(value)) {
let imageRelativePath: string
let sourceImagePath: string
let outputImagePath: string
if (path.isAbsolute(value)) {
sourceImagePath = path.join(self.root, value)
} else {
sourceImagePath = path.resolve(sourceFilePath, '..', value)
}
imageRelativePath = promoteRelativePath(path.relative(sourceFilePath, sourceImagePath))
outputImagePath = self.getDistFilePath(sourceImagePath)
if (fs.existsSync(sourceImagePath)) {
self.copyFileToTaro(sourceImagePath, outputImagePath)
printLog(processTypeEnum.COPY, '图片', self.generateShowPath(outputImagePath))
} else if (!t.isBinaryExpression(astPath.parent) || astPath.parent.operator !== '+'){
printLog(processTypeEnum.ERROR, '图片不存在', self.generateShowPath(sourceImagePath))
}
if (astPath.parentPath.isVariableDeclarator()) {
astPath.replaceWith(t.callExpression(t.identifier('require'), [t.stringLiteral(imageRelativePath)]))
} else if (astPath.parentPath.isJSXAttribute()) {
astPath.replaceWith(t.jSXExpressionContainer(t.callExpression(t.identifier('require'), [t.stringLiteral(imageRelativePath)])))
}
}
}
})
if (lastImport) {
if (importStylePath) {
lastImport.insertAfter(t.importDeclaration([], t.stringLiteral(promoteRelativePath(path.relative(sourceFilePath, importStylePath)))))
}
if (imports && imports.length) {
imports.forEach(({ name, ast, wxs }) => {
const importName = wxs ? name : pascalCase(name)
if (componentClassName === importName) {
return
}
const importPath = path.join(self.importsDir, importName + (wxs ? self.wxsIncrementId() : '') + '.js')
if (!self.hadBeenBuiltImports.has(importPath)) {
self.hadBeenBuiltImports.add(importPath)
self.writeFileToTaro(importPath, prettier.format(generateMinimalEscapeCode(ast), prettierJSConfig))
}
lastImport.insertAfter(template(`import ${importName} from '${promoteRelativePath(path.relative(outputFilePath, importPath))}'`, babylonConfig)())
})
}
if (depComponents && depComponents.size) {
depComponents.forEach(componentObj => {
const name = pascalCase(componentObj.name)
const component = componentObj.path
lastImport.insertAfter(template(`import ${name} from '${promoteRelativePath(path.relative(sourceFilePath, component))}'`, babylonConfig)())
})
}
if (isApp) {
(astPath.node as t.Program).body.push(template(`Taro.render(<App />, document.getElementById('app'))`, babylonConfig)())
}
}
}
}
})
return {
ast,
scriptFiles
}
}
getApp () {
this.entryJSPath = path.join(this.root, `app${this.fileTypes.SCRIPT}`)
this.entryJSONPath = path.join(this.root, `app${this.fileTypes.CONFIG}`)
this.entryStylePath = path.join(this.root, `app${this.fileTypes.STYLE}`)
try {
this.entryJSON = JSON.parse(String(fs.readFileSync(this.entryJSONPath)))
printLog(processTypeEnum.CONVERT, '入口文件', this.generateShowPath(this.entryJSPath))
printLog(processTypeEnum.CONVERT, '入口配置', this.generateShowPath(this.entryJSONPath))
if (fs.existsSync(this.entryStylePath)) {
this.entryStyle = String(fs.readFileSync(this.entryStylePath))
printLog(processTypeEnum.CONVERT, '入口样式', this.generateShowPath(this.entryStylePath))
}
} catch (err) {
this.entryJSON = {}
console.log(chalk.red(`app${this.fileTypes.CONFIG} 读取失败,请检查!`))
process.exit(1)
}
}
getPages () {
const pages = this.entryJSON['pages']
if (!pages || !pages.length) {
console.log(chalk.red(`app${this.fileTypes.CONFIG} 配置有误,缺少页面相关配置`))
return
}
this.pages = new Set(pages)
}
getSubPackages () {
const subPackages = this.entryJSON['subpackages'] || this.entryJSON['subPackages']
if (!subPackages || !subPackages.length) {
return
}
subPackages.forEach(item => {
if (item.pages && item.pages.length) {
const root = item.root
item.pages.forEach(page => {
let pagePath = `${root}/${page}`
pagePath = pagePath.replace(/\/{2,}/g, '/')
this.pages.add(pagePath)
})
}
})
}
getSitemapLocation () {
const sitemapLocation = this.entryJSON['sitemapLocation']
if (sitemapLocation) {
const sitemapFilePath = path.join(this.root, sitemapLocation)
if (fs.existsSync(sitemapFilePath)) {
const outputFilePath = path.join(this.convertRoot, sitemapLocation)
this.copyFileToTaro(sitemapFilePath, outputFilePath)
}
}
}
generateScriptFiles (files: Set<string>) {
if (!files) {
return
}
if (files.size) {
files.forEach(file => {
if (!fs.existsSync(file) || this.hadBeenCopyedFiles.has(file)) {
return
}
const code = fs.readFileSync(file).toString()
let outputFilePath = file.replace(this.root, this.convertDir)
const extname = path.extname(outputFilePath)
if (/\.wxs/.test(extname)) {
outputFilePath += '.js'
}
const transformResult = wxTransformer({
code,
sourcePath: file,
outputPath: outputFilePath,
isNormal: true,
isTyped: REG_TYPESCRIPT.test(file)
})
const { ast, scriptFiles } = this.parseAst({
ast: transformResult.ast,
outputFilePath,
sourceFilePath: file
})
const jsCode = generateMinimalEscapeCode(ast)
this.writeFileToTaro(outputFilePath, prettier.format(jsCode, prettierJSConfig))
printLog(processTypeEnum.COPY, 'JS 文件', this.generateShowPath(outputFilePath))
this.hadBeenCopyedFiles.add(file)
this.generateScriptFiles(scriptFiles)
})
}
}
writeFileToTaro (dist: string, code: string) {
fs.ensureDirSync(path.dirname(dist))
fs.writeFileSync(dist, code)
}
copyFileToTaro (from: string, to: string, options?: fs.CopyOptionsSync) {
const filename = path.basename(from)
if (fs.statSync(from).isFile() && !path.extname(to)) {
fs.ensureDir(to)
return fs.copySync(from, path.join(to, filename), options)
}
fs.ensureDir(path.dirname(to))
return fs.copySync(from, to, options)
}
getDistFilePath (src: string, extname?: string): string {
if (!extname) return src.replace(this.root, this.convertDir)
return src.replace(this.root, this.convertDir).replace(path.extname(src), extname)
}
generateShowPath (filePath: string): string {
return filePath.replace(path.join(this.root, '/'), '').split(path.sep).join('/')
}
generateEntry () {
try {
const entryJS = String(fs.readFileSync(this.entryJSPath))
const entryJSON = JSON.stringify(this.entryJSON)
const entryDistJSPath = this.getDistFilePath(this.entryJSPath)
const taroizeResult = taroize({
json: entryJSON,
script: entryJS,
path: this.root,
rootPath: this.root,
type: this.type
})
const { ast, scriptFiles } = this.parseAst({
ast: taroizeResult.ast,
sourceFilePath: this.entryJSPath,
outputFilePath: entryDistJSPath,
importStylePath: this.entryStyle ? this.entryStylePath.replace(path.extname(this.entryStylePath), OUTPUT_STYLE_EXTNAME) : null,
isApp: true
})
const jsCode = generateMinimalEscapeCode(ast)
this.writeFileToTaro(entryDistJSPath, prettier.format(jsCode, prettierJSConfig))
printLog(processTypeEnum.GENERATE, '入口文件', this.generateShowPath(entryDistJSPath))
if (this.entryStyle) {
this.traverseStyle(this.entryStylePath, this.entryStyle)
}
this.generateScriptFiles(scriptFiles)
if (this.entryJSON.tabBar) {
this.generateTabBarIcon(this.entryJSON.tabBar)
}
} catch (err) {
console.log(err)
}
}
generateTabBarIcon (tabBar: TabBar) {
const { list = [] } = tabBar
const icons = new Set<string>()
if (Array.isArray(list) && list.length) {
list.forEach(item => {
if (typeof item.iconPath === 'string') icons.add(item.iconPath)
if (typeof item.selectedIconPath === 'string') icons.add(item.selectedIconPath)
})
if (icons.size > 0) {
Array.from(icons)
.map(icon => path.join(this.root, icon))
.forEach(iconPath => {
const iconDistPath = this.getDistFilePath(iconPath)
this.copyFileToTaro(iconPath, iconDistPath)
printLog(processTypeEnum.COPY, 'TabBar 图标', this.generateShowPath(iconDistPath))
})
}
}
}
traversePages () {
this.pages.forEach(page => {
const pagePath = path.join(this.root, page)
const pageJSPath = pagePath + this.fileTypes.SCRIPT
const pageDistJSPath = this.getDistFilePath(pageJSPath)
const pageConfigPath = pagePath + this.fileTypes.CONFIG
const pageStylePath = pagePath + this.fileTypes.STYLE
const pageTemplPath = pagePath + this.fileTypes.TEMPL
try {
const depComponents = new Set<IComponent>()
if (!fs.existsSync(pageJSPath)) {
throw new Error(`页面 ${page} 没有 JS 文件!`)
}
const param: ITaroizeOptions = {}
printLog(processTypeEnum.CONVERT, '页面文件', this.generateShowPath(pageJSPath))
if (fs.existsSync(pageConfigPath)) {
printLog(processTypeEnum.CONVERT, '页面配置', this.generateShowPath(pageConfigPath))
const pageConfigStr = String(fs.readFileSync(pageConfigPath))
const pageConfig = JSON.parse(pageConfigStr)
const pageUsingComponnets = pageConfig.usingComponents
if (pageUsingComponnets) {
// 页面依赖组件
const usingComponents = {}
Object.keys(pageUsingComponnets).forEach(component => {
let componentPath = path.resolve(pageConfigPath, '..', pageUsingComponnets[component])
if (!fs.existsSync(resolveScriptPath(componentPath))) {
componentPath = path.join(this.root, pageUsingComponnets[component])
}
if (pageUsingComponnets[component].startsWith('plugin://')) {
console.log(component)
usingComponents[component] = pageUsingComponnets[component]
} else {
depComponents.add({
name: component,
path: componentPath
})
}
})
if (Object.keys(usingComponents).length === 0) {
delete pageConfig.usingComponents
} else {
pageConfig.usingComponents = usingComponents
}
}
param.json = JSON.stringify(pageConfig)
}
param.script = String(fs.readFileSync(pageJSPath))
if (fs.existsSync(pageTemplPath)) {
printLog(processTypeEnum.CONVERT, '页面模板', this.generateShowPath(pageTemplPath))
param.wxml = String(fs.readFileSync(pageTemplPath))
}
let pageStyle: string | null = null
if (fs.existsSync(pageStylePath)) {
printLog(processTypeEnum.CONVERT, '页面样式', this.generateShowPath(pageStylePath))
pageStyle = String(fs.readFileSync(pageStylePath))
}
param.path = path.dirname(pageJSPath)
param.rootPath = this.root
param.type = this.type
const taroizeResult = taroize(param)
const { ast, scriptFiles } = this.parseAst({
ast: taroizeResult.ast,
sourceFilePath: pageJSPath,
outputFilePath: pageDistJSPath,
importStylePath: pageStyle ? pageStylePath.replace(path.extname(pageStylePath), OUTPUT_STYLE_EXTNAME) : null,
depComponents,
imports: taroizeResult.imports
})
const jsCode = generateMinimalEscapeCode(ast)
this.writeFileToTaro(pageDistJSPath, prettier.format(jsCode, prettierJSConfig))
printLog(processTypeEnum.GENERATE, '页面文件', this.generateShowPath(pageDistJSPath))
if (pageStyle) {
this.traverseStyle(pageStylePath, pageStyle)
}
this.generateScriptFiles(scriptFiles)
this.traverseComponents(depComponents)
} catch (err) {
printLog(processTypeEnum.ERROR, '页面转换', this.generateShowPath(pageJSPath))
console.log(err)
}
})
}
traverseComponents (components: Set<IComponent>) {
if (!components || !components.size) {
return
}
components.forEach(componentObj => {
const component = componentObj.path
if (this.hadBeenBuiltComponents.has(component)) return
this.hadBeenBuiltComponents.add(component)
const componentJSPath = component + this.fileTypes.SCRIPT
const componentDistJSPath = this.getDistFilePath(componentJSPath)
const componentConfigPath = component + this.fileTypes.CONFIG
const componentStylePath = component + this.fileTypes.STYLE
const componentTemplPath = component + this.fileTypes.TEMPL
try {
const param: ITaroizeOptions = {}
const depComponents = new Set<IComponent>()
if (!fs.existsSync(componentJSPath)) {
throw new Error(`组件 ${component} 没有 JS 文件!`)
}
printLog(processTypeEnum.CONVERT, '组件文件', this.generateShowPath(componentJSPath))
if (fs.existsSync(componentConfigPath)) {
printLog(processTypeEnum.CONVERT, '组件配置', this.generateShowPath(componentConfigPath))
const componentConfigStr = String(fs.readFileSync(componentConfigPath))
const componentConfig = JSON.parse(componentConfigStr)
const componentUsingComponnets = componentConfig.usingComponents
if (componentUsingComponnets) {
// 页面依赖组件
Object.keys(componentUsingComponnets).forEach(component => {
let componentPath = path.resolve(componentConfigPath, '..', componentUsingComponnets[component])
if (!fs.existsSync(resolveScriptPath(componentPath))) {
componentPath = path.join(this.root, componentUsingComponnets[component])
}
depComponents.add({
name: component,
path: componentPath
})
})
delete componentConfig.usingComponents
}
param.json = JSON.stringify(componentConfig)
}
param.script = String(fs.readFileSync(componentJSPath))
if (fs.existsSync(componentTemplPath)) {
printLog(processTypeEnum.CONVERT, '组件模板', this.generateShowPath(componentTemplPath))
param.wxml = String(fs.readFileSync(componentTemplPath))
}
let componentStyle: string | null = null
if (fs.existsSync(componentStylePath)) {
printLog(processTypeEnum.CONVERT, '组件样式', this.generateShowPath(componentStylePath))
componentStyle = String(fs.readFileSync(componentStylePath))
}
param.path = path.dirname(componentJSPath)
param.rootPath = this.root
param.type = this.type
const taroizeResult = taroize(param)
const { ast, scriptFiles } = this.parseAst({
ast: taroizeResult.ast,
sourceFilePath: componentJSPath,
outputFilePath: componentDistJSPath,
importStylePath: componentStyle ? componentStylePath.replace(path.extname(componentStylePath), OUTPUT_STYLE_EXTNAME) : null,
depComponents,
imports: taroizeResult.imports
})
const jsCode = generateMinimalEscapeCode(ast)
this.writeFileToTaro(componentDistJSPath, prettier.format(jsCode, prettierJSConfig))
printLog(processTypeEnum.GENERATE, '组件文件', this.generateShowPath(componentDistJSPath))
if (componentStyle) {
this.traverseStyle(componentStylePath, componentStyle)
}
this.generateScriptFiles(scriptFiles)
this.traverseComponents(depComponents)
} catch (err) {
printLog(processTypeEnum.ERROR, '组件转换', this.generateShowPath(componentJSPath))
console.log(err)
}
})
}
async styleUnitTransform (filePath: string, content: string) {
const postcssResult = await postcss([
unitTransform()
]).process(content, {
from: filePath
})
return postcssResult
}
async traverseStyle (filePath: string, style: string) {
const { imports, content } = processStyleImports(style, BUILD_TYPES.WEAPP, (str, stylePath) => {
let relativePath = stylePath
if (path.isAbsolute(relativePath)) {
relativePath = promoteRelativePath(path.relative(filePath, path.join(this.root, stylePath)))
}
return str.replace(stylePath, relativePath)
.replace(MINI_APP_FILES[BUILD_TYPES.WEAPP].STYLE, OUTPUT_STYLE_EXTNAME)
})
const styleDist = this.getDistFilePath(filePath, OUTPUT_STYLE_EXTNAME)
const { css } = await this.styleUnitTransform(filePath, content)
this.writeFileToTaro(styleDist, css)
printLog(processTypeEnum.GENERATE, '样式文件', this.generateShowPath(styleDist))
if (imports && imports.length) {
imports.forEach(importItem => {
const importPath = path.isAbsolute(importItem)
? path.join(this.root, importItem)
: path.resolve(path.dirname(filePath), importItem)
if (fs.existsSync(importPath)) {
const styleText = fs.readFileSync(importPath).toString()
this.traverseStyle(importPath, styleText)
}
})
}
}
generateConfigFiles () {
const creator = new Creator()
const templateName = 'default'
const configDir = path.join(this.convertRoot, 'config')
const pkgPath = path.join(this.convertRoot, 'package.json')
const projectName = 'taroConvert'
const description = ''
const version = getPkgVersion()
const dateObj = new Date()
const date = `${dateObj.getFullYear()}-${(dateObj.getMonth() + 1)}-${dateObj.getDate()}`
creator.template(templateName, 'pkg', pkgPath, {
description,
projectName,
version,
css: 'sass',
typescript: false,
template: templateName
})
creator.template(templateName, path.join('config', 'index.js'), path.join(configDir, 'index.js'), {
date,
projectName
})
creator.template(templateName, path.join('config', 'dev.js'), path.join(configDir, 'dev.js'))
creator.template(templateName, path.join('config', 'prod.js'), path.join(configDir, 'prod.js'))
creator.template(templateName, 'project.config.json', path.join(this.convertRoot, 'project.config.json'), {
description,
projectName
})
creator.template(templateName, '.gitignore', path.join(this.convertRoot, '.gitignore'))
creator.template(templateName, '.editorconfig', path.join(this.convertRoot, '.editorconfig'))
creator.template(templateName, '.eslintrc', path.join(this.convertRoot, '.eslintrc'), {
typescript: false
})
creator.template(templateName, path.join('src', 'index.html'), path.join(this.convertDir, 'index.html'))
creator.fs.commit(() => {
const pkgObj = JSON.parse(fs.readFileSync(pkgPath).toString())
pkgObj.dependencies['@tarojs/with-weapp'] = `^${version}`
fs.writeJSONSync(pkgPath, pkgObj, {
spaces: 2,
EOL: '\n'
})
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(configDir, 'index.js')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(configDir, 'dev.js')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(configDir, 'prod.js')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(pkgPath))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(this.convertRoot, 'project.config.json')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(this.convertRoot, '.gitignore')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(this.convertRoot, '.editorconfig')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(this.convertRoot, '.eslintrc')))
printLog(processTypeEnum.GENERATE, '文件', this.generateShowPath(path.join(this.convertDir, 'index.html')))
this.showLog()
})
}
showLog () {
console.log()
console.log(`${chalk.green('✔ ')} 转换成功,请进入 ${chalk.bold('taroConvert')} 目录下使用 npm 或者 yarn 安装项目依赖后再运行!`)
}
run () {
this.generateEntry()
this.traversePages()
this.generateConfigFiles()
}
}