From 1bc10c5936d5f087e33ccf5ce9bd170e811cebd9 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 14 Jul 2022 18:55:12 +0800 Subject: [PATCH] fix: document is not found when generate pha manifest (#360) * fix: document is not found when generate pha manifest * chore: fix test case --- examples/with-pha/{ice.config.ts => ice.config.mts} | 2 +- packages/plugin-pha/src/generateManifest.ts | 1 + packages/plugin-pha/src/index.ts | 3 ++- packages/plugin-pha/src/manifestHelpers.ts | 2 +- packages/plugin-pha/src/phaMiddleware.ts | 3 ++- packages/plugin-pha/tests/manifestHelper.test.ts | 8 ++++---- 6 files changed, 11 insertions(+), 8 deletions(-) rename examples/with-pha/{ice.config.ts => ice.config.mts} (74%) diff --git a/examples/with-pha/ice.config.ts b/examples/with-pha/ice.config.mts similarity index 74% rename from examples/with-pha/ice.config.ts rename to examples/with-pha/ice.config.mts index 79282b2c7..2f94d776a 100644 --- a/examples/with-pha/ice.config.ts +++ b/examples/with-pha/ice.config.mts @@ -2,5 +2,5 @@ import { defineConfig } from '@ice/app'; import pha from '@ice/plugin-pha'; export default defineConfig({ - plugins: [pha()], + plugins: [pha({ template: true })], }); \ No newline at end of file diff --git a/packages/plugin-pha/src/generateManifest.ts b/packages/plugin-pha/src/generateManifest.ts index 7af187064..cdb717bf9 100644 --- a/packages/plugin-pha/src/generateManifest.ts +++ b/packages/plugin-pha/src/generateManifest.ts @@ -10,6 +10,7 @@ export interface Options { outputDir: string; parseOptions: Partial; compiler: Compiler; + compileTask?: () => Promise<{ serverEntry: string}>; } export async function getAppWorkerContent( diff --git a/packages/plugin-pha/src/index.ts b/packages/plugin-pha/src/index.ts index 07ef0eeca..064d698d2 100644 --- a/packages/plugin-pha/src/index.ts +++ b/packages/plugin-pha/src/index.ts @@ -28,7 +28,7 @@ function getDevPath(url: string): string { return url.startsWith('http') ? `${new URL(url).origin}/` : url; } -const plugin: Plugin = ({ onGetConfig, onHook, context, generator }, options) => { +const plugin: Plugin = ({ onGetConfig, onHook, context, generator, serverCompileTask }, options) => { const { template } = options || {}; const { command, rootDir } = context; @@ -118,6 +118,7 @@ const plugin: Plugin = ({ onGetConfig, onHook, context, generator compiler, rootDir, outputDir, + compileTask: () => serverCompileTask.get(), parseOptions: { publicPath, urlPrefix, diff --git a/packages/plugin-pha/src/manifestHelpers.ts b/packages/plugin-pha/src/manifestHelpers.ts index 64b72dcb8..445639b9f 100644 --- a/packages/plugin-pha/src/manifestHelpers.ts +++ b/packages/plugin-pha/src/manifestHelpers.ts @@ -109,7 +109,7 @@ async function getPageConfig(routeId: string, configEntry: string): Promise { const serverContext = { req: { - url: routeId, + url: `/${routeId}`, }, }; const serverModule = await import(serverEntry); diff --git a/packages/plugin-pha/src/phaMiddleware.ts b/packages/plugin-pha/src/phaMiddleware.ts index 122bd5587..9ac1b67a3 100644 --- a/packages/plugin-pha/src/phaMiddleware.ts +++ b/packages/plugin-pha/src/phaMiddleware.ts @@ -14,6 +14,7 @@ function sendResponse(res: ServerResponse, content: string, mime: string): void const createPHAMiddleware = ({ rootDir, outputDir, + compileTask, parseOptions, compiler, }: Options): ExpressRequestHandler => { @@ -25,7 +26,7 @@ const createPHAMiddleware = ({ const requestAppWorker = req.url === '/app-worker.js'; if (requestManifest || requestAppWorker) { // Get serverEntry from middleware of server-compile. - const { serverEntry } = req as any; + const { serverEntry } = await compileTask(); const [manifestEntry, routesConfigEntry] = await compileEntires(compiler, { rootDir, outputDir }); let manifest: Manifest = (await import(manifestEntry)).default; const appWorkerPath = getAppWorkerUrl(manifest, path.join(rootDir, 'src')); diff --git a/packages/plugin-pha/tests/manifestHelper.test.ts b/packages/plugin-pha/tests/manifestHelper.test.ts index fc3cd1a6e..0861ed436 100644 --- a/packages/plugin-pha/tests/manifestHelper.test.ts +++ b/packages/plugin-pha/tests/manifestHelper.test.ts @@ -281,9 +281,9 @@ describe('parse manifest', () => { template: true, }); - expect(manifest?.pages![0].document).toBe('home-document'); - expect(manifest?.pages![1].document).toBe('about-document'); - expect(manifest?.pages![2]?.frames![0].document).toBe('home-document'); + expect(manifest?.pages![0].document).toBe('/home-document'); + expect(manifest?.pages![1].document).toBe('/about-document'); + expect(manifest?.pages![2]?.frames![0].document).toBe('/home-document'); }); @@ -387,7 +387,7 @@ describe('parse manifest', () => { const manifest = await parseManifest(phaManifest, options); expect(manifest.pages![0]?.tab_header?.url).toBe('https://url-prefix.com/header'); - expect(manifest.pages![0]?.tab_header?.html).toBe('header-document'); + expect(manifest.pages![0]?.tab_header?.html).toBe('/header-document'); expect(manifest?.tab_bar?.url).toBe('https://url-prefix.com/CustomTabBar'); });