1 | import * as path from 'path'
|
2 | import * as fs from 'fs-extra'
|
3 | import wxTransformer from '@tarojs/transformer-wx'
|
4 | import {
|
5 | printLog,
|
6 | resolveScriptPath,
|
7 | npm as npmProcess,
|
8 | processTypeEnum,
|
9 | REG_TYPESCRIPT,
|
10 | chalk
|
11 | } from '@tarojs/helper'
|
12 | import { IBuildData, IH5BuildConfig } from './ui.types'
|
13 | import { copyFileToDist, analyzeFiles, parseEntryAst, analyzeStyleFilesImport, H5_OUTPUT_NAME, copyAllInterfaceFiles } from './common'
|
14 |
|
15 | async function buildForH5 (uiIndex = 'index', buildData: IBuildData) {
|
16 | console.log()
|
17 | console.log(chalk.green('开始编译 H5 端组件库!'))
|
18 | if (process.env.TARO_BUILD_TYPE === 'script') {
|
19 | await buildH5Script(buildData)
|
20 | } else {
|
21 | await buildH5Lib(uiIndex, buildData)
|
22 | }
|
23 | }
|
24 |
|
25 | async function buildH5Script (buildData: IBuildData) {
|
26 | const { appPath, projectConfig, entryFileName, sourceDirName, tempPath } = buildData
|
27 | let { outputDirName } = buildData
|
28 | const h5Config: IH5BuildConfig = Object.assign({}, projectConfig.h5)
|
29 | const entryFile = path.basename(entryFileName, path.extname(entryFileName)) + '.js'
|
30 | outputDirName = `${outputDirName}/${H5_OUTPUT_NAME}`
|
31 | h5Config.env = projectConfig.env
|
32 | h5Config.defineConstants = projectConfig.defineConstants
|
33 | h5Config.plugins = projectConfig.plugins
|
34 | h5Config.babel = projectConfig.babel
|
35 | h5Config.csso = projectConfig.csso
|
36 | h5Config.uglify = projectConfig.uglify
|
37 | h5Config.sass = projectConfig.sass
|
38 | h5Config.designWidth = projectConfig.designWidth
|
39 | if (projectConfig.deviceRatio) {
|
40 | h5Config.deviceRatio = projectConfig.deviceRatio
|
41 | }
|
42 | h5Config.sourceRoot = sourceDirName
|
43 | h5Config.outputRoot = outputDirName
|
44 | h5Config.entry = Object.assign({
|
45 | app: [path.join(tempPath, entryFile)]
|
46 | }, h5Config.entry)
|
47 | h5Config.isWatch = false
|
48 | const webpackRunner = await npmProcess.getNpmPkg('@tarojs/webpack-runner', appPath)
|
49 | webpackRunner(appPath, h5Config)
|
50 | }
|
51 |
|
52 | async function buildH5Lib (uiIndex, buildData: IBuildData) {
|
53 | try {
|
54 | const { sourceDir, appPath, outputDirName, tempPath } = buildData
|
55 | const outputDir = path.join(appPath, outputDirName, H5_OUTPUT_NAME)
|
56 | const tempEntryFilePath = resolveScriptPath(path.join(tempPath, uiIndex))
|
57 | const outputEntryFilePath = path.join(outputDir, path.basename(tempEntryFilePath))
|
58 | const code = fs.readFileSync(tempEntryFilePath).toString()
|
59 | const transformResult = wxTransformer({
|
60 | code,
|
61 | sourcePath: tempEntryFilePath,
|
62 | isNormal: true,
|
63 | isTyped: REG_TYPESCRIPT.test(tempEntryFilePath)
|
64 | })
|
65 | const { styleFiles, components, code: generateCode } = parseEntryAst(transformResult.ast, tempEntryFilePath)
|
66 | const relativePath = path.relative(appPath, tempEntryFilePath)
|
67 | printLog(processTypeEnum.COPY, '发现文件', relativePath)
|
68 | fs.ensureDirSync(path.dirname(outputEntryFilePath))
|
69 | fs.writeFileSync(outputEntryFilePath, generateCode)
|
70 | if (components.length) {
|
71 | components.forEach(item => {
|
72 | copyFileToDist(item.path as string, tempPath, outputDir, buildData)
|
73 | })
|
74 | analyzeFiles(components.map(item => item.path as string), tempPath, outputDir, buildData)
|
75 | }
|
76 | if (styleFiles.length) {
|
77 | styleFiles.forEach(item => {
|
78 | copyFileToDist(item, tempPath, path.join(appPath, outputDirName), buildData)
|
79 | })
|
80 | analyzeStyleFilesImport(styleFiles, tempPath, path.join(appPath, outputDirName), buildData)
|
81 | }
|
82 | copyAllInterfaceFiles(sourceDir, outputDir, buildData)
|
83 | } catch (err) {
|
84 | console.log(err)
|
85 | }
|
86 | }
|
87 |
|
88 | export { buildForH5, buildH5Lib, buildH5Script }
|