点击查看html编辑器说明文档

babel typescript and flow demoedit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk
提交反馈
嵌入
设置
下载
HTML
CSS
JS
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <div id="output"></div>
<!-- Load Babel -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- Your custom script here -->
<script>
    console.log(Babel.availablePresets)

    // Babel.registerPreset('tsx', {
    //   presets: [
    //     [Babel.availablePresets['typescript'], {allExtensions: true, isTSX: true}]],
    //   },
    // );

    var input = 'const getMessage = (str: string) => <div>123132</div>;';
    var output = Babel.transform(input, {
        presets: [
            "env",
            ['typescript', {
                allExtensions: true,
                isTSX: true
            }],
            "react"
        ]
    }).code;

    console.log(output)


    var input2 = `
function foo(x: ?number): string {
  if (x) {
    return <div>{x}</div>;           
  }
  return "default string";
}
`;
    var output2 = Babel.transform(input2, {
        presets: [
            "env",
            // ['typescript', {allExtensions: true, isTSX: true}],
            'flow',
            "react"
        ]
    }).code;

    console.log(output2)
</script>
        
</body>
预览
控制台