Skip to content

Allow to access original filename from node transform function #6164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
makkarpov opened this issue Jun 22, 2022 · 2 comments
Open

Allow to access original filename from node transform function #6164

makkarpov opened this issue Jun 22, 2022 · 2 comments
Labels
✨ feature request New feature or request

Comments

@makkarpov
Copy link

makkarpov commented Jun 22, 2022

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.

@makkarpov makkarpov added the ✨ feature request New feature or request label Jun 22, 2022
@makkarpov
Copy link
Author

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.

pohnean added a commit to pohnean/vuejs-core that referenced this issue Feb 3, 2023
@pohnean
Copy link

pohnean commented Feb 3, 2023

This is useful for me as well, I've created a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants