From cd3818c51bfe91ea0745f3cb9a6ae07e944c8039 Mon Sep 17 00:00:00 2001 From: Dominic Watson Date: Mon, 30 Oct 2023 10:37:09 +0000 Subject: [PATCH] fix: use clientOutDir if provided for qwik-insights consumption --- packages/qwik-city/buildtime/vite/plugin.ts | 2 +- packages/qwik-labs/src-vite/insights/index.ts | 2 +- packages/qwik/src/optimizer/src/plugins/vite.ts | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/qwik-city/buildtime/vite/plugin.ts b/packages/qwik-city/buildtime/vite/plugin.ts index f0e8c451ba1e..5d78295c5d51 100644 --- a/packages/qwik-city/buildtime/vite/plugin.ts +++ b/packages/qwik-city/buildtime/vite/plugin.ts @@ -258,8 +258,8 @@ function qwikCityPlugin(userOpts?: QwikCityVitePluginOptions): any { if (ctx?.target === 'ssr') { // ssr build const manifest = qwikPlugin!.api.getManifest(); - const insightsManifest = await qwikPlugin!.api.getInsightsManifest(); const clientOutDir = qwikPlugin!.api.getClientOutDir(); + const insightsManifest = await qwikPlugin!.api.getInsightsManifest(clientOutDir); if (manifest && clientOutDir) { const basePathRelDir = api.getBasePathname().replace(/^\/|\/$/, ''); diff --git a/packages/qwik-labs/src-vite/insights/index.ts b/packages/qwik-labs/src-vite/insights/index.ts index 6424ef054070..ec7df840fd79 100644 --- a/packages/qwik-labs/src-vite/insights/index.ts +++ b/packages/qwik-labs/src-vite/insights/index.ts @@ -34,7 +34,7 @@ export async function qwikInsights(qwikInsightsOpts: { logWarn('fail to fetch manifest from Insights DB'); } if (!existsSync(join(process.cwd(), outDir))) { - mkdirSync(join(process.cwd(), outDir)); + mkdirSync(join(process.cwd(), outDir), { recursive: true }); } await writeFile(join(process.cwd(), outDir, 'q-insights.json'), JSON.stringify(qManifest)); } diff --git a/packages/qwik/src/optimizer/src/plugins/vite.ts b/packages/qwik/src/optimizer/src/plugins/vite.ts index 2010b9d165b7..f03de27528a5 100644 --- a/packages/qwik/src/optimizer/src/plugins/vite.ts +++ b/packages/qwik/src/optimizer/src/plugins/vite.ts @@ -56,10 +56,10 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { const injections: GlobalInjections[] = []; const qwikPlugin = createPlugin(qwikViteOpts.optimizerOptions); - async function loadQwikInsights(): Promise { + async function loadQwikInsights(clientOutDir?: string | null): Promise { const sys = qwikPlugin.getSys(); const fs: typeof import('fs') = await sys.dynamicImport('node:fs'); - const path = sys.path.join(process.cwd(), 'dist', 'q-insights.json'); + const path = sys.path.join(process.cwd(), clientOutDir ?? 'dist', 'q-insights.json'); if (fs.existsSync(path)) { return JSON.parse(await fs.promises.readFile(path, 'utf-8')) as InsightManifest; } @@ -70,7 +70,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { getOptimizer: () => qwikPlugin.getOptimizer(), getOptions: () => qwikPlugin.getOptions(), getManifest: () => manifestInput, - getInsightsManifest: () => loadQwikInsights(), + getInsightsManifest: (clientOutDir?: string | null) => loadQwikInsights(clientOutDir), getRootDir: () => qwikPlugin.getOptions().rootDir, getClientOutDir: () => clientOutDir, getClientPublicOutDir: () => clientPublicOutDir, @@ -118,7 +118,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { if (sys.env === 'node' && !qwikViteOpts.entryStrategy) { try { - const entryStrategy = await loadQwikInsights(); + const entryStrategy = await loadQwikInsights(!qwikViteOpts.csr ? qwikViteOpts.client?.outDir : undefined); if (entryStrategy) { qwikViteOpts.entryStrategy = entryStrategy; } @@ -871,7 +871,7 @@ export interface QwikVitePluginApi { getOptimizer: () => Optimizer | null; getOptions: () => NormalizedQwikPluginOptions; getManifest: () => QwikManifest | null; - getInsightsManifest: () => Promise; + getInsightsManifest: (clientOutDir?: string | null) => Promise; getRootDir: () => string | null; getClientOutDir: () => string | null; getClientPublicOutDir: () => string | null;