Babel JS-只需将JavaScript STRING转换为es5(使用nodejs) [英] Babel JS -- simply convert JavaScript STRING to es5 (USING nodejs)

查看:68
本文介绍了Babel JS-只需将JavaScript STRING转换为es5(使用nodejs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索很多,发现很多半答案或答案都不能解决我的问题,所以:

I've been googling a lot and found a lot of half-answers or answers that don't address my question, so:

我只是想输入一个输入 string ,该字符串是常规来源的javascript代码(不是nodejs,仅是常规JavaScript),然后将其转换为另一个STRING(不是文件)),其中包含与浏览器兼容的JavaScript(es5或其他版本).

I'm just trying to take an input string, that string being regular-source javascript code (NOT nodejs, JUST regular JavaScript) and convert that into another STRING (not a file) that contains browser-compatible JavaScript (es5 or whatever).

我不是要以es5的身份运行nodejs,也不是要转换单个文件,而是要使用更新的JavaScript的 string 并获取 string 较旧的JavaScript.

I'm NOT trying to run nodejs as es5, and I'm not trying to convert a single file, I want to take a string of newer JavaScript and get a string of older JavaScript.

现在,使用 BabelJS文档,它说要这样做:

Now, using the BabelJS docs, it says to do this:

babel.transform(code, options, function(err, result) {
  result; // => { code, map, ast }
});

在其中创建一个.babelrc文件(并在npm中安装@ babel/preset-env --save-dev)后:

After making a .babelrc file with this in it (and npm install @babel/preset-env --save-dev):

{
  "presets": ["@babel/preset-env"]
}

但是在文档中,并没有说什么选项"应该是只是为了使其起作用.

But on the docs it doesn't say what "options" should be just to get it working.

这里的许多其他帖子说包括 npm install babel-preset-es2015

A bunch of other posts on here said to include npm install babel-preset-es2015 and

module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader?presets[]=es2015'
      }
    ]
  }

但是我认为这是针对不再使用的旧版本

But I think that's for an older version which doesn't work anymore

这个著名的答案提供了以下解决方案:

and this famous answer gives this as a solution:

npm install babel-preset-env

然后运行

babel --presets env proxy.js --out-file proxified.js

或创建一个包含以下内容的.babelrc文件

or create a .babelrc file containing

{
    "presets": [
        "env"
    ]
}

像以前一样运行它.

env在这种情况下是一个预设,基本上说是编译所有ES5的标准ES *行为.如果您使用的是Node版本,支持某些ES6,您可能要考虑做

env in this case is a preset which basically says to compile all standard ES* behavior to ES5. If you are using Node versions that support some ES6, you may want to consider doing

{
    "presets": [
        ["env", { "targets": { "node": "true" } }],
    ]
}

但这只是创建一个新的javascript文件,我只是想从另一个字符串中创建一个字符串.

But that's just making a new javascript FILE I just simply want to make a string from another string.

我认为我需要使用Babel 7,因为这是最新版本,但是我不断收到各种控制台错误..

I think I need to use Babel 7 as that's the newest version, but I keep getting various console errors..

有人能提供一个简单的分步流程来使babel(最好是7)将较新的JavaScript的 string 转换为较旧的 string 吗?/p>

Can someone just provide a simple step-by-step process for getting babel (preferably 7) to convert a string of newer JavaScript to a string of older?

推荐答案

我不确定这是否是您所期望的.

I'm not certain if this is what you've expected.

我有一个简单的项目结构:

I have this simple project structure:

-- package.json
-- index.js
-- .babelrc

index.js

let babel = require('@babel/core')

// some es6 code
let code = `
    let a = () => { console.log('hello') }
`
babel.transform(
    code,
    {
        babelrc: true,
        filename: '.babelrc'
    },
    function(err, result) {
        console.log(result.code)
    }
)

这是控制台输出:

D:\Documents\code\test>node index.js
"use strict";

var a = function a() {
  console.log('hello');
};

D:\Documents\code\test>

和我的 .babelrc & package.json 文件,以备不时之需.

and my .babelrc & package.json files in case you're interested.

{
    "presets": [
        "@babel/preset-env"
    ]
}

我只是按照使用指南进行操作.在这种情况下,不需要 polyfill cli .

I just followed along the usage guide. In this case, polyfill and cli are not required.

{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
    "@babel/polyfill": "^7.2.5"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4"
  }
}

这篇关于Babel JS-只需将JavaScript STRING转换为es5(使用nodejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆