提交 a4632447 编写于 作者: 雪洛's avatar 雪洛

feat(harmony): 支持在非uni_modules uts api内引用鸿蒙原生包

上级 3719024c
......@@ -9,15 +9,89 @@ import {
parseUniExtApi,
resolveUTSCompiler,
} from '@dcloudio/uni-cli-shared'
import type { OutputChunk } from 'rollup'
const commondGlobals: Record<string, string> = {
vue: 'Vue',
'@vue/shared': 'uni.VueShared',
}
const harmonyGlobals: (string | RegExp)[] = [
/^@ohos\./,
/^@kit\./,
/^@hms\./,
'@ohos/hypium',
'@ohos/hamock',
]
function isHarmoneyGlobal(id: string) {
return harmonyGlobals.some((harmonyGlobal) =>
typeof harmonyGlobal === 'string'
? harmonyGlobal === id
: harmonyGlobal.test(id)
)
}
function generateHarmonyImportSpecifier(id: string) {
return id.replace(/([@\.])/g, function (_, $1) {
switch ($1) {
case '.':
return '_'
case '/':
return '__'
default:
return ''
}
})
}
function generateHarmonyImportExternalCode(hamonyPackageNames: string[]) {
return hamonyPackageNames
.filter((hamonyPackageName) => isHarmoneyGlobal(hamonyPackageName))
.map(
(hamonyPackageName) =>
`import ${generateHarmonyImportSpecifier(
hamonyPackageName
)} from '${hamonyPackageName}';`
)
}
export function uniAppHarmonyPlugin(): UniVitePlugin {
return {
name: 'uni:app-harmony',
apply: 'build',
async generateBundle() {
config() {
return {
build: {
rollupOptions: {
external: [...Object.keys(commondGlobals), ...harmonyGlobals],
output: {
globals: function (id: string) {
return (
commondGlobals[id] ||
(isHarmoneyGlobal(id)
? generateHarmonyImportSpecifier(id)
: '')
)
},
},
},
},
}
},
async generateBundle(_, bundle) {
genAppHarmonyIndex(
process.env.UNI_INPUT_DIR,
getCurrentCompiledUTSPlugins()
)
for (const key in bundle) {
const serviceBundle = bundle[key] as OutputChunk
if (serviceBundle.code) {
serviceBundle.code =
generateHarmonyImportExternalCode(serviceBundle.imports) +
serviceBundle.code
}
}
},
async writeBundle() {
if (process.env.UNI_COMPILE_TARGET === 'uni_modules') {
......
......@@ -44,15 +44,7 @@ export function uniAppVuePlugin(): UniVitePlugin {
},
build: {
rollupOptions: {
external: [
'vue',
'@vue/shared',
/^@ohos\./,
/^@kit\./,
/^@hms\./,
'@ohos/hypium',
'@ohos/hamock',
],
external: ['vue', '@vue/shared'],
output: {
name: 'AppService',
banner: polyfillCode + restoreGlobalCode,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册