Allow to access original filename from node transform function
What problem does this feature solve?
I'm writing a node transformation plugin that needs to know origin of the node that is being processed (more specifically, it looks for some specific component usages and dumps information about it, including filename and location, to external JSON file, and then transforms such usages. I can explain it a bit more verbose, if you are interested). Unfortunately, currently TransformContext
is missing any means to extract original file name. It has root nodes, source locations, even the source itself, but not filenames.
What does the proposed API look like?
Introduce new field to TransformContext
that would allow to access the filename. Currently SFCParseOptions
includes filename as it's argument (I'm not sure hovewer whether it is full file path or just name), so it probably should be possible to pass the path down to template compiler.
Looks like it's literally just a two-line patch, since filename is already passed to the compiler, but just excluded from the context for some reason:
diff --git a/packages/compiler-core/src/transform.ts b/packages/compiler-core/src/transform.ts
index 6397df92..ce6091d3 100644
--- a/packages/compiler-core/src/transform.ts
+++ b/packages/compiler-core/src/transform.ts
@@ -83,7 +83,7 @@ export interface ImportItem {
export interface TransformContext
extends Required<
- Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions>
+ Omit<TransformOptions, keyof CompilerCompatOptions>
>,
CompilerCompatOptions {
selfName: string | null
@@ -152,6 +152,7 @@ export function createTransformContext(
const context: TransformContext = {
// options
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
+ filename,
prefixIdentifiers,
hoistStatic,
cacheHandlers,
I tested that and it's indeed passing the filename to the transformation context. I can submit that as a PR, but just want to make sure that I got everything right, because I still don't know the reason why filename is expicitly excluded from the context now.