Skip to content

feat: add a kit.files.src option #14144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-clowns-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add a `kit.files.src` option
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
kit: {
files: {
src: 'source'
}
}
};
11 changes: 11 additions & 0 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ export function validate_config(config) {
}

const validated = options(config, 'config');
const files = validated.kit.files;

files.hooks.client ??= path.join(files.src, 'hooks.client');
files.hooks.server ??= path.join(files.src, 'hooks.server');
files.hooks.universal ??= path.join(files.src, 'hooks');
files.lib ??= path.join(files.src, 'lib');
files.params ??= path.join(files.src, 'params');
files.routes ??= path.join(files.src, 'routes');
files.serviceWorker ??= path.join(files.src, 'service-worker');
files.appTemplate ??= path.join(files.src, 'app.html');
files.errorTemplate ??= path.join(files.src, 'error.html');

if (validated.kit.router.resolution === 'server') {
if (validated.kit.router.type === 'hash') {
Expand Down
13 changes: 13 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const get_defaults = (prefix = '') => ({
remoteFunctions: false
},
files: {
src: join(prefix, 'src'),
assets: join(prefix, 'static'),
hooks: {
client: join(prefix, 'src/hooks.client'),
Expand Down Expand Up @@ -407,3 +408,15 @@ test('errors on loading config with incorrect default export', async () => {
'The Svelte config file must have a configuration object as its default export. See https://svelte.dev/docs/kit/configuration'
);
});

test('uses src prefix for other kit.files options', async () => {
const cwd = join(__dirname, 'fixtures/custom-src');

const config = await load_config({ cwd });
remove_keys(config, ([, v]) => typeof v === 'function');

const defaults = get_defaults(cwd + '/');
defaults.kit.version.name = config.kit.version.name;

expect(config.kit.files.lib).toEqual(join(cwd, 'source/lib'));
});
20 changes: 10 additions & 10 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { join } from 'node:path';
import process from 'node:process';

/** @typedef {import('./types.js').Validator} Validator */
Expand Down Expand Up @@ -125,18 +124,19 @@ const options = object(
}),

files: object({
src: string('src'),
assets: string('static'),
hooks: object({
client: string(join('src', 'hooks.client')),
server: string(join('src', 'hooks.server')),
universal: string(join('src', 'hooks'))
client: string(null),
server: string(null),
universal: string(null)
}),
lib: string(join('src', 'lib')),
params: string(join('src', 'params')),
routes: string(join('src', 'routes')),
serviceWorker: string(join('src', 'service-worker')),
appTemplate: string(join('src', 'app.html')),
errorTemplate: string(join('src', 'error.html'))
lib: string(null),
params: string(null),
routes: string(null),
serviceWorker: string(null),
appTemplate: string(null),
errorTemplate: string(null)
}),

inlineStyleThreshold: number(0),
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/sync/create_manifest_data/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { assert, expect, test } from 'vitest';
import create_manifest_data from './index.js';
import options from '../../config/options.js';
import { sort_routes } from './sort.js';
import { validate_config } from '../../config/index.js';

const cwd = fileURLToPath(new URL('./test', import.meta.url));

Expand All @@ -13,7 +13,7 @@ const cwd = fileURLToPath(new URL('./test', import.meta.url));
* @param {import('@sveltejs/kit').Config} config
*/
const create = (dir, config = {}) => {
const initial = options(config, 'config');
const initial = validate_config(config);

initial.kit.files.assets = path.resolve(cwd, 'static');
initial.kit.files.params = path.resolve(cwd, 'params');
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { assert, expect, test } from 'vitest';
import { rimraf } from '../../../utils/filesystem.js';
import options from '../../config/options.js';
import create_manifest_data from '../create_manifest_data/index.js';
import { tweak_types, write_all_types } from './index.js';
import { validate_config } from '../../config/index.js';

const cwd = fileURLToPath(new URL('./test', import.meta.url));

Expand All @@ -16,7 +16,7 @@ const cwd = fileURLToPath(new URL('./test', import.meta.url));
function run_test(dir) {
rimraf(path.join(cwd, dir, '.svelte-kit'));

const initial = options({}, 'config');
const initial = validate_config({});

initial.kit.files.assets = path.resolve(cwd, 'static');
initial.kit.files.params = path.resolve(cwd, dir, 'params');
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ export interface KitConfig {
* Where to find various files within your project.
*/
files?: {
/**
* the location of your source code
* @default "src"
* @since 2.28
*/
src?: string;
/**
* a place to put static files that should have stable URLs and undergo no processing, such as `favicon.ico` or `manifest.json`
* @default "static"
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ declare module '@sveltejs/kit' {
* Where to find various files within your project.
*/
files?: {
/**
* the location of your source code
* @default "src"
* @since 2.28
*/
src?: string;
/**
* a place to put static files that should have stable URLs and undergo no processing, such as `favicon.ico` or `manifest.json`
* @default "static"
Expand Down
11 changes: 6 additions & 5 deletions packages/package/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ test('create package with typescript using nodenext', async () => {
await test_make_package('typescript-nodenext');
});

// only run this test in older Node versions
// TODO: remove after dropping support for Node 20
const is_node_without_ts_support =
process.versions.node && Number(process.versions.node.split('.', 1)[0]) < 22;
if (!is_node_without_ts_support) {
// only run this test in newer Node versions
// TODO: remove after dropping support for Node < 22.18
const [major, minor] = process.versions.node.split('.', 2).map((str) => +str);
const has_ts_support = major > 22 || (major === 22 && minor >= 18);

if (has_ts_support) {
test('create package with typescript using nodenext and svelte.config.ts', async () => {
await test_make_package('typescript-svelte-config');
});
Expand Down
Loading