I've been googling a lot and found a lot of half-answers or answers that don't address my question, so:
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).
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.
Now, using the BabelJS docs, it says to do this:
babel.transform(code, options, function(err, result) {
result; // => { code, map, ast }
});
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.
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
and run
babel --presets env proxy.js --out-file proxified.js
or create a .babelrc file containing
{ "presets": [ "env" ] }
and run it just like you were before.
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" } }], ] }
But that's just making a new javascript FILE I just simply want to make a string from another string.
I think I need to use Babel 7 as that's the newest version, but I keep getting various console errors..
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?