Skip to content
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

Allow typescript configuration files #4105

Closed
wants to merge 4 commits into from
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
6 changes: 6 additions & 0 deletions .changeset/twelve-zebras-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/kit': patch
'kit.svelte.dev': patch
---

Add the ability to use a typescript configuration file. Move the docs site to this `svelte.config.ts`
12 changes: 12 additions & 0 deletions documentation/docs/13-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ const config = {
export default config;
```

### typescript config file

Thanks to [unconfig](https://www.github.com/antfu/unconfig), you can also use a typescript configuration file. Simply rename `svelte.config.js` to `svelte.config.ts` and replace the beginning of the file with:

```ts
/// file: svelte.config.ts
import type { Config } from 'sveltejs/kit';
const config: Config = {
// Contents
}
```

### adapter

Required when running `svelte-kit build` and determines how the output is converted for different platforms. See [Adapters](/docs/adapters).
Expand Down
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"svelte-preprocess": "^4.9.8",
"svelte2tsx": "~0.5.0",
"tiny-glob": "^0.2.9",
"unconfig": "^0.3.1",
"uvu": "^0.5.2"
},
"peerDependencies": {
Expand Down
16 changes: 10 additions & 6 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import path from 'path';
import * as url from 'url';
import { logger } from '../utils.js';
import options from './options.js';
import { loadConfig } from 'unconfig';

/**
* @param {string} cwd
Expand All @@ -28,17 +28,19 @@ export function load_template(cwd, config) {
}

export async function load_config({ cwd = process.cwd() } = {}) {
const config_file = path.join(cwd, 'svelte.config.js');
// load from `svelte.config.xx` where xx is one of ts, mjs, js, json
const { config, sources } = await loadConfig({
sources: [{ files: 'svelte.config' }],
cwd
});

if (!fs.existsSync(config_file)) {
if (!sources.length) {
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);
const validated = validate_config(config);

validated.kit.files.assets = path.resolve(cwd, validated.kit.files.assets);
validated.kit.files.hooks = path.resolve(cwd, validated.kit.files.hooks);
Expand All @@ -55,6 +57,8 @@ export async function load_config({ cwd = process.cwd() } = {}) {
* @returns {import('types').ValidatedConfig}
*/
export function validate_config(config) {
// unconfig returns an empty object if the config file does not return an object,
// so this should never happen. Leaving it here for safety.
if (typeof config !== 'object') {
throw new Error(
'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
Expand Down
22 changes: 9 additions & 13 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,20 +351,16 @@ test('load default config (esm)', async () => {
assert.equal(config, defaults);
});

test('errors on loading config with incorrect default export', async () => {
let message = null;

try {
const cwd = join(__dirname, 'fixtures', 'export-string');
await load_config({ cwd });
} catch (/** @type {any} */ e) {
message = e.message;
}
test('defaults used when using loading config with incorrect default export', async () => {
const cwd = join(__dirname, 'fixtures/export-string');

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

assert.equal(
message,
'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
);
const defaults = get_defaults(cwd + '/');
defaults.kit.version.name = config.kit.version.name;

assert.equal(config, defaults);
});

test.run();
136 changes: 136 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as path from 'path';
import adapter from '@sveltejs/adapter-auto';
import { imagetools } from 'vite-imagetools';

/** @type {import('@sveltejs/kit').Config} */
const config = {
import type { Config } from '@sveltejs/kit';

const config: Config = {
kit: {
adapter: adapter(),

Expand Down