From fe68e2134f716163cc578d0d13a58a20cac43e9a Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:22:16 +0700 Subject: [PATCH 1/6] feat: add support for svelte.config.ts --- .../fixtures/typescript/svelte.config.ts | 5 +++ packages/kit/src/core/config/index.js | 40 +++++++++++++++---- packages/kit/src/core/config/index.spec.js | 14 ++++++- 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 packages/kit/src/core/config/fixtures/typescript/svelte.config.ts diff --git a/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts b/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts new file mode 100644 index 000000000000..4ee2e713b05e --- /dev/null +++ b/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts @@ -0,0 +1,5 @@ +export default { + preprocess: [], + extensions: ['.svelte'], + kit: {} +}; diff --git a/packages/kit/src/core/config/index.js b/packages/kit/src/core/config/index.js index 7d4817fce5dc..fe8e81b6c3a9 100644 --- a/packages/kit/src/core/config/index.js +++ b/packages/kit/src/core/config/index.js @@ -2,6 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import * as url from 'node:url'; import options from './options.js'; +import { transpileModule } from 'typescript'; /** * Loads the template (src/app.html by default) and validates that it has the @@ -56,26 +57,49 @@ export function load_error_page(config) { } /** - * Loads and validates svelte.config.js + * Loads and validates svelte.config.js or svelte.config.ts * @param {{ cwd?: string }} options * @returns {Promise} */ export async function load_config({ cwd = process.cwd() } = {}) { - const config_file = path.join(cwd, 'svelte.config.js'); + const js_config_file = path.join(cwd, 'svelte.config.js'); + const ts_config_file = path.join(cwd, 'svelte.config.ts'); - if (!fs.existsSync(config_file)) { - return process_config({}, { cwd }); + if (fs.existsSync(js_config_file)) { + const config = await import(`${url.pathToFileURL(js_config_file).href}?ts=${Date.now()}`); + return process_config(config.default, { cwd }); } - const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`); + if (fs.existsSync(ts_config_file)) { + const config = await load_ts_config(ts_config_file); + console.log(config); + return process_config(config, { cwd }); + } + return process_config({}, { cwd }); +} + +/** + * Loads and transpiles the TypeScript configuration file + * @param {string} ts_config_file + * @returns {Promise} + */ +async function load_ts_config(ts_config_file) { try { - return process_config(config.default, { cwd }); + const ts_code = fs.readFileSync(ts_config_file, 'utf-8'); + const js_code = transpileModule(ts_code, { + compilerOptions: { module: 99, target: 99 } + }).outputText; + + const config = await import( + `data:text/javascript;base64,${Buffer.from(js_code).toString('base64')}` + ); + return config.default; } catch (e) { const error = /** @type {Error} */ (e); // redact the stack trace — it's not helpful to users - error.stack = `Could not load svelte.config.js: ${error.message}\n`; + error.stack = `Could not load svelte.config.ts: ${error.message}\n`; throw error; } } @@ -110,7 +134,7 @@ function process_config(config, { cwd = process.cwd() } = {}) { export function validate_config(config) { 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' + 'svelte.config.js or svelte.config.ts must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration' ); } diff --git a/packages/kit/src/core/config/index.spec.js b/packages/kit/src/core/config/index.spec.js index e5639defc4c2..da39ae39bc48 100644 --- a/packages/kit/src/core/config/index.spec.js +++ b/packages/kit/src/core/config/index.spec.js @@ -360,6 +360,18 @@ test('load default config (esm)', async () => { expect(config).toEqual(defaults); }); +test('load default typescript config (esm)', async () => { + const cwd = join(__dirname, 'fixtures/typescript'); + + 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).toEqual(defaults); +}); + test('errors on loading config with incorrect default export', async () => { let message = null; @@ -372,6 +384,6 @@ test('errors on loading config with incorrect default export', async () => { assert.equal( message, - 'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration' + 'svelte.config.js or svelte.config.ts must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration' ); }); From 085a225b5b2261b1aeab79bd0917da44befd73d8 Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:24:01 +0700 Subject: [PATCH 2/6] docs: add changeset --- .changeset/tall-adults-film.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tall-adults-film.md diff --git a/.changeset/tall-adults-film.md b/.changeset/tall-adults-film.md new file mode 100644 index 000000000000..647d5e91cf05 --- /dev/null +++ b/.changeset/tall-adults-film.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': major +--- + +add support for svelte.config.ts From 5d472d026b7bd7be161c5a6b022e180546e90532 Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:29:24 +0700 Subject: [PATCH 3/6] minor cleanups --- packages/kit/src/core/config/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/core/config/index.js b/packages/kit/src/core/config/index.js index fe8e81b6c3a9..9224a3c51687 100644 --- a/packages/kit/src/core/config/index.js +++ b/packages/kit/src/core/config/index.js @@ -72,8 +72,7 @@ export async function load_config({ cwd = process.cwd() } = {}) { if (fs.existsSync(ts_config_file)) { const config = await load_ts_config(ts_config_file); - console.log(config); - return process_config(config, { cwd }); + return process_config(config.default, { cwd }); } return process_config({}, { cwd }); @@ -94,7 +93,7 @@ async function load_ts_config(ts_config_file) { const config = await import( `data:text/javascript;base64,${Buffer.from(js_code).toString('base64')}` ); - return config.default; + return config; } catch (e) { const error = /** @type {Error} */ (e); From 63242d7ac17841d24bac1d328e693000eda4df2b Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:32:22 +0700 Subject: [PATCH 4/6] try to fix workflow --- packages/kit/src/core/config/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/core/config/index.js b/packages/kit/src/core/config/index.js index 9224a3c51687..fc2160f46d8b 100644 --- a/packages/kit/src/core/config/index.js +++ b/packages/kit/src/core/config/index.js @@ -2,7 +2,8 @@ import fs from 'node:fs'; import path from 'node:path'; import * as url from 'node:url'; import options from './options.js'; -import { transpileModule } from 'typescript'; +import pkg from 'typescript'; +const { transpileModule } = pkg; /** * Loads the template (src/app.html by default) and validates that it has the From b31554a11665e38f150b76b2ab6bcf80cef2abad Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:35:09 +0700 Subject: [PATCH 5/6] try to fix workflow --- .../src/core/config/fixtures/typescript/svelte.config.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts b/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts index 4ee2e713b05e..ff8b4c56321a 100644 --- a/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts +++ b/packages/kit/src/core/config/fixtures/typescript/svelte.config.ts @@ -1,5 +1 @@ -export default { - preprocess: [], - extensions: ['.svelte'], - kit: {} -}; +export default {}; From c5ed56c11ae067d31ee17db132ab3cf577599733 Mon Sep 17 00:00:00 2001 From: fzn0x Date: Wed, 10 Jul 2024 09:39:25 +0700 Subject: [PATCH 6/6] check types --- packages/kit/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index e3bf549b2407..91d447e829f0 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1840,7 +1840,7 @@ declare module '@sveltejs/kit' { class Redirect_1 { constructor(status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308, location: string); - status: 301 | 302 | 303 | 307 | 308 | 300 | 304 | 305 | 306; + status: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308; location: string; } }