0

I write some js files, and use some key words as async, await, lambda expressions and some functions of ecmas6. This work fine in chrome but I need that work in ie 11.

I find an example that transpile the files from src folder to lib, usign babel, but not do some changes, for example, not convert await.

.babelrc

{
    "presets": ["es2015"]
} 

jsconfig.json

{ 
    "compilerOptions":
    { 
        "target": "es6", 
        "module": "commonjs"
    }, 

    "exclude": [ "lib" ] 
} 

package.json

{
      "version": "0.1.0",
      "name": "es6-test",
      "author": "jesus.angulo@outlook.com",
      "devDependencies": 
      {            
          "babel-cli": "*",
          "babel-preset-es2015":"*"
      }
}  

task.json

{
    "version": "0.1.0",
    "command": "${workspaceRoot}/Transpiler/node_modules/.bin/babel",
    "isShellCommand": true,
    "tasks": 
    [
        {
            "taskName": "watch",
            "suppressTaskName": true,
            "isBuildCommand": true,
            "isBackground": true,
            "args": ["${workspaceRoot}/Transpiler/src","--out-dir","${workspaceRoot}/Transpiler/lib","-w" ]
        }
    ]
}

src/example.js

const greetings = ['Welcome', 'Willkommen', 'Hey', 'Gruezi'];

export function next() {
    let idx = Math.floor(Math.random() * greetings.length)
    return greetings[idx];
}

export function nth(n = 0) {
    return greetings[n];
}

function resolveAfter2Seconds(x) { 
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(x);
      }, 2000);
    });
}

async function f1() {
    var x = await resolveAfter2Seconds(10);
    console.log(x); // 10
}
f1();

lib/example.js

function next() {
  var idx = Math.floor(Math.random() * greetings.length);
  return greetings[idx];
}

function nth() {
  var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

  return greetings[n];
}

function resolveAfter2Seconds(x) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve(x);
    }, 2000);
  });
}

async function f1() {
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}

f1();

Which configs I need to do all changes?


UPDATE

I was finally able to fix the transpiler. The changes:

.babelrc

{
  "presets": [
    ["env",{
      "targets": { "browsers": ["last 1 versions"] },
      "debug": true
    }]
  ]
}

package.json

   {
      "version": "0.1.0",
      "name": "es6-test",
      "author": "jesus.angulo@outlook.com",
      "scripts": {
        "build": "babel src -d lib"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1"
  }
}

And execute the transpiler with the command npm run build

dev-cc
  • 434
  • 3
  • 13

1 Answers1

0

async / await is part of the ES2017 spec, so you will need to include the proper preset to get that working properly. Here is a link to the Babel homepage to read more about it:

https://babeljs.io/docs/plugins/preset-es2017/

th3n3wguy
  • 3,649
  • 2
  • 23
  • 30