Skip to content

Commit fa66862

Browse files
add config.kit.endpointExtensions option (#4197)
* add config.kit.endpointExtensions option * lint * docs * Update documentation/docs/13-configuration.md Co-authored-by: Conduitry <[email protected]> Co-authored-by: Conduitry <[email protected]>
1 parent 9e372c5 commit fa66862

File tree

9 files changed

+37
-8
lines changed

9 files changed

+37
-8
lines changed

.changeset/spotty-dancers-clean.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+
Add config.kit.endpointExtensions option

documentation/docs/13-configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const config = {
2929
// ...
3030
}
3131
},
32+
endpointExtensions: ['.js', '.ts'],
3233
files: {
3334
assets: 'static',
3435
hooks: 'src/hooks',
@@ -135,6 +136,10 @@ When pages are prerendered, the CSP header is added via a `<meta http-equiv>` ta
135136

136137
> When `mode` is `'auto'`, SvelteKit will use nonces for dynamically rendered pages and hashes for prerendered pages. Using nonces with prerendered pages is insecure and therefore forbidden.
137138
139+
### endpointExtensions
140+
141+
An array of file extensions that SvelteKit will treat as endpoints. Files with extensions that match neither `config.extensions` nor `config.kit.endpointExtensions` will be ignored by the router.
142+
138143
### files
139144

140145
An object containing zero or more of the following `string` values:

packages/kit/src/core/config/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const get_defaults = (prefix = '') => ({
5454
referrer: undefined
5555
}
5656
},
57+
endpointExtensions: ['.js', '.ts'],
5758
files: {
5859
assets: join(prefix, 'static'),
5960
hooks: join(prefix, 'src/hooks'),

packages/kit/src/core/config/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ const options = object(
9999
})
100100
}),
101101

102+
endpointExtensions: string_array(['.js', '.ts']),
103+
102104
files: object({
103105
assets: string('static'),
104106
hooks: string(join('src', 'hooks')),

packages/kit/src/core/sync/create_manifest_data/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ export default function create_manifest_data({
7474
const file = posixify(path.relative(cwd, resolved));
7575
const is_dir = fs.statSync(resolved).isDirectory();
7676

77-
const ext = config.extensions.find((ext) => basename.endsWith(ext)) || path.extname(basename);
77+
const ext = is_dir
78+
? ''
79+
: config.extensions.find((ext) => basename.endsWith(ext)) ||
80+
config.kit.endpointExtensions.find((ext) => basename.endsWith(ext));
81+
82+
if (ext === undefined) return;
7883

7984
const name = ext ? basename.slice(0, -ext.length) : basename;
8085

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'path';
21
import { write_if_changed } from './utils.js';
32

43
/**
@@ -25,12 +24,17 @@ export function write_types(config, manifest_data) {
2524
}
2625

2726
manifest_data.routes.forEach((route) => {
28-
if (route.type === 'endpoint') {
29-
const key = route.file.slice(0, -path.extname(route.file).length);
30-
shadow_types.set(key, { params: extract_params(key), type: 'endpoint' });
31-
} else if (route.shadow) {
32-
const key = route.shadow.slice(0, -path.extname(route.shadow).length);
33-
shadow_types.set(key, { params: extract_params(key), type: 'both' });
27+
const file = route.type === 'endpoint' ? route.file : route.shadow;
28+
29+
if (file) {
30+
const ext = /** @type {string} */ (
31+
config.kit.endpointExtensions.find((ext) => file.endsWith(ext))
32+
);
33+
const key = file.slice(0, -ext.length);
34+
shadow_types.set(key, {
35+
params: extract_params(key),
36+
type: route.type === 'endpoint' ? 'endpoint' : 'both'
37+
});
3438
}
3539
});
3640

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* https://github.com/sveltejs/kit/issues/3997 */
2+
h1 {
3+
color: blue;
4+
}

packages/kit/test/apps/basics/src/routes/index.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
</script>
1212

1313
<script>
14+
import './index.css';
15+
1416
/** @type {number} */
1517
export let answer;
1618
</script>

packages/kit/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface Config {
4141
mode?: 'hash' | 'nonce' | 'auto';
4242
directives?: CspDirectives;
4343
};
44+
endpointExtensions?: string[];
4445
files?: {
4546
assets?: string;
4647
hooks?: string;

0 commit comments

Comments
 (0)