pngwn / MDsveX

A markdown preprocessor for Svelte.

Home Page:https://mdsvex.pngwn.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Layout relative path problem with monorepo and VSCode

Magnus-Ignitic opened this issue · comments

Summary

When adding mdsvex to a mono repo SvelteKit project (where the SvelteKit project resides in a subfolder) I get the following error in source files:

Error: The layout path you provided couldn't be found at either
./src/mdsvex.svelte or /[Workspace root path]/src/mdsvex.svelte.
Please double-check it and try again.

It seems the relative path is based on the workspace root folder and not the location of svelte.config.js.

I only get the error in VSCode. Dev server and building works fine.

svelte.config.js

import adapter from '@sveltejs/adapter-netlify';
import { vitePreprocess } from '@sveltejs/kit/vite';
import path from 'path';
import { mdsvex } from 'mdsvex';

/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexOptions = {
  extensions: ['.md'],
  layout: {
    _: './src/mdsvex.svelte' // ← Also tried with path.resolve('./src/mdsvex.svelte')
  }
}

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://kit.svelte.dev/docs/integrations#preprocessors
  // for more information about preprocessors
  extensions: ['.svelte', '.md'],
  preprocess: [mdsvex(mdsvexOptions), vitePreprocess({})],
  kit: {
    adapter: adapter(),
  // ...
}

Workaround(?)

Using dirname() to get the absolute path to the svelte.config.js seems to work:

const mdsvexOptions = {
  extensions: ['.md'],
  layout: {
    _: dirname(fileURLToPath(import.meta.url)) + '/src/mdsvex.svelte'
  }
}

Thanks for taking the time to write this up @Magnus-Ignitic . I just ran into the same problem and the workaround provided helped me get unstuck.

For anyone else trying to figure out where to get dirname and fileURLToPath from, here you go:

import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';