From b76e79b801cca72daef5ff27464bdfba43d42155 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Aug 2025 15:34:35 -0400 Subject: [PATCH 1/6] fix: allow `asset(...)` to be used with imported assets --- .changeset/little-impalas-warn.md | 5 +++++ documentation/docs/98-reference/20-$app-types.md | 4 ++-- packages/kit/src/core/sync/write_non_ambient.js | 4 +++- packages/kit/src/runtime/app/paths/types.d.ts | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 .changeset/little-impalas-warn.md diff --git a/.changeset/little-impalas-warn.md b/.changeset/little-impalas-warn.md new file mode 100644 index 000000000000..e879fd8e265f --- /dev/null +++ b/.changeset/little-impalas-warn.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: allow `asset(...)` to be used with imported assets diff --git a/documentation/docs/98-reference/20-$app-types.md b/documentation/docs/98-reference/20-$app-types.md index 51b88c66cdad..1b5ea85682df 100644 --- a/documentation/docs/98-reference/20-$app-types.md +++ b/documentation/docs/98-reference/20-$app-types.md @@ -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.
```dts -type Asset = '/favicon.png' | '/robots.txt'; +type Asset = '/favicon.png' | '/robots.txt' | `/${string}` & {}; ```
diff --git a/packages/kit/src/core/sync/write_non_ambient.js b/packages/kit/src/core/sync/write_non_ambient.js index c2b4a57025c8..b92f772eb1d0 100644 --- a/packages/kit/src/core/sync/write_non_ambient.js +++ b/packages/kit/src/core/sync/write_non_ambient.js @@ -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 {', @@ -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}`;', - `\t\tAsset(): ${manifest_data.assets.map((asset) => s('/' + asset.file)).join(' | ') || 'never'};`, + `\t\tAsset(): ${assets.concat('/${string} & {}').join(' | ')};`, '\t}', '}' ].join('\n'); diff --git a/packages/kit/src/runtime/app/paths/types.d.ts b/packages/kit/src/runtime/app/paths/types.d.ts index 8f0e2f4ebaed..9e7c8e77e5a4 100644 --- a/packages/kit/src/runtime/app/paths/types.d.ts +++ b/packages/kit/src/runtime/app/paths/types.d.ts @@ -56,7 +56,7 @@ export function resolve(...args: ResolveArgs): * import { asset } from '$app/paths'; * * - * a potato + * a potato * ``` * @since 2.26 */ From b2e2ddfa8e8143ffb4d09af2881bdc853c3715fe Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Aug 2025 15:40:20 -0400 Subject: [PATCH 2/6] regenerate --- packages/kit/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index eefee3fce494..6c7fc61b4492 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -2770,7 +2770,7 @@ declare module '$app/paths' { * import { asset } from '$app/paths'; * * - * a potato + * a potato * ``` * @since 2.26 */ From 8440593b2f640816765a8b318e1e0af149fc761c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Aug 2025 15:44:05 -0400 Subject: [PATCH 3/6] oops --- packages/kit/src/core/sync/write_non_ambient.js | 2 +- packages/kit/src/core/sync/write_types/index.spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/core/sync/write_non_ambient.js b/packages/kit/src/core/sync/write_non_ambient.js index b92f772eb1d0..1516c563b6a5 100644 --- a/packages/kit/src/core/sync/write_non_ambient.js +++ b/packages/kit/src/core/sync/write_non_ambient.js @@ -101,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}`;', - `\t\tAsset(): ${assets.concat('/${string} & {}').join(' | ')};`, + `\t\tAsset(): ${assets.concat('`/${string}` & {}').join(' | ')};`, '\t}', '}' ].join('\n'); diff --git a/packages/kit/src/core/sync/write_types/index.spec.js b/packages/kit/src/core/sync/write_types/index.spec.js index 6ea35f7534d9..8de69589607f 100644 --- a/packages/kit/src/core/sync/write_types/index.spec.js +++ b/packages/kit/src/core/sync/write_types/index.spec.js @@ -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`); } } }); From 2e0732e1b590a57d6016c336e5630be3407fca61 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Aug 2025 16:01:00 -0400 Subject: [PATCH 4/6] actually it needs to be this, otherwise it won't work for imported assets --- packages/kit/src/core/sync/write_non_ambient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/sync/write_non_ambient.js b/packages/kit/src/core/sync/write_non_ambient.js index 1516c563b6a5..df55a3cbee0d 100644 --- a/packages/kit/src/core/sync/write_non_ambient.js +++ b/packages/kit/src/core/sync/write_non_ambient.js @@ -101,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}`;', - `\t\tAsset(): ${assets.concat('`/${string}` & {}').join(' | ')};`, + `\t\tAsset(): ${assets.concat('string & {}').join(' | ')};`, '\t}', '}' ].join('\n'); From b771ca401fd32b4347ed878af994d923d2fc1b3d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Aug 2025 16:48:10 -0400 Subject: [PATCH 5/6] Update documentation/docs/98-reference/20-$app-types.md --- documentation/docs/98-reference/20-$app-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/98-reference/20-$app-types.md b/documentation/docs/98-reference/20-$app-types.md index 1b5ea85682df..91642fea3d2a 100644 --- a/documentation/docs/98-reference/20-$app-types.md +++ b/documentation/docs/98-reference/20-$app-types.md @@ -20,7 +20,7 @@ A union of all the filenames of assets contained in your `static` directory, plu
```dts -type Asset = '/favicon.png' | '/robots.txt' | `/${string}` & {}; +type Asset = '/favicon.png' | '/robots.txt' | (string & {}); ```
From 5a7c5513772b5a8d289e0ca01348ccb502d85d78 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 19 Aug 2025 22:59:44 +0200 Subject: [PATCH 6/6] Update documentation/docs/98-reference/20-$app-types.md --- documentation/docs/98-reference/20-$app-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/98-reference/20-$app-types.md b/documentation/docs/98-reference/20-$app-types.md index 91642fea3d2a..97053e762869 100644 --- a/documentation/docs/98-reference/20-$app-types.md +++ b/documentation/docs/98-reference/20-$app-types.md @@ -15,7 +15,7 @@ import type { RouteId, RouteParams, LayoutParams } from '$app/types'; ## Asset -A union of all the filenames of assets contained in your `static` directory, plus a `/${string}` wildcard for asset paths generated from `import` declarations. +A union of all the filenames of assets contained in your `static` directory, plus a `string` wildcard for asset paths generated from `import` declarations.