将 es6 转换为 es5

Transpiling es6 into es5

本文关键字:es5 转换 es6      更新时间:2023-09-26

今天在工作中遇到了一个问题,想知道是否有人可以提供一些指导。我试图大致完成下面描述的以下内容。

我正在使用的文件:

  1. config.js :使用 es6 语法的配置文件(特别是使用 import 关键字)。
  2. script.js :使用不支持使用 es6 语法的节点版本编写的节点脚本(特别是使用 import 关键字)。 脚本.js旨在读取配置的内容.js

    //config.js
    import _ from 'npm:lodash'
    import foo from '../otherRandomFile.js'
    var configObject = {
      randomConfigOne: true,
      randomConfigTwo: false
    }
    export default configObject;
    //script.js
    var config = require('../app/config.js)
    //cannot console log as below because script barfs
    //due to import being a reserved wrk
    console.log(config.randomConfigOne);
    

问题:我必须在脚本中做什么.js以便我可以访问 config.js 文件导出的对象。现在,脚本正在喋喋不休地谈论我正在尝试在我正在访问的文件中使用 es6 语法的事实

console.log(config.default.randomConfigOne);

因此,您的模块可以在使用时导出多个项目

export default

它存储在module.default

查看此示例 https://babeljs.io/repl/#?evaluate=false&lineWrap=true&presets=es2015%2Ces2015-loose%2Cstage-0%2Cstage-1%2Cstage-2%2Cstage-3&experimental=true&loose=false&spec=true&code=export%20default%20%7B%0A%20%20%0A%7D

巴别塔皈依者 export default {}

"使用严格";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = {};

在这种情况下 - 只需检查转译的代码