1 | import * as fs from 'fs-extra'
|
2 | import * as path from 'path'
|
3 | import * as wxTransformer from '@tarojs/transformer-wx'
|
4 |
|
5 | import { processTypeEnum, REG_TYPESCRIPT, printLog, chalk } from '@tarojs/helper'
|
6 |
|
7 | import { analyzeFiles, parseEntryAst, WEAPP_OUTPUT_NAME, copyFileToDist, copyAllInterfaceFiles } from './common'
|
8 | import { IBuildData } from './ui.types'
|
9 |
|
10 | export async function buildForWeapp (buildData: IBuildData) {
|
11 | const { appPath, entryFilePath, outputDirName, entryFileName, sourceDir } = buildData
|
12 | console.log()
|
13 | console.log(chalk.green('开始编译小程序端组件库!'))
|
14 | if (!fs.existsSync(entryFilePath)) {
|
15 | console.log(chalk.red('入口文件不存在,请检查!'))
|
16 | return
|
17 | }
|
18 | try {
|
19 | const outputDir = path.join(appPath, outputDirName, WEAPP_OUTPUT_NAME)
|
20 | const outputEntryFilePath = path.join(outputDir, entryFileName)
|
21 | const code = fs.readFileSync(entryFilePath).toString()
|
22 | const transformResult = wxTransformer({
|
23 | code,
|
24 | sourcePath: entryFilePath,
|
25 | outputPath: outputEntryFilePath,
|
26 | isNormal: true,
|
27 | isTyped: REG_TYPESCRIPT.test(entryFilePath)
|
28 | })
|
29 | const { components } = parseEntryAst(transformResult.ast, entryFilePath)
|
30 | const relativePath = path.relative(appPath, entryFilePath)
|
31 | printLog(processTypeEnum.COPY, '发现文件', relativePath)
|
32 | fs.ensureDirSync(path.dirname(outputEntryFilePath))
|
33 | fs.copyFileSync(entryFilePath, path.format({
|
34 | dir: path.dirname(outputEntryFilePath),
|
35 | base: path.basename(outputEntryFilePath)
|
36 | }))
|
37 | if (components.length) {
|
38 | components.forEach(item => {
|
39 | copyFileToDist(item.path as string, sourceDir, outputDir, buildData)
|
40 | })
|
41 | analyzeFiles(components.map(item => item.path as string), sourceDir, outputDir, buildData)
|
42 | }
|
43 | copyAllInterfaceFiles(sourceDir, outputDir, buildData)
|
44 | } catch (err) {
|
45 | console.log(err)
|
46 | }
|
47 | }
|