Skip to content

Commit

Permalink
Check extensions for param matcher files (#5085)
Browse files Browse the repository at this point in the history
* Check file extensions in create_manifest_data

* fix break to continue

* use endpointExtensions instead `['.js', '.ts']`

* rename endpointExtensions to moduleExtensions

* fix doc

* changeset

* error on encountering endpointExtensions

* add TODO comment

* oops, update tests

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
h8gi and Rich-Harris committed Jun 29, 2022
1 parent 34f10ca commit 2829426
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-suns-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] change endpointExtensions to moduleExtensions, and use to filter param matchers
6 changes: 3 additions & 3 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const config = {
// ...
}
},
endpointExtensions: ['.js', '.ts'],
moduleExtensions: ['.js', '.ts'],
files: {
assets: 'static',
hooks: 'src/hooks',
Expand Down Expand Up @@ -155,9 +155,9 @@ 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
### moduleExtensions

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.
An array of file extensions that SvelteKit will treat as modules. Files with extensions that match neither `config.extensions` nor `config.kit.moduleExtensions` will be ignored by the router.

### files

Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const get_defaults = (prefix = '') => ({
referrer: undefined
}
},
endpointExtensions: ['.js', '.ts'],
endpointExtensions: undefined,
files: {
assets: join(prefix, 'static'),
hooks: join(prefix, 'src/hooks'),
Expand All @@ -88,6 +88,7 @@ const get_defaults = (prefix = '') => ({
parameter: '_method',
allowed: []
},
moduleExtensions: ['.js', '.ts'],
outDir: join(prefix, '.svelte-kit'),
package: {
dir: 'package',
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ const options = object(
})
}),

endpointExtensions: string_array(['.js', '.ts']),
// TODO: remove this for the 1.0 release
endpointExtensions: error(
(keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
),

files: object({
assets: string('static'),
Expand Down Expand Up @@ -161,6 +164,8 @@ const options = object(
})
}),

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

outDir: string('.svelte-kit'),

package: object({
Expand Down
3 changes: 2 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 @@ -82,7 +82,7 @@ export default function create_manifest_data({
});

const routes_base = posixify(path.relative(cwd, config.kit.files.routes));
const valid_extensions = [...config.extensions, ...config.kit.endpointExtensions];
const valid_extensions = [...config.extensions, ...config.kit.moduleExtensions];

if (fs.existsSync(config.kit.files.routes)) {
list_files(config.kit.files.routes).forEach((file) => {
Expand Down Expand Up @@ -267,6 +267,7 @@ export default function create_manifest_data({
if (fs.existsSync(config.kit.files.params)) {
for (const file of fs.readdirSync(config.kit.files.params)) {
const ext = path.extname(file);
if (!config.kit.moduleExtensions.includes(ext)) continue;
const type = file.slice(0, -ext.length);

if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(type)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function write_types(config, manifest_data) {

if (file) {
const ext = /** @type {string} */ (
config.kit.endpointExtensions.find((ext) => file.endsWith(ext))
config.kit.moduleExtensions.find((ext) => file.endsWith(ext))
);
const key = file.slice(0, -ext.length);
shadow_types.set(key, {
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 @@ -107,7 +107,7 @@ export interface KitConfig {
mode?: 'hash' | 'nonce' | 'auto';
directives?: CspDirectives;
};
endpointExtensions?: string[];
moduleExtensions?: string[];
files?: {
assets?: string;
hooks?: string;
Expand Down

0 comments on commit 2829426

Please sign in to comment.