Skip to content
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

feat(component-meta): extend component meta #5169

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

romhml
Copy link

@romhml romhml commented Feb 10, 2025

Resolves #5168

Introduces a new macro defineComponentMeta to extend component meta with arbitrary values.

<script setup lang="ts">
defineComponentMeta({
  foo: 'bar'
})
</script>

<template>
  <span />
</template>

Results in the following meta:

{
    bar: 'baz',
    props: [ /* ... */ ],
    // ...
}

if (!ts.isPropertyAssignment(property)) return
const key = property.name.getText();
const valueNode = resolveDefaultOptionExpression(property.initializer, ts);
meta[key] = printer?.printNode(ts.EmitHint.Expression, valueNode, ast) ?? valueNode.getText(ast)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KazariEX I'm wondering if it's a good idea to parse nested objects and arrays by traversing the rest of the AST here. I tried to match the parser for prop default values.

On one side it would be nice for end users to receive plain objects / arrays, but might be a bit heavy on performances.

});
}
}
return ts.forEachChild(node, traverse)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit aggressive since it'll walk the entire AST. I'll look into restraining it to the top level of <script setup> and defineComponent.

Copy link
Member

@KazariEX KazariEX Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can parse defineComponentMeta at language-core so that we can read the node directly without repeat traversal, since it's not useful for tsc and IDE.

Copy link
Author

@romhml romhml Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into it!

let meta: Record<string, any> = {};
const snapshot = language.scripts.get(componentPath)?.snapshot!;
const fileText = snapshot.getText(0, snapshot.getLength());
const ast = ts.createSourceFile('/temp', fileText, ts.ScriptTarget.Latest, true);
Copy link
Member

@KazariEX KazariEX Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See:

const vueFile = sourceScript.generated?.root;
const vueDefaults = vueFile && exportName === 'default'
? (vueFile instanceof vue.VueVirtualCode ? readVueComponentDefaultProps(vueFile, printer, ts) : {})
: {};
const tsDefaults = !vueFile ? readTsComponentDefaultProps(
componentPath.slice(componentPath.lastIndexOf('.') + 1), // ts | js | tsx | jsx
snapshot.getText(0, snapshot.getLength()),
exportName,
printer,
ts
) : {};

We can read ast from sourceFile.generated.root._sfc if it's a .vue file, or try sharing parsed ast if it's a .ts file.

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

Successfully merging this pull request may close these issues.

Extend component meta
2 participants