fix(dev): 検索インデックス対象ファイルでHMRが効かない問題を修正 (#15638)

This commit is contained in:
taichan 2025-03-10 07:45:17 +09:00 committed by GitHub
parent ac21fa7194
commit 88efc0a3be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 8 deletions

View File

@ -1213,22 +1213,37 @@ async function processVueFile(
transformedCodeCache: Record<string, string> transformedCodeCache: Record<string, string>
}> { }> {
const normalizedId = id.replace(/\\/g, '/'); // ファイルパスを正規化 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}`); logger.info(`Using cached version for ${id}`);
return { return {
code: transformedCodeCache[normalizedId], 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 transformedCodeCache
}; };
} }
const s = new MagicString(code); // magic-string のインスタンスを作成
const parsed = vueSfcParse(code, { filename: id }); const parsed = vueSfcParse(code, { filename: id });
if (!parsed.descriptor.template) { if (!parsed.descriptor.template) {
return { return {
code, code,
map: null, map: s.generateMap({ source: id, includeContent: true }),
transformedCodeCache transformedCodeCache
}; };
} }
@ -1466,16 +1481,21 @@ export default function pluginCreateSearchIndex(options: Options): Plugin {
if (isMatch) break; // いずれかのパターンでマッチしたら、outer loop も抜ける if (isMatch) break; // いずれかのパターンでマッチしたら、outer loop も抜ける
} }
if (!isMatch) { if (!isMatch) {
return; return;
} }
// ファイルの内容が変更された場合は再処理を行う
const normalizedId = id.replace(/\\/g, '/');
const hasContentChanged = !transformedCodeCache[normalizedId] || transformedCodeCache[normalizedId] !== code;
const transformed = await processVueFile(code, id, options, transformedCodeCache); const transformed = await processVueFile(code, id, options, transformedCodeCache);
transformedCodeCache = transformed.transformedCodeCache; // キャッシュを更新 transformedCodeCache = transformed.transformedCodeCache; // キャッシュを更新
if (isDevServer) {
await analyzeVueProps({ ...options, transformedCodeCache }); // analyzeVueProps を呼び出す if (isDevServer && hasContentChanged) {
await analyzeVueProps({ ...options, transformedCodeCache }); // ファイルが変更されたときのみ分析を実行
} }
return transformed; return transformed;
}, },