Skip to content

Commit 6d7505d

Browse files
fix: allow asset(...) to be used with imported assets (#14270)
* fix: allow `asset(...)` to be used with imported assets * regenerate * oops * actually it needs to be this, otherwise it won't work for imported assets * Update documentation/docs/98-reference/20-$app-types.md * Update documentation/docs/98-reference/20-$app-types.md --------- Co-authored-by: Simon H <[email protected]>
1 parent c5a9794 commit 6d7505d

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

.changeset/little-impalas-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: allow `asset(...)` to be used with imported assets

documentation/docs/98-reference/20-$app-types.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import type { RouteId, RouteParams, LayoutParams } from '$app/types';
1515

1616
## Asset
1717

18-
A union of all the filenames of assets contained in your `static` directory.
18+
A union of all the filenames of assets contained in your `static` directory, plus a `string` wildcard for asset paths generated from `import` declarations.
1919

2020
<div class="ts-block">
2121

2222
```dts
23-
type Asset = '/favicon.png' | '/robots.txt';
23+
type Asset = '/favicon.png' | '/robots.txt' | (string & {});
2424
```
2525

2626
</div>

packages/kit/src/core/sync/write_non_ambient.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ function generate_app_types(manifest_data) {
9191
layouts.push(layout_type);
9292
}
9393

94+
const assets = manifest_data.assets.map((asset) => s('/' + asset.file));
95+
9496
return [
9597
'declare module "$app/types" {',
9698
'\texport interface AppTypes {',
@@ -99,7 +101,7 @@ function generate_app_types(manifest_data) {
99101
`\t\tLayoutParams(): {\n\t\t\t${layouts.join(';\n\t\t\t')}\n\t\t};`,
100102
`\t\tPathname(): ${Array.from(pathnames).join(' | ')};`,
101103
'\t\tResolvedPathname(): `${"" | `/${string}`}${ReturnType<AppTypes[\'Pathname\']>}`;',
102-
`\t\tAsset(): ${manifest_data.assets.map((asset) => s('/' + asset.file)).join(' | ') || 'never'};`,
104+
`\t\tAsset(): ${assets.concat('string & {}').join(' | ')};`,
103105
'\t}',
104106
'}'
105107
].join('\n');

packages/kit/src/core/sync/write_types/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test('Creates correct $types', { timeout: 60000 }, () => {
4646
execSync('pnpm testtypes', { cwd: path.join(cwd, dir) });
4747
} catch (e) {
4848
console.error(/** @type {any} */ (e).stdout.toString());
49-
throw new Error('Type tests failed');
49+
throw new Error(`${dir} type tests failed`);
5050
}
5151
}
5252
});

packages/kit/src/runtime/app/paths/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>):
5656
* import { asset } from '$app/paths';
5757
* </script>
5858
*
59-
* <img alt="a potato" src={asset('potato.jpg')} />
59+
* <img alt="a potato" src={asset('/potato.jpg')} />
6060
* ```
6161
* @since 2.26
6262
*/

packages/kit/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2770,7 +2770,7 @@ declare module '$app/paths' {
27702770
* import { asset } from '$app/paths';
27712771
* </script>
27722772
*
2773-
* <img alt="a potato" src={asset('potato.jpg')} />
2773+
* <img alt="a potato" src={asset('/potato.jpg')} />
27742774
* ```
27752775
* @since 2.26
27762776
*/

0 commit comments

Comments
 (0)