0

Noob question here. Trying to assure myself that babel-preset-env works.

I install babel-core and babel-preset-env:

yarn add --dev babel-core
yarn add --dev babel-preset-env

My package.json has:

"babel": {
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": [
            "IE >= 8"
          ]
        }
      }
    ]
  ]
},

I create a JS script to test:

fs.readFile('my.js', 'utf8', (err, data) => {
  if (err) throw err;

  let babel = require("babel-core");
  let result = babel.transform(data).code;
});

I test with arrow functions in my.js:

new Promise((resolve, reject) => {
  console.log('whatever');
});

No matter how I tweak targets.browsers, the arrow function does not get converted.

1 Answers1

0

Tested this with babel-cli. Works.

It's clear that babel-core (the Javascript API of Babel) does not pick up anything from package.json.

Filed an issue here too: https://github.com/babel/babel/issues/7647

The JS API docs probably needs to be updated.

In order to use babel-preset-env in JS API:

const { execFile } = require('child_process');
const ug = require('uglify-es');
const { execFile } = require('child_process');
execFile('npx', ['babel', 'my.js'], (err, data, stderr) => {
  if (err) throw err; // U get the idea

  let result = ug.minify(data, { mangle: { toplevel: true } }).code;
});