简体   繁体   English

如何在 Deno 中使用 Babel?

[英]How to use Babel in Deno?

At the moment deno bundle does not support targeting older ES version such as ES3, since it's by default targeting ESNext, it makes the bundled code unable to be executed in certain environments.目前deno bundle不支持针对较旧的 ES 版本(如 ES3),因为它默认针对 ESNext,这使得捆绑代码无法在某些环境中执行。

Therefore, I wanted to use Babel to transpile the bundled code to a more compatible version ES.因此,我想使用 Babel 将捆绑的代码转译为更兼容的版本 ES。

You can achieve this by importing Babel standalone from jspm.io .您可以通过从jspm.io独立导入 Babel 来实现此目的。

import babelstandalone from "https://dev.jspm.io/@babel/standalone"

const code = `var x = 5`
const transformed = babelstandalone.transform(code, { 
  presets: ['env'] 
}).code
console.log(transformed)

If you intended to run the transformed code in browser, you will need to import runtime.js .如果您打算在浏览器中运行transformed后的代码,则需要导入runtime.js You can do so by including the following script tag in your HTML file.您可以通过在 HTML 文件中包含以下脚本标记来执行此操作。

<script src="https://unpkg.com/regenerator-runtime@0.13.1/runtime.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM