diff --git a/packages/svelte/src/compiler/index.js b/packages/svelte/src/compiler/index.js index 42427dd9c407..c0ce5de4842f 100644 --- a/packages/svelte/src/compiler/index.js +++ b/packages/svelte/src/compiler/index.js @@ -52,7 +52,7 @@ export function compile(source, options) { } /** - * `compileModule` takes your JavaScript source code containing runes, and turns it into a JavaScript module. + * `compileModule` takes your JavaScript/TypeScript source code containing runes, and turns it into a JavaScript module. * * @param {string} source The component source code * @param {ModuleCompileOptions} options @@ -64,7 +64,7 @@ export function compileModule(source, options) { const validated = validate_module_options(options, ''); state.reset(source, validated); - const analysis = analyze_module(parse_acorn(source, false), validated); + const analysis = analyze_module(parse_acorn(source, options.typeScript ?? false), validated); return transform_module(analysis, source, validated); } diff --git a/packages/svelte/src/compiler/types/index.d.ts b/packages/svelte/src/compiler/types/index.d.ts index 616c346ad35a..d7cf9106a004 100644 --- a/packages/svelte/src/compiler/types/index.d.ts +++ b/packages/svelte/src/compiler/types/index.d.ts @@ -57,7 +57,7 @@ export interface OptimizeOptions { hydrate?: boolean; } -export interface CompileOptions extends ModuleCompileOptions { +export interface CompileOptions extends Omit { /** * Sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). * If unspecified, will be inferred from `filename` @@ -207,6 +207,12 @@ export interface ModuleCompileOptions { * Use this to filter out warnings. Return `true` to keep the warning, `false` to discard it. */ warningFilter?: (warning: Warning) => boolean; + /** + * Indicates whether the source code is in Typescript or JavaScript. + * + * @default false + */ + typeScript?: boolean; } // The following two somewhat scary looking types ensure that certain types are required but can be undefined still diff --git a/packages/svelte/src/compiler/validate-options.js b/packages/svelte/src/compiler/validate-options.js index ab932ed5bca1..6a03a67dbb0f 100644 --- a/packages/svelte/src/compiler/validate-options.js +++ b/packages/svelte/src/compiler/validate-options.js @@ -47,7 +47,8 @@ const common = { export const validate_module_options = /** @type {Validator} */ ( object({ - ...common + ...common, + typeScript: boolean(false), }) ); diff --git a/packages/svelte/tests/helpers.js b/packages/svelte/tests/helpers.js index f853d5873c57..95dbc26ac411 100644 --- a/packages/svelte/tests/helpers.js +++ b/packages/svelte/tests/helpers.js @@ -80,13 +80,14 @@ export async function compile_directory( generate }; - if (file.endsWith('.js')) { + if (file.endsWith('.js') || file.endsWith('.ts')) { const out = `${output_dir}/${file}`; - if (file.endsWith('.svelte.js')) { + if (file.endsWith('.svelte.js') || file.endsWith('.svelte.ts')) { const compiled = compileModule(text, { filename: opts.filename, generate: opts.generate, - dev: opts.dev + dev: opts.dev, + typeScript: file.endsWith('.svelte.ts') }); write(out, compiled.js.code.replace(`v${VERSION}`, 'VERSION')); } else { diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/export.ts b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/export.ts new file mode 100644 index 000000000000..c550022dd49b --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/export.ts @@ -0,0 +1 @@ +export * from '../../export.ts'; \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/index.svelte.js new file mode 100644 index 000000000000..ae07615e30c5 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/index.svelte.js @@ -0,0 +1,8 @@ +import 'svelte/internal/disclose-version'; +import 'svelte/internal/flags/legacy'; +import * as $ from 'svelte/internal/client'; +import { random } from './module.svelte'; + +export default function Typescript_module($$anchor) { + +} \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/module.svelte.ts b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/module.svelte.ts new file mode 100644 index 000000000000..ba5fa5960884 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/client/module.svelte.ts @@ -0,0 +1,5 @@ +/* module.svelte.ts generated by Svelte VERSION */ +import * as $ from 'svelte/internal/client'; +import { random } from './export'; + +export { random }; \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/export.ts b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/export.ts new file mode 100644 index 000000000000..c550022dd49b --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/export.ts @@ -0,0 +1 @@ +export * from '../../export.ts'; \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/index.svelte.js b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/index.svelte.js new file mode 100644 index 000000000000..e1c2fce8a40d --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/index.svelte.js @@ -0,0 +1,6 @@ +import * as $ from 'svelte/internal/server'; +import { random } from './module.svelte'; + +export default function Typescript_module($$payload) { + +} \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/module.svelte.ts b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/module.svelte.ts new file mode 100644 index 000000000000..5f599039fcdd --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/_expected/server/module.svelte.ts @@ -0,0 +1,5 @@ +/* module.svelte.ts generated by Svelte VERSION */ +import * as $ from 'svelte/internal/server'; +import { random } from './export'; + +export { random }; \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/export.ts b/packages/svelte/tests/snapshot/samples/typescript-module/export.ts new file mode 100644 index 000000000000..07ed4a67bd35 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/export.ts @@ -0,0 +1 @@ +export const random = 42; diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/index.svelte b/packages/svelte/tests/snapshot/samples/typescript-module/index.svelte new file mode 100644 index 000000000000..518377177f84 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/index.svelte @@ -0,0 +1,3 @@ + diff --git a/packages/svelte/tests/snapshot/samples/typescript-module/module.svelte.ts b/packages/svelte/tests/snapshot/samples/typescript-module/module.svelte.ts new file mode 100644 index 000000000000..b39abe1b0e45 --- /dev/null +++ b/packages/svelte/tests/snapshot/samples/typescript-module/module.svelte.ts @@ -0,0 +1,3 @@ +import { random } from './export'; + +export { random }; diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index b233cfcc0b58..37c6cab43984 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -636,7 +636,7 @@ declare module 'svelte/compiler' { * */ export function compile(source: string, options: CompileOptions): CompileResult; /** - * `compileModule` takes your JavaScript source code containing runes, and turns it into a JavaScript module. + * `compileModule` takes your JavaScript/TypeScript source code containing runes, and turns it into a JavaScript module. * * @param source The component source code * */ @@ -788,7 +788,7 @@ declare module 'svelte/compiler' { hash: (input: string) => string; }) => string; - export interface CompileOptions extends ModuleCompileOptions { + export interface CompileOptions extends Omit { /** * Sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). * If unspecified, will be inferred from `filename` @@ -938,6 +938,12 @@ declare module 'svelte/compiler' { * Use this to filter out warnings. Return `true` to keep the warning, `false` to discard it. */ warningFilter?: (warning: Warning) => boolean; + /** + * Indicates whether the source code is in Typescript or JavaScript. + * + * @default false + */ + typeScript?: boolean; } /** * - `html` — the default, for e.g. `
` or `` @@ -2661,7 +2667,7 @@ declare module 'svelte/types/compiler/interfaces' { hash: (input: string) => string; }) => string; - interface CompileOptions_1 extends ModuleCompileOptions { + interface CompileOptions_1 extends Omit { /** * Sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). * If unspecified, will be inferred from `filename` @@ -2811,6 +2817,12 @@ declare module 'svelte/types/compiler/interfaces' { * Use this to filter out warnings. Return `true` to keep the warning, `false` to discard it. */ warningFilter?: (warning: Warning_1) => boolean; + /** + * Indicates whether the source code is in Typescript or JavaScript. + * + * @default false + */ + typeScript?: boolean; } /** * - `html` — the default, for e.g. `
` or ``