admin管理员组文章数量:1332994
In a snippets extension, for a snippet, I need to set an import statement based on the file path.
Essentially the logic should be:
if ($RELATIVE_FILEPATH.startsWith('/app') {
// Next.js with app router / RSC
render 'import {...} from 'react-bricks/rsc'
} else {
render 'import {...} from 'react-bricks/frontend'
}
Is it possible to have such a conditional?
Thank you!
Matteo
In a snippets extension, for a snippet, I need to set an import statement based on the file path.
Essentially the logic should be:
if ($RELATIVE_FILEPATH.startsWith('/app') {
// Next.js with app router / RSC
render 'import {...} from 'react-bricks/rsc'
} else {
render 'import {...} from 'react-bricks/frontend'
}
Is it possible to have such a conditional?
Thank you!
Matteo
- 1 Please see How to Ask. Your title should be a clear, specific question without tags. – isherwood Commented Nov 20, 2024 at 20:25
1 Answer
Reset to default 1You can do that kind of a conditional in vscode snippets. Try this snippet:
"conditional": {
"prefix": "cf",
"body": [
"$RELATIVE_FILEPATH", // just to check what this outputs
"render 'import {...} from 'react-bricks\/${RELATIVE_FILEPATH/^(\\app).*/${1:?rsc:frontend}/}'"
]
}
^(\\app).*
puts the \app
into capture group 1 (if it exists).
${1:?rsc:frontend}
means if there is a capture group 1, output rsc
, else (iff no capture group 1) output frontend
.
Note that vscode is probably returning the path separators as \
rather than /
.
You don't want any part of the RELATIVE_FILEPATH
in your output, so be sure to match it ALL with ^(\\app).*
- you only need to inspect the beginnging which is in capture group 1.
本文标签: visual studio codeVSCode Snippets conditional import based on file pathStack Overflow
版权声明:本文标题:visual studio code - VSCode Snippets: conditional import based on file path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742344237a2457202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论