Hi,
I am trying to use Kotlin/JS to develop a Svelte application, since Svelte is basically a compiler, any language targeting Javascript could theoretically be used to create a Svelte Web application. The Svelte framework use a tag to delimit any dynamic behavior in javascript. A project which is called svelte-preprocess
takes what inside the script tag and converts it to pure Javascript. The project already supports coffeescript and typescript.
Here is how a .svelte file looks with javascript:
<script>
let name = 'world';
</script>
<h1>Hello {name}!</h1>
the same in typescript:
<script lang="typescript">
export let name: string = 'world'
</script>
<h1>Hello {name}!</h1>
And what I would like to be able to do in Kotlin:
<script lang="kotlin">
val name = "world"
</script>
<h1>Hello {name}!</h1>
I am close to a solution with Kotlin/JS but the Kotlin/JS compiler is built around the concept of “output file” which is way to tell the compiler where to write the side effect of running it. This way of doing thing does not play nicely with the process used by Svelte,
Here is for example the typescript code handling typescript in svelte-preprocess
:
const {
outputText: code,
sourceMapText: map,
diagnostics,
} = ts.transpileModule(content, {
fileName: filename,
compilerOptions,
reportDiagnostics: options.reportDiagnostics !== false,
transformers: {
before: [importTransformer],
},
});
We can see that the compiler takes a string as input and returns the transformed javascript code, plus the source map and some diagnostics information. The precise definition of the return type is:
declare namespace ts {
interface TranspileOptions {
compilerOptions?: CompilerOptions;
fileName?: string;
reportDiagnostics?: boolean;
moduleName?: string;
renamedDependencies?: MapLike<string>;
transformers?: CustomTransformers;
}
interface TranspileOutput {
outputText: string;
diagnostics?: Diagnostic[];
sourceMapText?: string;
}
function transpileModule(input: string, transpileOptions: TranspileOptions): TranspileOutput;
function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string;
}
Would it be possible to include such changes in the kotlin/JS trans/compiler? What would be the best way to push for such a change? Or, maybe I am wrong, and there is already a way… or a technical solution I am not thinking about. I thought about writing temporary files but this solution seems quite inelegant to me, I may resort to it as a last option though.
I personally think Svelte would be quite a nice addition to the web frameworks supported by Kotlin/JS. It’s a new framework but the potential is definitely there and I think the steps to better integrate with from the Kotlin side are quite minimal.
Thanks!
SĂ©bastien