Skip to content

Commit

Permalink
fix(harmony): 修复部分情况下uni_modules编译错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Nov 12, 2024
1 parent ce42c32 commit 60ca970
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
61 changes: 35 additions & 26 deletions packages/uni-app-harmony/src/compiler/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import path from 'path'
import fs from 'fs-extra'
import {
type UniVitePlugin,
buildUniExtApis,
camelize,
formatExtApiProviderName,
getCurrentCompiledUTSPlugins,
getCurrentCompiledUTSProviders,
getUniExtApiProviderRegisters,
isNormalCompileTarget,
parseManifestJsonOnce,
parseUniExtApi,
resolveUTSCompiler,
} from '@dcloudio/uni-cli-shared'
import type { OutputChunk } from 'rollup'
import type { OutputChunk, PluginContext } from 'rollup'
import StandaloneExtApis from './standalone-ext-apis.json'

const commondGlobals: Record<string, string> = {
Expand Down Expand Up @@ -87,10 +86,18 @@ export function uniAppHarmonyPlugin(): UniVitePlugin {
}
},
async generateBundle(_, bundle) {
genAppHarmonyUniModules(
process.env.UNI_INPUT_DIR,
getCurrentCompiledUTSPlugins()
)
const utsExtApis = new Set<string>()
const utsPlugins = getCurrentCompiledUTSPlugins()
const utsProviders = getCurrentCompiledUTSProviders()
// utsPlugins.difference(utsProviders)
utsPlugins.forEach((plugin) => {
if (utsProviders.has(plugin)) {
return
}
utsExtApis.add(plugin)
})
// 此方法仅需要处理非provider
genAppHarmonyUniModules(this, process.env.UNI_INPUT_DIR, utsExtApis)
for (const key in bundle) {
const serviceBundle = bundle[key] as OutputChunk
if (serviceBundle.code) {
Expand Down Expand Up @@ -201,7 +208,11 @@ function getRelatedModules(inputDir: string): string[] {
return modules
}

function genAppHarmonyUniModules(inputDir: string, utsPlugins: Set<string>) {
function genAppHarmonyUniModules(
context: PluginContext,
inputDir: string,
utsPlugins: Set<string>
) {
const uniModulesDir = path.resolve(inputDir, 'uni_modules')
const importCodes: string[] = []
const extApiCodes: string[] = []
Expand Down Expand Up @@ -341,11 +352,10 @@ function genAppHarmonyUniModules(inputDir: string, utsPlugins: Set<string>) {
`import { registerUniProvider, uni } from '@dcloudio/uni-app-runtime'`
)

const uniModuleEntryDir =
resolveUTSCompiler().resolveAppHarmonyUniModulesEntryDir()
fs.outputFileSync(
path.resolve(uniModuleEntryDir, 'index.generated.ets'),
`// This file is automatically generated by uni-app.
context.emitFile({
type: 'asset',
fileName: 'uni_modules/index.generated.ets',
source: `// This file is automatically generated by uni-app.
// Do not modify this file -- YOUR CHANGES WILL BE ERASED!
${importCodes.join('\n')}
Expand All @@ -357,8 +367,8 @@ export function initUniModules() {
function initUniExtApi() {
${extApiCodes.join('\n ')}
}
`
)
`,
})

const dependencies: Record<string, string> = {}
const modules: { name: string; srcPath: string }[] = []
Expand All @@ -379,15 +389,14 @@ function initUniExtApi() {
}
}
})
// TODO 写入到用户项目的oh-package.json5、build-profile.json5内
fs.outputJSONSync(
path.resolve(uniModuleEntryDir, 'oh-package.json5'),
{ dependencies },
{ spaces: 2 }
)
fs.outputJSONSync(
path.resolve(uniModuleEntryDir, 'build-profile.json5'),
{ modules },
{ spaces: 2 }
)
context.emitFile({
type: 'asset',
fileName: 'uni_modules/oh-package.json5',
source: JSON.stringify({ dependencies }, null, 2),
})
context.emitFile({
type: 'asset',
fileName: 'uni_modules/build-profile.json5',
source: JSON.stringify({ modules }, null, 2),
})
}
6 changes: 6 additions & 0 deletions packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ interface UniUTSPluginOptions {
}

const utsPlugins = new Set<string>()
const utsProviders = new Set<string>()

export function getCurrentCompiledUTSPlugins() {
return utsPlugins
}
export function getCurrentCompiledUTSProviders() {
return utsProviders
}

let uniExtApiCompiler = async () => {}

Expand Down Expand Up @@ -461,6 +465,7 @@ export function uniUTSAppUniModulesPlugin(
}

uniExtApiCompiler = async () => {
// 此方法为兜底方法,确保uni_modules中的所有插件都会编译,目前仅用于编译provider
// 获取 provider 扩展(编译所有uni)
const plugins = getUniExtApiPlugins().filter(
(provider) => !utsPlugins.has(provider.plugin)
Expand Down Expand Up @@ -490,6 +495,7 @@ export function uniUTSAppUniModulesPlugin(
continue
}
}
utsProviders.add(plugin.plugin)
const result = await compilePlugin(pluginDir)
if (result) {
// 时机不对,不能addWatch
Expand Down

0 comments on commit 60ca970

Please sign in to comment.