Skip to content

Commit c130b9a

Browse files
committedMar 17, 2025
feat(harmony): inject build-profile
1 parent e1f1f91 commit c130b9a

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed
 

‎packages/taro-platform-harmony-cpp/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"@tarojs/service": "workspace:*",
4444
"@types/react": "^18.3.3",
4545
"@types/react-reconciler": "^0.28.8",
46-
"json5": "^2.2.3",
4746
"react": "^18.3.1",
4847
"react-dom": "^18.3.1",
4948
"react-reconciler": "0.29.0",

‎packages/taro-platform-harmony-cpp/src/index.ts

+60-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import path from 'node:path'
33

44
import { chalk, fs, NPM_DIR } from '@tarojs/helper'
5+
import { readJsonSync } from '@tarojs/vite-runner/dist/utils/compiler/harmony'
56
import { DEFAULT_TERSER_OPTIONS } from '@tarojs/vite-runner/dist/utils/constants'
67

78
import HarmonyCPP from './program'
@@ -12,6 +13,7 @@ import type { IPluginContext } from '@tarojs/service'
1213
import type { IHarmonyConfig } from '@tarojs/taro/types/compile'
1314

1415
const argProjectPath = getProcessArg('lib')
16+
const argHapName = getProcessArg('hap')
1517

1618
export interface IOptions {
1719
useConfigName?: string
@@ -48,7 +50,7 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
4850
terserOptions.config.output.comments ||= /^!/ // Note: 避免删除第一行注释影响 rawfile 代码缓存
4951

5052
harmonyConfig.name ||= 'default'
51-
harmonyConfig.hapName ||= 'entry'
53+
harmonyConfig.hapName ||= argHapName || 'entry'
5254
const { projectPath, hapName } = harmonyConfig
5355

5456
opts.outputRoot = path.join(argProjectPath || projectPath, hapName, 'src/main', 'ets')
@@ -65,10 +67,66 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
6567
await program.start()
6668

6769
if (options.useChoreLibrary === false) {
68-
const { hapName = 'entry', outputRoot } = config
70+
const { projectPath, hapName = 'entry', outputRoot } = config
6971
if (hapName !== 'entry') { // Note: 如果是 entry 不需要重写 BuildProfile 路径
7072
fixBuildProfile(outputRoot, path.join(outputRoot, '../../..'))
7173
}
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+
}
72130
}
73131
}
74132
})

‎packages/taro-platform-harmony-cpp/src/program/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import path from 'node:path'
33

44
import { chalk, fs, recursiveMerge, resolveSync } from '@tarojs/helper'
55
import { HarmonyOS_ArkTS as HarmonyOS } from '@tarojs/plugin-platform-harmony-ets/dist'
6+
import { readJsonSync } from '@tarojs/vite-runner/dist/utils/compiler/harmony'
67

7-
import { NPM_DIR, PACKAGE_NAME, PLATFORM_NAME, readJsonSync } from '../utils'
8+
import { NPM_DIR, PACKAGE_NAME, PLATFORM_NAME } from '../utils'
89
import appPlugin from './vite/app'
910
import injectEnv from './vite/inject-env'
1011
import pagePlugin from './vite/page'

‎packages/taro-platform-harmony-cpp/src/utils/file.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
import path from 'node:path'
21
import vm from 'node:vm'
32

43
import { fs } from '@tarojs/helper'
5-
import JSON5 from 'json5'
6-
7-
export function readJsonSync (file: string) {
8-
const ext = path.extname(file)
9-
if (ext === '.json5') {
10-
const raw = fs.readFileSync(file, 'utf-8')
11-
return JSON5.parse(raw)
12-
}
13-
return fs.readJSONSync(file)
14-
}
154

165
export async function generateV8Cache (filename: string, code?: string) {
176
if (!code) {

‎packages/taro-platform-harmony-cpp/src/utils/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22

3-
import { fs } from '@tarojs/helper'
3+
import { readJsonSync } from '@tarojs/vite-runner/dist/utils/compiler/harmony'
44

55
export const PLATFORM_NAME = 'harmony_cpp'
66
export const PACKAGE_NAME = '@tarojs/plugin-platform-harmony-cpp'
@@ -35,7 +35,7 @@ export function getProcessArg (name: string, defaultValue = '') {
3535
export const isDebug = checkDebug()
3636

3737
export function getProjectId (name = '') {
38-
const pkg = fs.readJsonSync(path.posix.join(process.cwd(), 'package.json'))
38+
const pkg = readJsonSync(path.posix.join(process.cwd(), 'package.json'))
3939
return `${pkg.name || name}@${pkg.version || '0.0.1'}`
4040
}
4141

‎pnpm-lock.yaml

+9-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)