Skip to content
Merged
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/little-impalas-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow `asset(...)` to be used with imported assets
4 changes: 2 additions & 2 deletions documentation/docs/98-reference/20-$app-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import type { RouteId, RouteParams, LayoutParams } from '$app/types';

## Asset

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

<div class="ts-block">

```dts
type Asset = '/favicon.png' | '/robots.txt';
type Asset = '/favicon.png' | '/robots.txt' | (string & {});
```

</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/core/sync/write_non_ambient.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ function generate_app_types(manifest_data) {
layouts.push(layout_type);
}

const assets = manifest_data.assets.map((asset) => s('/' + asset.file));

return [
'declare module "$app/types" {',
'\texport interface AppTypes {',
Expand All @@ -99,7 +101,7 @@ function generate_app_types(manifest_data) {
`\t\tLayoutParams(): {\n\t\t\t${layouts.join(';\n\t\t\t')}\n\t\t};`,
`\t\tPathname(): ${Array.from(pathnames).join(' | ')};`,
'\t\tResolvedPathname(): `${"" | `/${string}`}${ReturnType<AppTypes[\'Pathname\']>}`;',
`\t\tAsset(): ${manifest_data.assets.map((asset) => s('/' + asset.file)).join(' | ') || 'never'};`,
`\t\tAsset(): ${assets.concat('string & {}').join(' | ')};`,
'\t}',
'}'
].join('\n');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('Creates correct $types', { timeout: 60000 }, () => {
execSync('pnpm testtypes', { cwd: path.join(cwd, dir) });
} catch (e) {
console.error(/** @type {any} */ (e).stdout.toString());
throw new Error('Type tests failed');
throw new Error(`${dir} type tests failed`);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>):
* import { asset } from '$app/paths';
* </script>
*
* <img alt="a potato" src={asset('potato.jpg')} />
* <img alt="a potato" src={asset('/potato.jpg')} />
* ```
* @since 2.26
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2770,7 +2770,7 @@ declare module '$app/paths' {
* import { asset } from '$app/paths';
* </script>
*
* <img alt="a potato" src={asset('potato.jpg')} />
* <img alt="a potato" src={asset('/potato.jpg')} />
* ```
* @since 2.26
*/
Expand Down
Loading