1
1
/* eslint-disable no-console */
2
2
import path from 'node:path'
3
3
4
- import { chalk , NODE_MODULES } from '@tarojs/helper'
4
+ import { chalk , fs , NODE_MODULES , NPM_DIR } from '@tarojs/helper'
5
5
import { DEFAULT_TERSER_OPTIONS } from '@tarojs/vite-runner/dist/utils/constants'
6
6
7
7
import initCommands from './commands'
8
8
import HarmonyCPP from './program'
9
- import { CPP_LIBRARY_NAME , CPP_LIBRARY_PATH , getProcessArg , PLATFORM_NAME } from './utils'
9
+ import { CPP_LIBRARY_NAME , CPP_LIBRARY_PATH , getProcessArg , PLATFORM_NAME , SEP_RGX } from './utils'
10
10
import { PKG_DEPENDENCIES , PKG_NAME , PKG_VERSION , PROJECT_DEPENDENCIES_NAME } from './utils/constant'
11
11
12
12
import type { IPluginContext } from '@tarojs/service'
@@ -29,7 +29,7 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
29
29
opts . ohPackage . dependencies ||= { }
30
30
31
31
if ( options . useChoreLibrary ) {
32
- opts . chorePackagePrefix ||= `${ PKG_NAME } /src/main/ets/npm `
32
+ opts . chorePackagePrefix ||= `${ PKG_NAME } /src/main/ets/${ NPM_DIR } `
33
33
opts . ohPackage . dependencies [ PKG_NAME ] ||= `^${ PKG_VERSION } `
34
34
PROJECT_DEPENDENCIES_NAME . forEach ( key => {
35
35
opts . ohPackage . dependencies [ key ] ||= PKG_DEPENDENCIES [ key ]
@@ -72,12 +72,17 @@ export default (ctx: IPluginContext, options: IOptions = {}) => {
72
72
73
73
if ( options . useChoreLibrary === false ) {
74
74
const { appPath } = ctx . paths
75
- const { outputRoot } = ctx . runOpts . config
75
+ const { outputRoot } = config
76
76
const npmDir = path . join ( outputRoot , NODE_MODULES )
77
77
// Note: 注入 C-API 库
78
78
program . externalDeps . forEach ( ( [ libName , _ , target ] ) => {
79
79
program . moveLibraries ( target || libName , path . resolve ( npmDir , libName ) , appPath , ! target )
80
80
} )
81
+ program . handleResourceEmit ( outputRoot , appPath )
82
+ program . generateInitialEntry ( )
83
+ if ( config . hapName !== 'entry' ) { // Note: 如果是 entry 不需要重写 BuildProfile 路径
84
+ fixBuildProfile ( outputRoot , path . join ( outputRoot , '../../..' ) )
85
+ }
81
86
}
82
87
}
83
88
} )
@@ -102,3 +107,20 @@ function assertHarmonyConfig (ctx: IPluginContext, config): asserts config is IH
102
107
throwError ( '请设置 harmony.projectPath' )
103
108
}
104
109
}
110
+
111
+ export function fixBuildProfile ( lib = '' , outputRoot = '' ) {
112
+ const stats = fs . statSync ( lib )
113
+ if ( stats . isDirectory ( ) ) {
114
+ fs . readdirSync ( lib ) . forEach ( ( item ) => fixBuildProfile ( path . join ( lib , item ) , outputRoot ) )
115
+ } else if ( stats . isFile ( ) && lib . endsWith ( '.ets' ) ) {
116
+ let data = fs . readFileSync ( lib , 'utf-8' )
117
+ const buildProfilePath = path . resolve ( outputRoot , 'BuildProfile' )
118
+ const rgx = / i m p o r t \s + ( \S + ) \s + f r o m \s * [ ' " ] B u i l d P r o f i l e [ ' " ] /
119
+ if ( rgx . test ( data ) ) {
120
+ data = data . replace ( rgx , ( _ , p1 ) => {
121
+ return `import ${ p1 } from '${ path . relative ( path . dirname ( lib ) , buildProfilePath ) . replace ( SEP_RGX , '/' ) } '`
122
+ } )
123
+ fs . writeFileSync ( lib , data , 'utf-8' )
124
+ }
125
+ }
126
+ }
0 commit comments