Skip to content

Support svelte.config.ts #4031

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

Closed
Closed
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/good-brooms-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Support `svelte.config.ts` as Typescript-based config file.
6 changes: 3 additions & 3 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ prog
if (H) throw new Error('-H is no longer supported — use --https instead');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { dev } = await import('./core/dev/index.js');

Expand Down Expand Up @@ -89,7 +89,7 @@ prog
.action(async ({ verbose }) => {
try {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { build } = await import('./core/build/index.js');
const build_data = await build(config);
Expand Down Expand Up @@ -132,7 +132,7 @@ prog
await check_port(port);

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const config = await load_config({ mode: process.env.NODE_ENV });

const { preview } = await import('./core/preview/index.js');

Expand Down
24 changes: 20 additions & 4 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,33 @@ export function load_template(cwd, config) {
return fs.readFileSync(template, 'utf-8');
}

export async function load_config({ cwd = process.cwd() } = {}) {
/**
* @param {{
* cwd?: string;
* mode?: string;
* }} opts
*/
export async function load_config({ cwd = process.cwd(), mode = 'development' } = {}) {
const config_file = path.join(cwd, 'svelte.config.js');
const ts_config_file = path.join(cwd, 'svelte.config.ts');
let config;

if (!fs.existsSync(config_file)) {
if (fs.existsSync(config_file)) {
config = await import(url.pathToFileURL(config_file).href);
} else if (fs.existsSync(ts_config_file)) {
const { loadConfigFromFile } = await import('vite');
const resolved_config = await loadConfigFromFile(
{ command: 'build', mode },
ts_config_file,
cwd
);
config = { default: resolved_config?.config };
} else {
throw new Error(
'You need to create a svelte.config.js file. See https://kit.svelte.dev/docs/configuration'
);
}

const config = await import(url.pathToFileURL(config_file).href);

const validated = validate_config(config.default);

validated.kit.files.assets = path.resolve(cwd, validated.kit.files.assets);
Expand Down