From 88efc0a3be239432fa3693f182064567fed5a7f3 Mon Sep 17 00:00:00 2001 From: taichan <40626578+tai-cha@users.noreply.github.com> Date: Mon, 10 Mar 2025 07:45:17 +0900 Subject: [PATCH] =?UTF-8?q?fix(dev):=20=E6=A4=9C=E7=B4=A2=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=83=87=E3=83=83=E3=82=AF=E3=82=B9=E5=AF=BE=E8=B1=A1?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=A7HMR=E3=81=8C?= =?UTF-8?q?=E5=8A=B9=E3=81=8B=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20(#15638)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/vite-plugin-create-search-index.ts | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/frontend/lib/vite-plugin-create-search-index.ts b/packages/frontend/lib/vite-plugin-create-search-index.ts index 509eb804cb..e194872640 100644 --- a/packages/frontend/lib/vite-plugin-create-search-index.ts +++ b/packages/frontend/lib/vite-plugin-create-search-index.ts @@ -1213,22 +1213,37 @@ async function processVueFile( transformedCodeCache: Record }> { const normalizedId = id.replace(/\\/g, '/'); // ファイルパスを正規化 - // すでにキャッシュに存在する場合は、そのまま返す - if (transformedCodeCache[normalizedId] && transformedCodeCache[normalizedId].includes('markerId=')) { + + // 開発モード時はコード内容に変更があれば常に再処理する + // コード内容が同じ場合のみキャッシュを使用 + const isDevMode = process.env.NODE_ENV === 'development'; + + const s = new MagicString(code); // magic-string のインスタンスを作成 + + if (!isDevMode && transformedCodeCache[normalizedId] && transformedCodeCache[normalizedId].includes('markerId=')) { logger.info(`Using cached version for ${id}`); return { code: transformedCodeCache[normalizedId], - map: null, + map: s.generateMap({ source: id, includeContent: true }), + transformedCodeCache + }; + } + + // すでに処理済みのファイルでコードに変更がない場合はキャッシュを返す + if (transformedCodeCache[normalizedId] === code) { + logger.info(`Code unchanged for ${id}, using cached version`); + return { + code: transformedCodeCache[normalizedId], + map: s.generateMap({ source: id, includeContent: true }), transformedCodeCache }; } - const s = new MagicString(code); // magic-string のインスタンスを作成 const parsed = vueSfcParse(code, { filename: id }); if (!parsed.descriptor.template) { return { code, - map: null, + map: s.generateMap({ source: id, includeContent: true }), transformedCodeCache }; } @@ -1466,16 +1481,21 @@ export default function pluginCreateSearchIndex(options: Options): Plugin { if (isMatch) break; // いずれかのパターンでマッチしたら、outer loop も抜ける } - if (!isMatch) { return; } + // ファイルの内容が変更された場合は再処理を行う + const normalizedId = id.replace(/\\/g, '/'); + const hasContentChanged = !transformedCodeCache[normalizedId] || transformedCodeCache[normalizedId] !== code; + const transformed = await processVueFile(code, id, options, transformedCodeCache); transformedCodeCache = transformed.transformedCodeCache; // キャッシュを更新 - if (isDevServer) { - await analyzeVueProps({ ...options, transformedCodeCache }); // analyzeVueProps を呼び出す + + if (isDevServer && hasContentChanged) { + await analyzeVueProps({ ...options, transformedCodeCache }); // ファイルが変更されたときのみ分析を実行 } + return transformed; },