Skip to content

Commit

Permalink
add config.kit.endpointExtensions option (#4197)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Rich-Harris and Conduitry authored Mar 4, 2022
1 parent 9e372c5 commit fa66862
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-dancers-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add config.kit.endpointExtensions option
5 changes: 5 additions & 0 deletions documentation/docs/13-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const config = {
// ...
}
},
endpointExtensions: ['.js', '.ts'],
files: {
assets: 'static',
hooks: 'src/hooks',
Expand Down Expand Up @@ -135,6 +136,10 @@ When pages are prerendered, the CSP header is added via a `<meta http-equiv>` ta

> 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.
### endpointExtensions

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.

### files

An object containing zero or more of the following `string` values:
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const get_defaults = (prefix = '') => ({
referrer: undefined
}
},
endpointExtensions: ['.js', '.ts'],
files: {
assets: join(prefix, 'static'),
hooks: join(prefix, 'src/hooks'),
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const options = object(
})
}),

endpointExtensions: string_array(['.js', '.ts']),

files: object({
assets: string('static'),
hooks: string(join('src', 'hooks')),
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export default function create_manifest_data({
const file = posixify(path.relative(cwd, resolved));
const is_dir = fs.statSync(resolved).isDirectory();

const ext = config.extensions.find((ext) => basename.endsWith(ext)) || path.extname(basename);
const ext = is_dir
? ''
: config.extensions.find((ext) => basename.endsWith(ext)) ||
config.kit.endpointExtensions.find((ext) => basename.endsWith(ext));

if (ext === undefined) return;

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

Expand Down
18 changes: 11 additions & 7 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path';
import { write_if_changed } from './utils.js';

/**
Expand All @@ -25,12 +24,17 @@ export function write_types(config, manifest_data) {
}

manifest_data.routes.forEach((route) => {
if (route.type === 'endpoint') {
const key = route.file.slice(0, -path.extname(route.file).length);
shadow_types.set(key, { params: extract_params(key), type: 'endpoint' });
} else if (route.shadow) {
const key = route.shadow.slice(0, -path.extname(route.shadow).length);
shadow_types.set(key, { params: extract_params(key), type: 'both' });
const file = route.type === 'endpoint' ? route.file : route.shadow;

if (file) {
const ext = /** @type {string} */ (
config.kit.endpointExtensions.find((ext) => file.endsWith(ext))
);
const key = file.slice(0, -ext.length);
shadow_types.set(key, {
params: extract_params(key),
type: route.type === 'endpoint' ? 'endpoint' : 'both'
});
}
});

Expand Down
4 changes: 4 additions & 0 deletions packages/kit/test/apps/basics/src/routes/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* https://github.com/sveltejs/kit/issues/3997 */
h1 {
color: blue;
}
2 changes: 2 additions & 0 deletions packages/kit/test/apps/basics/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</script>

<script>
import './index.css';
/** @type {number} */
export let answer;
</script>
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Config {
mode?: 'hash' | 'nonce' | 'auto';
directives?: CspDirectives;
};
endpointExtensions?: string[];
files?: {
assets?: string;
hooks?: string;
Expand Down

0 comments on commit fa66862

Please sign in to comment.