2
2
import path from 'node:path'
3
3
4
4
import { chalk , fs , NPM_DIR } from '@tarojs/helper'
5
+ import { readJsonSync } from '@tarojs/vite-runner/dist/utils/compiler/harmony'
5
6
import { DEFAULT_TERSER_OPTIONS } from '@tarojs/vite-runner/dist/utils/constants'
6
7
7
8
import HarmonyCPP from './program'
@@ -12,6 +13,7 @@ import type { IPluginContext } from '@tarojs/service'
12
13
import type { IHarmonyConfig } from '@tarojs/taro/types/compile'
13
14
14
15
const argProjectPath = getProcessArg ( 'lib' )
16
+ const argHapName = getProcessArg ( 'hap' )
15
17
16
18
export interface IOptions {
17
19
useConfigName ?: string
@@ -48,7 +50,7 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
48
50
terserOptions . config . output . comments ||= / ^ ! / // Note: 避免删除第一行注释影响 rawfile 代码缓存
49
51
50
52
harmonyConfig . name ||= 'default'
51
- harmonyConfig . hapName ||= 'entry'
53
+ harmonyConfig . hapName ||= argHapName || 'entry'
52
54
const { projectPath, hapName } = harmonyConfig
53
55
54
56
opts . outputRoot = path . join ( argProjectPath || projectPath , hapName , 'src/main' , 'ets' )
@@ -65,10 +67,66 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
65
67
await program . start ( )
66
68
67
69
if ( options . useChoreLibrary === false ) {
68
- const { hapName = 'entry' , outputRoot } = config
70
+ const { projectPath , hapName = 'entry' , outputRoot } = config
69
71
if ( hapName !== 'entry' ) { // Note: 如果是 entry 不需要重写 BuildProfile 路径
70
72
fixBuildProfile ( outputRoot , path . join ( outputRoot , '../../..' ) )
71
73
}
74
+
75
+ const buildProfilePath = path . join ( projectPath , hapName , `build-profile.${ program . useJSON5 !== false ? 'json5' : 'json' } ` )
76
+ if ( ! fs . existsSync ( buildProfilePath ) ) {
77
+ console . log (
78
+ chalk . yellow (
79
+ `目标路径配置文件缺失,可能是非法的 Harmony 模块: ${ buildProfilePath } `
80
+ )
81
+ )
82
+ }
83
+ try {
84
+ const profile = readJsonSync ( buildProfilePath )
85
+ profile . buildOption . externalNativeOptions = {
86
+ ...profile . externalNativeOptions ,
87
+ path : './src/main/cpp/CMakeLists.txt' ,
88
+ arguments : '-DCMAKE_JOB_POOL_COMPILE:STRING=compile -DCMAKE_JOB_POOL_LINK:STRING=link -DCMAKE_JOB_POOLS:STRING=compile=8;link=8' ,
89
+ cppFlags : '' ,
90
+ abiFilters : [
91
+ 'arm64-v8a'
92
+ ]
93
+ }
94
+ if ( profile . buildOptionSet instanceof Array ) {
95
+ fs . writeFileSync (
96
+ path . join ( buildProfilePath , '..' , 'consumer-rules.txt' ) ,
97
+ [
98
+ '-keep-property-name' ,
99
+ '# pages' ,
100
+ 'toLocation' ,
101
+ '# @tarojs/runtime' ,
102
+ 'designRatio' ,
103
+ 'densityDPI' ,
104
+ 'densityPixels' ,
105
+ 'deviceWidth' ,
106
+ 'deviceHeight' ,
107
+ 'viewportWidth' ,
108
+ 'viewportHeight' ,
109
+ 'safeArea' ,
110
+ 'scaledDensity' ,
111
+ 'orientation' ,
112
+ 'content' ,
113
+ 'selectorList' ,
114
+ ] . join ( '\n' )
115
+ )
116
+ profile . buildOptionSet . forEach ( ( option ) => {
117
+ option . arkOptions ||= { }
118
+ option . arkOptions . obfuscation ||= { }
119
+ option . arkOptions . obfuscation . consumerFiles ||= [ ]
120
+ option . arkOptions . obfuscation . consumerFiles . push (
121
+ './consumer-rules.txt' ,
122
+ './src/main/cpp/types/taro-native-node/obfuscation-rules.txt' ,
123
+ )
124
+ } )
125
+ }
126
+ fs . writeJSONSync ( buildProfilePath , profile , { spaces : 2 } )
127
+ } catch ( error ) {
128
+ console . warn ( chalk . red ( '更新鸿蒙配置失败:' , error ) )
129
+ }
72
130
}
73
131
}
74
132
} )
0 commit comments