Replies: 6 comments
-
|
Beta Was this translation helpful? Give feedback.
-
@david-plugge I looked at
So not a solution to getting the file path for the current route. |
Beta Was this translation helpful? Give feedback.
-
Ah got it. I first thought you could use You can however create a custom vite plugin // vite.config.js
import { sveltekit } from '@sveltejs/kit/vite';
/** @type {import('vite').UserConfig} */
const config = {
plugins: [
sveltekit(),
{
name: 'filepath',
transform(code, id) {
return code.replace('FILEPATH', `'${id}'`);
}
}
]
};
export default config; And create the global type // src/app.d.ts
declare const FILEPATH: string; With that in place you can use the global console.log(FILEPATH); |
Beta Was this translation helpful? Give feedback.
-
That's cool! But also not quite what I need. This will get me the path to the file containing the <script lang="ts">
import { page } from '$app/stores'
export let stackblitz: string | boolean | null = null
const stackblitz_url = `https://stackblitz.com/github/user/repo`
$: serving_file = `src/routes${$page.url.pathname}/+page.svx` // or .md or .svelte
$: console.log(import.meta.filepath) // src/components/CodeLinks.svelte
</script>
{#if stackblitz}
<!-- file param defaults to path of file serving the current page if stackblitz=true -->
{@const file = encodeURIComponent(stackblitz == true ? serving_file : stackblitz)}
<a href="{stackblitz_url}?file={file}"> StackBlitz </a>
{/if} That's why I think this has to be a SvelteKit-specific solution (if SvelteKit wants to handle this). |
Beta Was this translation helpful? Give feedback.
-
This is not something we want to add to SvelteKit itself. It sounds more like a dev time / showcase thing and not something that people generally would use. /// file: some/+page.svelte
...
<CodeLinks path={FILEPATH} /> |
Beta Was this translation helpful? Give feedback.
-
You could also use import.meta.glob to get all +page files with a valid extension for pages and find the correct one using route.id. This should work as you are not allowed to have more than one page file in one folder |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the problem
I'm trying to add StackBlitz links to a bunch of example pages that can have different file formats (
.svelte
,.svx
or.md
). Is there a to make this line work for all file types?Describe the proposed solution
Ideally, I could get the path to the page file relative to repo root from the
$page
store:Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
Beta Was this translation helpful? Give feedback.
All reactions