summaryrefslogtreecommitdiff
path: root/packages/frontend/lib/vite-plugin-create-search-index.ts
blob: cfbba0823c3dc39f4eb80c05d3b0a88a38e6d8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/// <reference lib="esnext" />

import { parse as vueSfcParse } from 'vue/compiler-sfc';
import {
	createLogger,
	type EnvironmentModuleGraph,
	type LogErrorOptions,
	type LogOptions,
	normalizePath,
	type Plugin,
	type PluginOption
} from 'vite';
import fs from 'node:fs';
import JSON5 from 'json5';
import MagicString, { SourceMap } from 'magic-string';
import path from 'node:path'
import { hash, toBase62 } from '../vite.config';
import { minimatch } from 'minimatch';
import {
	type AttributeNode,
	type DirectiveNode,
	type ElementNode,
	ElementTypes,
	NodeTypes,
	type RootNode,
	type SimpleExpressionNode,
	type TemplateChildNode,
} from '@vue/compiler-core';

export interface SearchIndexItem {
	id: string;
	parentId?: string;
	path?: string;
	label: string;
	keywords: string[];
	texts: string[];
	icon?: string;
	inlining?: string[];
}

export type Options = {
	targetFilePaths: string[],
	mainVirtualModule: string,
	modulesToHmrOnUpdate: string[],
	fileVirtualModulePrefix?: string,
	fileVirtualModuleSuffix?: string,
	verbose?: boolean,
};

// マーカー関係を表す型
interface MarkerRelation {
	parentId?: string;
	markerId: string;
	node: ElementNode;
}

// ロガー
let logger = {
	info: (msg: string, options?: LogOptions) => { },
	warn: (msg: string, options?: LogOptions) => { },
	error: (msg: string, options?: LogErrorOptions | unknown) => { },
};
let loggerInitialized = false;

function initLogger(options: Options) {
	if (loggerInitialized) return;
	loggerInitialized = true;
	const viteLogger = createLogger(options.verbose ? 'info' : 'warn');

	logger.info = (msg, options) => {
		msg = `[create-search-index] ${msg}`;
		viteLogger.info(msg, options);
	}

	logger.warn = (msg, options) => {
		msg = `[create-search-index] ${msg}`;
		viteLogger.warn(msg, options);
	}

	logger.error = (msg, options) => {
		msg = `[create-search-index] ${msg}`;
		viteLogger.error(msg, options);
	}
}

//region AST Utility

type WalkVueNode = RootNode | TemplateChildNode | SimpleExpressionNode;

/**
 * Walks the Vue AST.
 * @param nodes
 * @param context The context value passed to callback. you can update context for children by returning value in callback
 * @param callback Returns false if you don't want to walk inner tree
 */
function walkVueElements<C extends {} | null>(nodes: WalkVueNode[], context: C, callback: (node: ElementNode, context: C) => C | undefined | void | false): void {
	for (const node of nodes) {
		let currentContext = context;
		if (node.type === NodeTypes.COMPOUND_EXPRESSION) throw new Error("Unexpected COMPOUND_EXPRESSION");
		if (node.type === NodeTypes.ELEMENT) {
			const result = callback(node, context);
			if (result === false) return;
			if (result !== undefined) currentContext = result;
		}
		if ('children' in node) {
			walkVueElements(node.children, currentContext, callback);
		}
	}
}

function findAttribute(props: Array<AttributeNode | DirectiveNode>, name: string): AttributeNode | DirectiveNode | null {
	for (const prop of props) {
		switch (prop.type) {
			case NodeTypes.ATTRIBUTE:
				if (prop.name === name) {
					return prop;
				}
				break;
			case NodeTypes.DIRECTIVE:
				if (prop.name === 'bind' && prop.arg && 'content' in prop.arg && prop.arg.content === name) {
					return prop;
				}
				break;
		}
	}
	return null;
}

function findEndOfStartTagAttributes(node: ElementNode): number {
	if (node.children.length > 0) {
		// 子要素がある場合、最初の子要素の開始位置を基準にする
		const nodeStart = node.loc.start.offset;
		const firstChildStart = node.children[0].loc.start.offset;
		const endOfStartTag = node.loc.source.lastIndexOf('>', firstChildStart - nodeStart);
		if (endOfStartTag === -1) throw new Error("Bug: Failed to find end of start tag");
		return nodeStart + endOfStartTag;
	} else {
		// 子要素がない場合、自身の終了位置から逆算
		return node.isSelfClosing ? node.loc.end.offset - 1 : node.loc.end.offset;
	}
}

//endregion

/**
 * TypeScriptコード生成
 */
function generateJavaScriptCode(resolvedRootMarkers: SearchIndexItem[]): string {
	return `import { i18n } from '@/i18n.js';\n`
		+ `export const searchIndexes = ${customStringify(resolvedRootMarkers)};\n`;
}

/**
 * オブジェクトを特殊な形式の文字列に変換する
 * i18n参照を保持しつつ適切な形式に変換
 */
function customStringify(obj: unknown): string {
	return JSON.stringify(obj).replaceAll(/"(.*?)"/g, (all, group) => {
		// propertyAccessProxy が i18n 参照を "${i18n.xxx}"のような形に変換してるので、これをそのまま`${i18n.xxx}`
		// のような形にすると、実行時にi18nのプロパティにアクセスするようになる。
		// objectのkeyでは``が使えないので、${ が使われている場合にのみ``に置き換えるようにする
		return group.includes('${') ? '`' + group + '`' : all;
	});
}

// region extractElementText

/**
 * 要素のノードの中身のテキストを抽出する
 */
function extractElementText(node: ElementNode, id: string): string | null {
	return extractElementTextChecked(node, node.tag, id);
}

function extractElementTextChecked(node: ElementNode, processingNodeName: string, id: string): string | null {
	const result: string[] = [];
	for (const child of node.children) {
		const text = extractElementText2Inner(child, processingNodeName, id);
		if (text == null) return null;
		result.push(text);
	}
	return result.join('');
}

function extractElementText2Inner(node: TemplateChildNode, processingNodeName: string, id: string): string | null {
	if (node.type === NodeTypes.COMPOUND_EXPRESSION) throw new Error("Unexpected COMPOUND_EXPRESSION");

	switch (node.type) {
		case NodeTypes.INTERPOLATION: {
			const expr = node.content;
			if (expr.type === NodeTypes.COMPOUND_EXPRESSION) throw new Error(`Unexpected COMPOUND_EXPRESSION`);
			const exprResult = evalExpression(expr.content);
			if (typeof exprResult !== 'string') {
				logger.error(`Result of interpolation node is not string at line ${id}:${node.loc.start.line}`);
				return null;
			}
			return exprResult;
		}
		case NodeTypes.ELEMENT:
			if (node.tagType === ElementTypes.ELEMENT) {
				return extractElementTextChecked(node, processingNodeName, id);
			} else {
				logger.error(`Unexpected ${node.tag} extracting text of ${processingNodeName} ${id}:${node.loc.start.line}`);
				return null;
			}
		case NodeTypes.TEXT:
			return node.content;
		case NodeTypes.COMMENT:
			// We skip comments
			return '';
		case NodeTypes.IF:
		case NodeTypes.IF_BRANCH:
		case NodeTypes.FOR:
		case NodeTypes.TEXT_CALL:
			logger.error(`Unexpected controlflow element extracting text of ${processingNodeName} ${id}:${node.loc.start.line}`);
			return null;
	}
}

// endregion

// region extractUsageInfoFromTemplateAst

/**
 * SearchLabel/SearchText/SearchIconを探して抽出する関数
 */
function extractSugarTags(nodes: TemplateChildNode[], id: string): { label: string | null; texts: string[]; icon: string | null; } {
	let label: string | null | undefined = undefined;
	let icon: string | null | undefined = undefined;
	const texts: string[] = [];

	logger.info(`Extracting labels and texts from ${nodes.length} nodes`);

	walkVueElements(nodes, null, (node) => {
		switch (node.tag) {
			case 'SearchMarker':
				return false; // SearchMarkerはスキップ
			case 'SearchLabel':
				if (label !== undefined) {
					logger.warn(`Duplicate SearchLabel found, ignoring the second one at ${id}:${node.loc.start.line}`);
					break; // 2つ目のSearchLabelは無視
				}

				label = extractElementText(node, id);
				return;
			case 'SearchText':
				const content = extractElementText(node, id);
				if (content) {
					texts.push(content);
				}
				return;
			case 'SearchIcon':
				if (icon !== undefined) {
					logger.warn(`Duplicate SearchIcon found, ignoring the second one at ${id}:${node.loc.start.line}`);
					break; // 2つ目のSearchIconは無視
				}

				if (node.children.length !== 1) {
					logger.error(`SearchIcon must have exactly one child at ${id}:${node.loc.start.line}`);
					return;
				}

				const iconNode = node.children[0];
				if (iconNode.type !== NodeTypes.ELEMENT) {
					logger.error(`SearchIcon must have a child element at ${id}:${node.loc.start.line}`);
					return;
				}
				icon = getStringProp(findAttribute(iconNode.props, 'class'), id);
				return;
		}

		return;
	});

	// デバッグ情報
	logger.info(`Extraction completed: label=${label}, text=[${texts.join(', ')}, icon=${icon}]`);
	return { label: label ?? null, texts, icon: icon ?? null };
}

function getStringProp(attr: AttributeNode | DirectiveNode | null, id: string): string | null {
	switch (attr?.type) {
		case null:
		case undefined:
			return null;
		case NodeTypes.ATTRIBUTE:
			return attr.value?.content ?? null;
		case NodeTypes.DIRECTIVE:
			if (attr.exp == null) return null;
			if (attr.exp.type === NodeTypes.COMPOUND_EXPRESSION) throw new Error('Unexpected COMPOUND_EXPRESSION');
			const value = evalExpression(attr.exp.content ?? '');
			if (typeof value !== 'string') {
				logger.error(`Expected string value, got ${typeof value} at ${id}:${attr.loc.start.line}`);
				return null;
			}
			return value;
	}
}

function getStringArrayProp(attr: AttributeNode | DirectiveNode | null, id: string): string[] | null {
	switch (attr?.type) {
		case null:
		case undefined:
			return null;
		case NodeTypes.ATTRIBUTE:
			logger.error(`Expected directive, got attribute at ${id}:${attr.loc.start.line}`);
			return null;
		case NodeTypes.DIRECTIVE:
			if (attr.exp == null) return null;
			if (attr.exp.type === NodeTypes.COMPOUND_EXPRESSION) throw new Error('Unexpected COMPOUND_EXPRESSION');
			const value = evalExpression(attr.exp.content ?? '');
			if (!Array.isArray(value) || !value.every(x => typeof x === 'string')) {
				logger.error(`Expected string array value, got ${typeof value} at ${id}:${attr.loc.start.line}`);
				return null;
			}
			return value;
	}
}

function extractUsageInfoFromTemplateAst(
	templateAst: RootNode | undefined,
	id: string,
): SearchIndexItem[] {
	const allMarkers: SearchIndexItem[] = [];
	const markerMap = new Map<string, SearchIndexItem>();

	if (!templateAst) return allMarkers;

	walkVueElements<string | null>([templateAst], null, (node, parentId) => {
		if (node.tag !== 'SearchMarker') {
			return;
		}

		// マーカーID取得
		const markerIdProp = node.props?.find(p => p.name === 'markerId');
		const markerId = markerIdProp?.type == NodeTypes.ATTRIBUTE ? markerIdProp.value?.content : null;

		// SearchMarkerにマーカーIDがない場合はエラー
		if (markerId == null) {
			logger.error(`Marker ID not found for node: ${JSON.stringify(node)}`);
			throw new Error(`Marker ID not found in file ${id}`);
		}

		// マーカー基本情報
		const markerInfo: SearchIndexItem = {
			id: markerId,
			parentId: parentId ?? undefined,
			label: '', // デフォルト値
			keywords: [],
			texts: [],
		};

		// バインドプロパティを取得
		const path = getStringProp(findAttribute(node.props, 'path'), id);
		const icon = getStringProp(findAttribute(node.props, 'icon'), id);
		const label = getStringProp(findAttribute(node.props, 'label'), id);
		const inlining = getStringArrayProp(findAttribute(node.props, 'inlining'), id);
		const keywords = getStringArrayProp(findAttribute(node.props, 'keywords'), id);
		const texts = getStringArrayProp(findAttribute(node.props, 'texts'), id);

		if (path) markerInfo.path = path;
		if (icon) markerInfo.icon = icon;
		if (label) markerInfo.label = label;
		if (inlining) markerInfo.inlining = inlining;
		if (keywords) markerInfo.keywords = keywords;
		if (texts) markerInfo.texts = texts;

		// pathがない場合はファイルパスを設定
		if (markerInfo.path == null && parentId == null) {
			markerInfo.path = id.match(/.*(\/(admin|settings)\/[^\/]+)\.vue$/)?.[1];
		}

		// SearchLabelとSearchTextを抽出 (AST全体を探索)
		{
			const extracted = extractSugarTags(node.children, id);
			if (extracted.label && markerInfo.label) logger.warn(`Duplicate label found for ${markerId} at ${id}:${node.loc.start.line}`);
			if (extracted.icon && markerInfo.icon) logger.warn(`Duplicate icon found for ${markerId} at ${id}:${node.loc.start.line}`);
			markerInfo.label = extracted.label ?? markerInfo.label ?? '';
			markerInfo.texts = [...extracted.texts, ...markerInfo.texts];
			markerInfo.icon = extracted.icon ?? markerInfo.icon ?? undefined;
		}

		if (!markerInfo.label) {
			logger.warn(`No label found for ${markerId} at ${id}:${node.loc.start.line}`);
		}

		// マーカーを登録
		markerMap.set(markerId, markerInfo);
		allMarkers.push(markerInfo);
		return markerId;
	});

	return allMarkers;
}

//endregion

//region evalExpression

/**
 * expr を実行します。
 * i18n はそのアクセスを保持するために propertyAccessProxy を使用しています。
 */
function evalExpression(expr: string): unknown {
	const rarResult = Function('i18n', `return ${expr}`)(i18nProxy);
	// JSON.stringify を一回通すことで、 AccessProxy を文字列に変換する
	// Walk してもいいんだけど横着してJSON.stringifyしてる。ビルド時にしか通らないのであんまりパフォーマンス気にする必要ないんで
	return JSON.parse(JSON.stringify(rarResult));
}

const propertyAccessProxySymbol = Symbol('propertyAccessProxySymbol');

type AccessProxy = {
	[propertyAccessProxySymbol]: string[],
	[k: string]: AccessProxy,
}

const propertyAccessProxyHandler: ProxyHandler<AccessProxy> = {
	get(target: AccessProxy, p: string | symbol): any {
		if (p in target) {
			return (target as any)[p];
		}
		if (p == "toJSON" || p == Symbol.toPrimitive) {
			return propertyAccessProxyToJSON;
		}
		if (typeof p == 'string') {
			return target[p] = propertyAccessProxy([...target[propertyAccessProxySymbol], p]);
		}
		return undefined;
	}
}

function propertyAccessProxyToJSON(this: AccessProxy, hint: string) {
	const expression = this[propertyAccessProxySymbol].reduce((prev, current) => {
		if (current.match(/^[a-z][0-9a-z]*$/i)) {
			return `${prev}.${current}`;
		} else {
			return `${prev}['${current}']`;
		}
	});
	return '$\{' + expression + '}';
}

/**
 * プロパティのアクセスを保持するための Proxy オブジェクトを作成します。
 *
 * この関数で生成した proxy は JSON でシリアライズするか、`${}`のように string にすると、 ${property.path} のような形になる。
 * @param path
 */
function propertyAccessProxy(path: string[]): AccessProxy {
	const target: AccessProxy = {
		[propertyAccessProxySymbol]: path,
	};
	return new Proxy(target, propertyAccessProxyHandler);
}

const i18nProxy = propertyAccessProxy(['i18n']);

export function collectFileMarkers(id: string, code: string): SearchIndexItem[] {
	try {
		const { descriptor, errors } = vueSfcParse(code, {
			filename: id,
		});

		if (errors.length > 0) {
			logger.error(`Compile Error: ${id}, ${errors}`);
			return []; // エラーが発生したファイルはスキップ
		}

		return extractUsageInfoFromTemplateAst(descriptor.template?.ast, id);
	} catch (error) {
		logger.error(`Error analyzing file ${id}:`, error);
	}

	return [];
}

// endregion

type TransformedCode = {
	code: string,
	map: SourceMap,
};

export class MarkerIdAssigner {
	// key: file id
	private cache: Map<string, TransformedCode>;

	constructor() {
		this.cache = new Map();
	}

	public onInvalidate(id: string) {
		this.cache.delete(id);
	}

	public processFile(id: string, code: string): TransformedCode {
		// try cache first
		if (this.cache.has(id)) {
			return this.cache.get(id)!;
		}
		const transformed = this.#processImpl(id, code);
		this.cache.set(id, transformed);
		return transformed;
	}

	#processImpl(id: string, code: string): TransformedCode {
		const s = new MagicString(code); // magic-string のインスタンスを作成

		const parsed = vueSfcParse(code, { filename: id });
		if (!parsed.descriptor.template) {
			return {
				code,
				map: s.generateMap({ source: id, includeContent: true }),
			};
		}
		const ast = parsed.descriptor.template.ast; // テンプレート AST を取得
		const markerRelations: MarkerRelation[] = []; //  MarkerRelation 配列を初期化

		if (!ast) {
			return {
				code: s.toString(), // 変更後のコードを返す
				map: s.generateMap({ source: id, includeContent: true }), // ソースマップも生成 (sourceMap: true が必要)
			};
		}

		walkVueElements<string | null>([ast], null, (node, parentId) => {
			if (node.tag !== 'SearchMarker') return;

			const markerIdProp = findAttribute(node.props, 'markerId');

			let nodeMarkerId: string;
			if (markerIdProp != null) {
				if (markerIdProp.type !== NodeTypes.ATTRIBUTE) return logger.error(`markerId must be a attribute at ${id}:${markerIdProp.loc.start.line}`);
				if (markerIdProp.value == null) return logger.error(`markerId must have a value at ${id}:${markerIdProp.loc.start.line}`);
				nodeMarkerId = markerIdProp.value.content;
			} else {
				// ファイルパスと行番号からハッシュ値を生成
				// この際実行環境で差が出ないようにファイルパスを正規化
				const idKey = id.replace(/\\/g, '/').split('packages/frontend/')[1]
				const generatedMarkerId = toBase62(hash(`${idKey}:${node.loc.start.line}`));

				// markerId attribute を追加
				const endOfStartTag = findEndOfStartTagAttributes(node);
				s.appendRight(endOfStartTag, ` markerId="${generatedMarkerId}" data-in-app-search-marker-id="${generatedMarkerId}"`);

				nodeMarkerId = generatedMarkerId;
			}

			markerRelations.push({
				parentId: parentId ?? undefined,
				markerId: nodeMarkerId,
				node: node,
			});

			return nodeMarkerId;
		})

		// 2段階目: :children 属性の追加
		// 最初に親マーカーごとに子マーカーIDを集約する処理を追加
		const parentChildrenMap = new Map<string, string[]>();

		// 1. まず親ごとのすべての子マーカーIDを収集
		markerRelations.forEach(relation => {
			if (relation.parentId) {
				if (!parentChildrenMap.has(relation.parentId)) {
					parentChildrenMap.set(relation.parentId, []);
				}
				parentChildrenMap.get(relation.parentId)!.push(relation.markerId);
			}
		});

		// 2. 親ごとにまとめて :children 属性を処理
		for (const [parentId, childIds] of parentChildrenMap.entries()) {
			const parentRelation = markerRelations.find(r => r.markerId === parentId);
			if (!parentRelation) continue;

			const parentNode = parentRelation.node;
			const childrenProp = findAttribute(parentNode.props, 'children');
			if (childrenProp != null) {
				if (childrenProp.type !== NodeTypes.DIRECTIVE) {
					console.error(`children prop should be directive (:children) at ${id}:${childrenProp.loc.start.line}`);
					continue;
				}

				// AST で :children 属性が検出された場合、それを更新
				const childrenValue = getStringArrayProp(childrenProp, id);
				if (childrenValue == null) continue;

				const newValue: string[] = [...childrenValue];
				for (const childId of [...childIds]) {
					if (!newValue.includes(childId)) {
						newValue.push(childId);
					}
				}

				const expression = JSON.stringify(newValue).replaceAll(/"/g, "'");
				s.overwrite(childrenProp.exp!.loc.start.offset, childrenProp.exp!.loc.end.offset, expression);
				logger.info(`Added ${childIds.length} child markerIds to existing :children in ${id}`);
			} else {
				// :children 属性がまだない場合、新規作成
				const endOfParentStartTag = findEndOfStartTagAttributes(parentNode);
				s.appendRight(endOfParentStartTag, ` :children="${JSON5.stringify(childIds).replace(/"/g, "'")}"`);
				logger.info(`Created new :children attribute with ${childIds.length} markerIds in ${id}`);
			}
		}

		return {
			code: s.toString(), // 変更後のコードを返す
			map: s.generateMap({ source: id, includeContent: true }), // ソースマップも生成 (sourceMap: true が必要)
		};
	}

	async getOrLoad(id: string) {
		// if there already exists a cache, return it
		// note cahce will be invalidated on file change so the cache must be up to date
		let code = this.getCached(id)?.code;
		if (code != null) {
			return code;
		}

		// if no cache found, read and parse the file
		const originalCode = await fs.promises.readFile(id, 'utf-8');

		// Other code may already parsed the file while we were waiting for the file to be read so re-check the cache
		code = this.getCached(id)?.code;
		if (code != null) {
			return code;
		}

		// parse the file
		code = this.processFile(id, originalCode)?.code;
		return code;
	}

	getCached(id: string) {
		return this.cache.get(id);
	}
}

// Rollup プラグインとして export
export default function pluginCreateSearchIndex(options: Options): PluginOption {
	const assigner = new MarkerIdAssigner();
	return [
		createSearchIndex(options, assigner),
		pluginCreateSearchIndexVirtualModule(options, assigner),
	]
}

function createSearchIndex(options: Options, assigner: MarkerIdAssigner): Plugin {
	initLogger(options); // ロガーを初期化
	const root = normalizePath(process.cwd());

	function isTargetFile(id: string): boolean {
		const relativePath = path.posix.relative(root, id);
		return options.targetFilePaths.some(pat => minimatch(relativePath, pat))
	}

	return {
		name: 'autoAssignMarkerId',
		enforce: 'pre',

		watchChange(id) {
			assigner.onInvalidate(id);
		},

		async transform(code, id) {
			if (!id.endsWith('.vue')) {
				return;
			}

			if (!isTargetFile(id)) {
				return;
			}

			return assigner.processFile(id, code);
		},
	};
}

export function pluginCreateSearchIndexVirtualModule(options: Options, asigner: MarkerIdAssigner): Plugin {
	const searchIndexPrefix = options.fileVirtualModulePrefix ?? 'search-index-individual:';
	const searchIndexSuffix = options.fileVirtualModuleSuffix ?? '.ts';
	const allSearchIndexFile = options.mainVirtualModule;
	const root = normalizePath(process.cwd());

	function isTargetFile(id: string): boolean {
		const relativePath = path.posix.relative(root, id);
		return options.targetFilePaths.some(pat => minimatch(relativePath, pat))
	}

	function parseSearchIndexFileId(id: string): string | null {
		const noQuery = id.split('?')[0];
		if (noQuery.startsWith(searchIndexPrefix) && noQuery.endsWith(searchIndexSuffix)) {
			const filePath = id.slice(searchIndexPrefix.length).slice(0, -searchIndexSuffix.length);
			if (isTargetFile(filePath)) {
				return filePath;
			}
		}
		return null;
	}

	return {
		name: 'generateSearchIndexVirtualModule',
		// hotUpdate hook を vite:vue よりもあとに実行したいため enforce: post
		enforce: 'post',

		async resolveId(id) {
			if (id == allSearchIndexFile) {
				return '\0' + allSearchIndexFile;
			}

			const searchIndexFilePath = parseSearchIndexFileId(id);
			if (searchIndexFilePath != null) {
				return id;
			}
			return undefined;
		},

		async load(id) {
			if (id == '\0' + allSearchIndexFile) {
				const files = options.targetFilePaths.map((filePathPattern) => fs.globSync(filePathPattern)).flat();
				let generatedFile = '';
				let arrayElements = '';
				for (let file of files) {
					const normalizedRelative = normalizePath(file);
					const absoluteId = normalizePath(path.join(process.cwd(), normalizedRelative)) + searchIndexSuffix;
					const variableName = normalizedRelative.replace(/[\/.-]/g, '_');
					generatedFile += `import { searchIndexes as ${variableName} } from '${searchIndexPrefix}${absoluteId}';\n`;
					arrayElements += `  ...${variableName},\n`;
				}
				generatedFile += `export let searchIndexes = [\n${arrayElements}];\n`;
				return generatedFile;
			}

			const searchIndexFilePath = parseSearchIndexFileId(id);
			if (searchIndexFilePath != null) {
				// call load to update the index file when the file is changed
				this.addWatchFile(searchIndexFilePath);

				const code = await asigner.getOrLoad(searchIndexFilePath);
				return generateJavaScriptCode(collectFileMarkers(searchIndexFilePath, code));
			}
			return null;
		},

		hotUpdate(this: { environment: { moduleGraph: EnvironmentModuleGraph } }, { file, modules }) {
			if (isTargetFile(file)) {
				const updateMods = options.modulesToHmrOnUpdate.map(id => this.environment.moduleGraph.getModuleById(path.posix.join(root, id))).filter(x => x != null);
				return [...modules, ...updateMods];
			}
			return modules;
		}
	};
}