Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/free-bats-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat(cli): wrap links with `resolve()` function to follow [best practices](https://svelte.dev/docs/kit/$app-paths#resolve)
18 changes: 11 additions & 7 deletions packages/addons/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,22 @@ export function addEslintConfigPrettier(content: string): string {
return generateCode();
}

export function addToDemoPage(content: string, path: string): string {
const { template, generateCode } = parseSvelte(content);
export function addToDemoPage(existingContent: string, path: string, typescript: boolean): string {
const { script, template, generateCode } = parseSvelte(existingContent, { typescript });

for (const node of template.ast.childNodes) {
if (node.type === 'tag' && node.attribs['href'] === `/demo/${path}`) {
return content;
// we use inclules as it could be "/demo/${path}" or "resolve("demo/${path}")" or "resolve('demo/${path}')"
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where?
I'm looking everywhere, but but find it 🫣

if (node.type === 'tag' && node.attribs['href'].includes(`/demo/${path}`)) {
return existingContent;
}
}

const newLine = template.source ? '\n' : '';
const src = template.source + `${newLine}<a href="/demo/${path}">${path}</a>`;
return generateCode({ template: src });
imports.addNamed(script.ast, { imports: ['resolve'], from: '$app/paths' });

return generateCode({
script: script.generateCode(),
template: `<a href={resolve('/demo/${path}')}>${path}</a>\n${template.source}`
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/addons/lucia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export default defineAddon({

if (options.demo) {
sv.file(`${kit?.routesDirectory}/demo/+page.svelte`, (content) => {
return addToDemoPage(content, 'lucia');
return addToDemoPage(content, 'lucia', typescript);
});

sv.file(`${kit!.routesDirectory}/demo/lucia/login/+page.server.${ext}`, (content) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/addons/paraglide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default defineAddon({

if (options.demo) {
sv.file(`${kit.routesDirectory}/demo/+page.svelte`, (content) => {
return addToDemoPage(content, 'paraglide');
return addToDemoPage(content, 'paraglide', typescript);
});

// add usage example
Expand Down
7 changes: 4 additions & 3 deletions packages/create/templates/demo/src/routes/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { resolve } from '$app/paths';
import { page } from '$app/state';
import logo from '$lib/images/svelte-logo.svg';
import github from '$lib/images/github.svg';
Expand All @@ -17,13 +18,13 @@
</svg>
<ul>
<li aria-current={page.url.pathname === '/' ? 'page' : undefined}>
<a href="/">Home</a>
<a href={resolve('/')}>Home</a>
</li>
<li aria-current={page.url.pathname === '/about' ? 'page' : undefined}>
<a href="/about">About</a>
<a href={resolve('/about')}>About</a>
</li>
<li aria-current={page.url.pathname.startsWith('/sverdle') ? 'page' : undefined}>
<a href="/sverdle">Sverdle</a>
<a href={resolve('/sverdle')}>Sverdle</a>
</li>
</ul>
<svg viewBox="0 0 2 3" aria-hidden="true">
Expand Down
8 changes: 6 additions & 2 deletions packages/create/templates/demo/src/routes/about/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script lang="ts">
import { resolve } from '$app/paths';
</script>

<svelte:head>
<title>About</title>
<meta name="description" content="About this app" />
Expand All @@ -20,7 +24,7 @@
</p>

<p>
The <a href="/sverdle">Sverdle</a> page illustrates SvelteKit's data loading and form handling. Try
using it with JavaScript disabled!
The <a href={resolve('/sverdle')}>Sverdle</a> page illustrates SvelteKit's data loading and form
handling. Try using it with JavaScript disabled!
</p>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { resolve } from '$app/paths';
import { confetti } from '@neoconfetti/svelte';
import type { ActionData, PageData } from './$types';
import { MediaQuery } from 'svelte/reactivity';
Expand Down Expand Up @@ -117,7 +118,7 @@
};
}}
>
<a class="how-to-play" href="/sverdle/how-to-play">How to play</a>
<a class="how-to-play" href={resolve('/sverdle/how-to-play')}>How to play</a>

<div class="grid" class:playing={!won} class:bad-guess={form?.badGuess}>
{#each Array.from(Array(6).keys()) as row (row)}
Expand Down
5 changes: 3 additions & 2 deletions packages/create/test/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
import { exec, type PromiseWithChild } from 'node:child_process';
import { beforeAll, describe, expect, test } from 'vitest';
import { create, type LanguageType, type TemplateType } from '../index.ts';
import { installAddon, officialAddons } from '../../cli/lib/index.ts';

// Resolve the given path relative to the current file
const resolve_path = (path: string) => fileURLToPath(new URL(path, import.meta.url));
Expand Down Expand Up @@ -42,13 +43,13 @@ for (const template of templates) {
fs.rmSync(cwd, { recursive: true, force: true });

create(cwd, { name: `create-svelte-test-${template}-${types}`, template, types });
await installAddon({ cwd, addons: { eslint: officialAddons.eslint }, options: { eslint: {} } });

const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'));

// run provided scripts that are non-blocking. All of them should exit with 0
// package script requires lib dir
// TODO: lint should run before format
const scripts_to_test = ['format', 'lint', 'check', 'build', 'package'].filter(
const scripts_to_test = ['lint', 'format', 'check', 'build', 'package'].filter(
(s) => s in pkg.scripts
);

Expand Down
Loading