Skip to content
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
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface OptimizeOptions {
hydrate?: boolean;
}

export interface CompileOptions extends ModuleCompileOptions {
export interface CompileOptions extends Omit<ModuleCompileOptions, 'typeScript'> {
/**
* 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`
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const common = {
export const validate_module_options =
/** @type {Validator<ModuleCompileOptions, ValidatedModuleCompileOptions>} */ (
object({
...common
...common,
typeScript: boolean(false),
})
);

Expand Down
7 changes: 4 additions & 3 deletions packages/svelte/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../export.ts';
Original file line number Diff line number Diff line change
@@ -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) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* module.svelte.ts generated by Svelte VERSION */
import * as $ from 'svelte/internal/client';
import { random } from './export';

export { random };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../export.ts';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as $ from 'svelte/internal/server';
import { random } from './module.svelte';

export default function Typescript_module($$payload) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* module.svelte.ts generated by Svelte VERSION */
import * as $ from 'svelte/internal/server';
import { random } from './export';

export { random };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const random = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
import { random } from './module.svelte';
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { random } from './export';

export { random };
18 changes: 15 additions & 3 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* */
Expand Down Expand Up @@ -788,7 +788,7 @@ declare module 'svelte/compiler' {
hash: (input: string) => string;
}) => string;

export interface CompileOptions extends ModuleCompileOptions {
export interface CompileOptions extends Omit<ModuleCompileOptions, 'typeScript'> {
/**
* 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`
Expand Down Expand Up @@ -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. `<div>` or `<span>`
Expand Down Expand Up @@ -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<ModuleCompileOptions, 'typeScript'> {
/**
* 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`
Expand Down Expand Up @@ -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. `<div>` or `<span>`
Expand Down