From fa66862171a0424f0e5c8e8e9676d7284fa5b136 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 3 Mar 2022 19:24:53 -0500 Subject: [PATCH] add config.kit.endpointExtensions option (#4197) * add config.kit.endpointExtensions option * lint * docs * Update documentation/docs/13-configuration.md Co-authored-by: Conduitry Co-authored-by: Conduitry --- .changeset/spotty-dancers-clean.md | 5 +++++ documentation/docs/13-configuration.md | 5 +++++ packages/kit/src/core/config/index.spec.js | 1 + packages/kit/src/core/config/options.js | 2 ++ .../core/sync/create_manifest_data/index.js | 7 ++++++- packages/kit/src/core/sync/write_types.js | 18 +++++++++++------- .../kit/test/apps/basics/src/routes/index.css | 4 ++++ .../test/apps/basics/src/routes/index.svelte | 2 ++ packages/kit/types/index.d.ts | 1 + 9 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 .changeset/spotty-dancers-clean.md create mode 100644 packages/kit/test/apps/basics/src/routes/index.css diff --git a/.changeset/spotty-dancers-clean.md b/.changeset/spotty-dancers-clean.md new file mode 100644 index 000000000000..ad0b07025322 --- /dev/null +++ b/.changeset/spotty-dancers-clean.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add config.kit.endpointExtensions option diff --git a/documentation/docs/13-configuration.md b/documentation/docs/13-configuration.md index 154a6e15060f..9727cff24ec9 100644 --- a/documentation/docs/13-configuration.md +++ b/documentation/docs/13-configuration.md @@ -29,6 +29,7 @@ const config = { // ... } }, + endpointExtensions: ['.js', '.ts'], files: { assets: 'static', hooks: 'src/hooks', @@ -135,6 +136,10 @@ When pages are prerendered, the CSP header is added via a `` 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: diff --git a/packages/kit/src/core/config/index.spec.js b/packages/kit/src/core/config/index.spec.js index b2c0f71a8918..03b6b450d583 100644 --- a/packages/kit/src/core/config/index.spec.js +++ b/packages/kit/src/core/config/index.spec.js @@ -54,6 +54,7 @@ const get_defaults = (prefix = '') => ({ referrer: undefined } }, + endpointExtensions: ['.js', '.ts'], files: { assets: join(prefix, 'static'), hooks: join(prefix, 'src/hooks'), diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index b6047884a2d5..d63090abcc76 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -99,6 +99,8 @@ const options = object( }) }), + endpointExtensions: string_array(['.js', '.ts']), + files: object({ assets: string('static'), hooks: string(join('src', 'hooks')), diff --git a/packages/kit/src/core/sync/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js index 51e6cf3ffdd6..bc14c21cb5b5 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.js @@ -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; diff --git a/packages/kit/src/core/sync/write_types.js b/packages/kit/src/core/sync/write_types.js index 2da467d4fb87..be415675221c 100644 --- a/packages/kit/src/core/sync/write_types.js +++ b/packages/kit/src/core/sync/write_types.js @@ -1,4 +1,3 @@ -import path from 'path'; import { write_if_changed } from './utils.js'; /** @@ -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' + }); } }); diff --git a/packages/kit/test/apps/basics/src/routes/index.css b/packages/kit/test/apps/basics/src/routes/index.css new file mode 100644 index 000000000000..58eef34c807a --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/index.css @@ -0,0 +1,4 @@ +/* https://github.com/sveltejs/kit/issues/3997 */ +h1 { + color: blue; +} diff --git a/packages/kit/test/apps/basics/src/routes/index.svelte b/packages/kit/test/apps/basics/src/routes/index.svelte index 692fd50e0d43..c0d95fa284ae 100644 --- a/packages/kit/test/apps/basics/src/routes/index.svelte +++ b/packages/kit/test/apps/basics/src/routes/index.svelte @@ -11,6 +11,8 @@ diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index ca8065de695f..57fca8050080 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -41,6 +41,7 @@ export interface Config { mode?: 'hash' | 'nonce' | 'auto'; directives?: CspDirectives; }; + endpointExtensions?: string[]; files?: { assets?: string; hooks?: string;