Skip to content

Commit b983cb3

Browse files
authored
feat(cli): wrap links with resolve() (#754)
* feat(cli): wrap links with `resolve()` * let's just add a linting step to our create flavors * adding `resolve()` also to demo pages * fix typo :)
1 parent 4613ac0 commit b983cb3

File tree

8 files changed

+33
-17
lines changed

8 files changed

+33
-17
lines changed

.changeset/free-bats-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
feat(cli): wrap links with `resolve()` function to follow [best practices](https://svelte.dev/docs/kit/$app-paths#resolve)

packages/addons/common.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ export function addEslintConfigPrettier(content: string): string {
6363
return generateCode();
6464
}
6565

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

6969
for (const node of template.ast.childNodes) {
70-
if (node.type === 'tag' && node.attribs['href'] === `/demo/${path}`) {
71-
return content;
70+
// we use includes as it could be "/demo/${path}" or "resolve("demo/${path}")" or "resolve('demo/${path}')"
71+
if (node.type === 'tag' && node.attribs['href'].includes(`/demo/${path}`)) {
72+
return existingContent;
7273
}
7374
}
7475

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

8084
/**

packages/addons/lucia/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ export default defineAddon({
396396

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

402402
sv.file(`${kit!.routesDirectory}/demo/lucia/login/+page.server.${ext}`, (content) => {

packages/addons/paraglide/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export default defineAddon({
178178

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

184184
// add usage example

packages/create/templates/demo/src/routes/Header.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { resolve } from '$app/paths';
23
import { page } from '$app/state';
34
import logo from '$lib/images/svelte-logo.svg';
45
import github from '$lib/images/github.svg';
@@ -17,13 +18,13 @@
1718
</svg>
1819
<ul>
1920
<li aria-current={page.url.pathname === '/' ? 'page' : undefined}>
20-
<a href="/">Home</a>
21+
<a href={resolve('/')}>Home</a>
2122
</li>
2223
<li aria-current={page.url.pathname === '/about' ? 'page' : undefined}>
23-
<a href="/about">About</a>
24+
<a href={resolve('/about')}>About</a>
2425
</li>
2526
<li aria-current={page.url.pathname.startsWith('/sverdle') ? 'page' : undefined}>
26-
<a href="/sverdle">Sverdle</a>
27+
<a href={resolve('/sverdle')}>Sverdle</a>
2728
</li>
2829
</ul>
2930
<svg viewBox="0 0 2 3" aria-hidden="true">

packages/create/templates/demo/src/routes/about/+page.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<script lang="ts">
2+
import { resolve } from '$app/paths';
3+
</script>
4+
15
<svelte:head>
26
<title>About</title>
37
<meta name="description" content="About this app" />
@@ -20,7 +24,7 @@
2024
</p>
2125

2226
<p>
23-
The <a href="/sverdle">Sverdle</a> page illustrates SvelteKit's data loading and form handling. Try
24-
using it with JavaScript disabled!
27+
The <a href={resolve('/sverdle')}>Sverdle</a> page illustrates SvelteKit's data loading and form
28+
handling. Try using it with JavaScript disabled!
2529
</p>
2630
</div>

packages/create/templates/demo/src/routes/sverdle/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { enhance } from '$app/forms';
3+
import { resolve } from '$app/paths';
34
import { confetti } from '@neoconfetti/svelte';
45
import type { ActionData, PageData } from './$types';
56
import { MediaQuery } from 'svelte/reactivity';
@@ -117,7 +118,7 @@
117118
};
118119
}}
119120
>
120-
<a class="how-to-play" href="/sverdle/how-to-play">How to play</a>
121+
<a class="how-to-play" href={resolve('/sverdle/how-to-play')}>How to play</a>
121122

122123
<div class="grid" class:playing={!won} class:bad-guess={form?.badGuess}>
123124
{#each Array.from(Array(6).keys()) as row (row)}

packages/create/test/check.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
55
import { exec, type PromiseWithChild } from 'node:child_process';
66
import { beforeAll, describe, expect, test } from 'vitest';
77
import { create, type LanguageType, type TemplateType } from '../index.ts';
8+
import { installAddon, officialAddons } from '../../cli/lib/index.ts';
89

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

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

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

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

0 commit comments

Comments
 (0)