提交 9f17f581 编写于 作者: fxy060608's avatar fxy060608

wip(app-harmony): uni_modules

上级 bcfab83c
......@@ -14,7 +14,7 @@ declare namespace NodeJS {
UNI_UTS_JS_CODE_FORMAT?: 'cjs' | 'es'
UNI_UTS_MODULE_TYPE?: 'built-in' | ''
UNI_UTS_MODULE_PREFIX?: string
UNI_UTS_TARGET_LANGUAGE?: 'javascript' | 'kotlin' | 'swift'
UNI_UTS_TARGET_LANGUAGE?: 'javascript' | 'kotlin' | 'swift' | 'arkts'
UNI_INPUT_DIR: string
UNI_OUTPUT_DIR: string
......
......@@ -123,6 +123,34 @@ export function parseUniExtApis(
return injects
}
export function parseUniExtApi(
pluginDir: string,
pluginId: string,
vite = true,
platform: typeof process.env.UNI_UTS_PLATFORM,
language: UTSTargetLanguage = 'javascript'
) {
const pkgPath = path.resolve(pluginDir, 'package.json')
if (!fs.existsSync(pkgPath)) {
return
}
let exports: Exports | undefined
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
if (pkg && pkg.uni_modules && pkg.uni_modules['uni-ext-api']) {
exports = pkg.uni_modules['uni-ext-api']
}
if (exports) {
return parseInjects(
vite,
platform,
language,
`@/uni_modules/${pluginId}`,
pluginDir,
exports
)
}
}
type Inject = string | string[]
export type Injects = {
[name: string]:
......
......@@ -10,9 +10,10 @@ import {
checkEncryptUniModules,
getUniExtApiPlugins,
parseUTSModuleDeps,
parseUniExtApi,
resolveEncryptUniModule,
} from '../../../uni_modules'
import { enableSourceMap } from '../../../utils'
import { camelize, enableSourceMap } from '../../../utils'
import { parseManifestJsonOnce } from '../../../json'
const UTSProxyRE = /\?uts-proxy$/
......@@ -56,7 +57,7 @@ export function uniUTSAppUniModulesPlugin(
utsPlugins.add(path.basename(pluginDir))
const pkgJson = require(path.join(pluginDir, 'package.json'))
const isExtApi = !!pkgJson.uni_modules?.['uni-ext-api']
const extApiProvider = resolveExtApiProvider(pkgJson)
// 如果是 provider 扩展,需要判断 provider 的宿主插件是否在本地,在的话,自动导入该宿主插件包名
let uniExtApiProviderServicePlugin = ''
......@@ -84,6 +85,7 @@ export function uniUTSAppUniModulesPlugin(
if (process.env.UNI_PLATFORM === 'app-harmony') {
return compiler.compileArkTS(pluginDir, {
isX: !!options.x,
isExtApi,
})
}
......@@ -197,7 +199,11 @@ export function uniUTSAppUniModulesPlugin(
}
}
},
async generateBundle() {},
async generateBundle() {
if (process.env.UNI_PLATFORM === 'app-harmony') {
genAppHarmonyIndex(inputDir, utsPlugins)
}
},
}
}
......@@ -268,3 +274,64 @@ export function uniDecryptUniModulesPlugin(): Plugin {
},
}
}
function genAppHarmonyIndex(inputDir: string, utsPlugins: Set<string>) {
if (!process.env.UNI_APP_HARMONY_PROJECT_PATH) {
return
}
const uniModulesDir = path.resolve(inputDir, 'uni_modules')
const importCodes: string[] = []
const extApiCodes: string[] = []
const registerCodes: string[] = []
utsPlugins.forEach((plugin) => {
const injects = parseUniExtApi(
path.resolve(uniModulesDir, plugin),
plugin,
true,
'app-harmony',
'arkts'
)
if (injects) {
Object.keys(injects).forEach((key) => {
const inject = injects[key]
if (Array.isArray(inject) && inject.length > 1) {
const apiName = inject[1]
importCodes.push(
`import { ${inject[1]} } from './${plugin}/utssdk/app-harmony'`
)
extApiCodes.push(`uni.${apiName} = ${apiName}`)
}
})
} else {
const ident = camelize(plugin)
importCodes.push(
`import * as ${ident} from './${plugin}/utssdk/app-harmony'`
)
registerCodes.push(
`uni.registerUTSPlugin('uni_modules/${plugin}', ${ident})`
)
}
})
fs.writeFileSync(
path.resolve(
resolveUTSCompiler().resolveAppHarmonyUniModulesRootDir(
process.env.UNI_APP_HARMONY_PROJECT_PATH
),
'index.generated.ets'
),
`// This file is automatically generated by uni-app.
// Do not modify this file -- YOUR CHANGES WILL BE ERASED!
${importCodes.join('\n')}
export function initUniModules(uni: ESObject) {
initUniExtApi(uni)
${registerCodes.join('\n ')}
}
function initUniExtApi(uni: ESObject) {
${extApiCodes.join('\n ')}
}
`
)
}
......@@ -6,10 +6,11 @@ import type { CompileResult } from '.'
interface ArkTSCompilerOptions {
isX?: boolean
isExtApi?: boolean
}
export async function compileArkTS(
pluginDir: string,
_: ArkTSCompilerOptions
{ isExtApi }: ArkTSCompilerOptions
): Promise<CompileResult | void> {
if (!process.env.UNI_APP_HARMONY_PROJECT_PATH) {
console.error('manifest.json -> app-harmony -> projectPath is required')
......@@ -28,6 +29,37 @@ export async function compileArkTS(
projectPath,
pluginId
)
const globals = [
'IUniError',
'IUTSObject',
'UniError',
'UTSObject',
'UTSJSONObject',
'string',
'ComponentPublicInstance',
'ComponentInternalInstance',
]
const banners: string[] = [
`import { ${globals.join(', ')} } from '../../../../uni-app/lib/uts'`,
]
if (isExtApi) {
const globals = [
'defineAsyncApi',
'defineSyncApi',
'defineTaskApi',
'defineOnApi',
'defineOffApi',
'ApiExcutor',
'ProtocolOptions',
'ApiOptions',
'ErrRes',
]
banners.push(
`import { ${globals.join(
', '
)} } from '../../../../uni-app/lib/uni-api-shared'`
)
}
const buildOptions: UTSBundleOptions = {
hbxVersion: process.env.HX_Version || process.env.UNI_COMPILER_VERSION,
input: {
......@@ -55,6 +87,8 @@ export async function compileArkTS(
treeshake: {
noSideEffects: true,
},
banner: banners.join('\n'),
// footer:''
},
}
const result = await bundle(UTSTarget.ARKTS, buildOptions)
......@@ -65,7 +99,7 @@ export async function compileArkTS(
}
}
return {
code: requireUTSPluginCode(pluginId),
code: requireUTSPluginCode(pluginId, !!isExtApi),
deps,
encrypt: true,
dir: outputUniModuleDir,
......@@ -74,7 +108,10 @@ export async function compileArkTS(
}
}
function requireUTSPluginCode(pluginId: string) {
function requireUTSPluginCode(pluginId: string, isExtApi: boolean) {
if (isExtApi) {
return `export default uni`
}
return `export default uni.requireUTSPlugin('uni_modules/${pluginId}')`
}
......
......@@ -44,7 +44,7 @@ import { uvueOutDir } from './uvue/index'
export * from './tsc'
export { compileArkTS } from './arkts'
export { compileArkTS, resolveAppHarmonyUniModulesRootDir } from './arkts'
export const sourcemap = {
generateCodeFrameWithKotlinStacktrace,
......
......@@ -45,6 +45,8 @@ export type UTSOutputOptions = {
outDir: string
outFilename?: string
package: string
banner?: string
footer?: string
imports?: string[]
sourceMap?: boolean | string
inlineSourcesContent?: boolean
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册