Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use typescript with Babel 7 Standalone

I would like to use babel-standalone (https://babeljs.io/docs/en/next/babel-standalone.html) with typescript.

I tried:

const output = Babel.transform(input, { presets: ['typescript'] }).code;

And:

<script type="text/babel" data-presets="typescript">

But nothing seems to work

I'm loading Babel in my app using: https://unpkg.com/@babel/standalone/babel.min.js

like image 513
Jaroslav Benc Avatar asked May 03 '26 21:05

Jaroslav Benc


1 Answers

I have only tested this with embedded javascript v8 in a C++ project. In order to transpile typescript with Babel standalone, a filename must be specified in the settings passed to Babel.transform, otherwise a javascript error is thrown from Babel about the missing filename.

const output = Babel.transform(
    input,
    {
        "presets": ["typescript"],
        "filename": "example.ts"
    }
).code;

The filename doesn't have to refer to a real file. However the file extension must be .ts for the typescript transpiler to be used.

like image 83
larso Avatar answered May 05 '26 13:05

larso