简体   繁体   中英

Transpiling es6 into es5

Ran into a problem today at work, wondering if anyone could offer some guidance. Im trying to accomplish roughly the following described below.

Files im working with:

  1. config.js : config file using es6 syntax (particularly using import keyword).
  2. script.js : node script written using node version that does not support the use of es6 syntax (particulary using import keyword). script.js aims to read the contents of config.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); 

Question: What do I have to do in script.js so that I can access the object exported by the config.js file. Right now the script is barfing about the fact that I'm trying to use es6 syntax in the file I'm accessing

console.log(config.default.randomConfigOne);

thus your module can export multiple items when you use

export default

it stored at module.default

Check this example 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

babel converts export default {} into

"use strict";

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

in such cases - just check transpiled code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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