Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/taro-framework-react/src/vite.harmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export function harmonyVitePlugin (ctx: IPluginContext, framework: Frameworks):
function injectLoaderMeta (ctx: IPluginContext, framework: Frameworks): PluginOption {
return {
name: 'taro-react:loader-meta',
buildStart () {
async buildStart () {
const { runnerUtils } = ctx
const { getViteHarmonyCompilerContext } = runnerUtils
const viteCompilerContext = getViteHarmonyCompilerContext(this)
const viteCompilerContext = await getViteHarmonyCompilerContext(this)
if (viteCompilerContext) {
viteCompilerContext.loaderMeta ||= {}
Object.assign(viteCompilerContext.loaderMeta, getLoaderMeta(framework))
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-framework-solid/src/vite.harmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function harmonyVitePlugin (ctx: IPluginContext): PluginOption {
function injectLoaderMeta (ctx: IPluginContext): PluginOption {
return {
name: 'taro-solid:loader-meta',
buildStart () {
async buildStart () {
const { runnerUtils } = ctx
const { getViteHarmonyCompilerContext } = runnerUtils
const viteCompilerContext = getViteHarmonyCompilerContext(this)
const viteCompilerContext = await getViteHarmonyCompilerContext(this)
if (viteCompilerContext) {
viteCompilerContext.loaderMeta ||= {}
Object.assign(viteCompilerContext.loaderMeta, getLoaderMeta())
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-framework-vue3/src/vite.harmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function harmonyVitePlugin (ctx: IPluginContext, componentConfig: ICompon
function injectLoaderMeta (ctx: IPluginContext): PluginOption {
return {
name: 'taro-vue3:loader-meta',
buildStart () {
async buildStart () {
const { runnerUtils } = ctx
const { getViteHarmonyCompilerContext } = runnerUtils
const viteCompilerContext = getViteHarmonyCompilerContext(this)
const viteCompilerContext = await getViteHarmonyCompilerContext(this)
if (viteCompilerContext) {
viteCompilerContext.loaderMeta ||= {}
Object.assign(viteCompilerContext.loaderMeta, getLoaderMeta())
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-platform-harmony-cpp/src/program/vite/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default function (this: Harmony): PluginOption {
return {
name: 'taro:vite-harmony-template-app',
enforce: 'pre',
buildStart (this: PluginContext) {
async buildStart (this: PluginContext) {
const pluginContext = this
const { runnerUtils } = that.context

const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)
const compiler = await getViteHarmonyCompilerContext(pluginContext)

if (compiler) {
const app: TaroHarmonyAppMeta = compiler.app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default function (this: Harmony): PluginOption {

return {
name: 'taro:vite-add-method-env',
transform (code, id) {
async transform (code, id) {
const pluginContext = this
const { runnerUtils } = that.context
const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)
const compiler = await getViteHarmonyCompilerContext(pluginContext)
const exts = Array.from(new Set(compiler?.frameworkExts.concat(SCRIPT_EXT)))
if (!exts.some(ext => id?.split('?')[0].endsWith(ext))) {
return
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-platform-harmony-cpp/src/program/vite/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function (this: Harmony): PluginOption {
}
}
},
buildStart(this: PluginContext) {
async buildStart(this: PluginContext) {
const pluginContext = this
const { runnerUtils, runOpts } = that.context
const isPureComp = (page: TCPageMeta | TCPageMeta[]): page is TCPageMeta => {
Expand All @@ -50,7 +50,7 @@ export default function (this: Harmony): PluginOption {
}

const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)!
const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
const taroConfig = compiler.taroConfig

if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
Comment on lines +53 to 56
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

非空断言 ! 存在运行时崩溃风险。

getViteHarmonyCompilerContext 的返回类型为 Promise<ViteHarmonyCompilerContext | void>,若返回 undefined,第 54 行 compiler.taroConfig 将抛出 TypeError。其他所有调用点(如 app.tsrender.tsvue3/vite.harmony.ts 等)均使用 if (compiler) 守卫,建议此处保持一致。

建议修复
-      const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
-      const taroConfig = compiler.taroConfig
-
-      if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
+      const compiler = await getViteHarmonyCompilerContext(pluginContext)
+      if (!compiler) return
+      const taroConfig = compiler.taroConfig
+
+      if (compiler.pages instanceof Array || compiler.components instanceof Array) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
const taroConfig = compiler.taroConfig
if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
const compiler = await getViteHarmonyCompilerContext(pluginContext)
if (!compiler) return
const taroConfig = compiler.taroConfig
if (compiler.pages instanceof Array || compiler.components instanceof Array) {
🤖 Prompt for AI Agents
In `@packages/taro-platform-harmony-cpp/src/program/vite/page.ts` around lines 53
- 56, The code uses a non-null assertion on the result of
getViteHarmonyCompilerContext which can be undefined and causes a runtime crash
when accessing compiler.taroConfig; change the flow to await
getViteHarmonyCompilerContext into a possibly-undefined compiler and guard
accesses with if (compiler) (or if (compiler && (compiler.pages instanceof Array
|| compiler.components instanceof Array))) before reading compiler.taroConfig or
other properties to match the pattern used elsewhere (e.g., in app.ts,
render.ts, vue3/vite.harmony.ts).

Expand Down
4 changes: 2 additions & 2 deletions packages/taro-platform-harmony-cpp/src/program/vite/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export default function (this: Harmony): PluginOption {
return {
name: 'taro:vite-harmony-template-render',
enforce: 'pre',
buildStart (this: PluginContext) {
async buildStart (this: PluginContext) {
const pluginContext = this
const { runnerUtils } = that.context

const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)
const compiler = await getViteHarmonyCompilerContext(pluginContext)

if (compiler) {
compiler.extraComponents?.push('WaterFlowView')
Expand Down
8 changes: 4 additions & 4 deletions packages/taro-platform-harmony-cpp/src/program/vite/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export default function (this: Harmony): PluginOption {
configResolved(config) {
viteConfigResolved = config
},
buildStart(this: PluginContext) {
async buildStart(this: PluginContext) {
const pluginContext = this
const { runnerUtils } = that.context

const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)
const compiler = await getViteHarmonyCompilerContext(pluginContext)

if (compiler) {
compiler.loaderMeta ||= {}
Expand Down Expand Up @@ -70,13 +70,13 @@ export default function (this: Harmony): PluginOption {
compiler?.pages?.forEach?.(modifyPageOrComp)
compiler?.components?.forEach?.(modifyPageOrComp)
},
buildEnd() {
async buildEnd() {
const pluginContext = this
const { runnerUtils } = that.context
const cacheStyleMap = new Map<string, Set<string>>()

const { getViteHarmonyCompilerContext } = runnerUtils
const compiler = getViteHarmonyCompilerContext(pluginContext)
const compiler = await getViteHarmonyCompilerContext(pluginContext)
const cssModuleId = new Set<string>()
if (compiler) {
PageMap.forEach((moduleInfo, styleJsonPath) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-platform-harmony/src/program/arkTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ declare global {
name: 'taro:vite-loader-meta',
async buildStart () {
const { getViteHarmonyCompilerContext } = that.ctx.runnerUtils
const compiler = getViteHarmonyCompilerContext(this)
const compiler = await getViteHarmonyCompilerContext(this)
if (compiler) {
switch (that.framework) {
// @ts-ignore
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-runner-utils/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export async function getViteH5CompilerContext (rollupPluginContext: PluginConte
return compiler
}

export function getViteHarmonyCompilerContext (rollupPluginContext: PluginContext): ViteHarmonyCompilerContext | void {
const info = rollupPluginContext.getModuleInfo(VITE_COMPILER_LABEL)
export async function getViteHarmonyCompilerContext (rollupPluginContext: PluginContext): Promise<ViteHarmonyCompilerContext | void> {
const info = process.env.NODE_ENV === 'production'
? rollupPluginContext.getModuleInfo(VITE_COMPILER_LABEL)
: await rollupPluginContext.load({ id: VITE_COMPILER_LABEL })
const compiler = info?.meta.viteCompilerContext
return compiler
}
Expand Down