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?
I'm not certain if this is what you've expected.
I have this simple project structure:
-- package.json
-- index.js
-- .babelrc
index.js
let babel = require('@babel/core')
// some es6 code
let code = `
let a = () => { console.log('hello') }
`
babel.transform(
code,
{
babelrc: true,
filename: '.babelrc'
},
function(err, result) {
console.log(result.code)
}
)
and this is the console output:
D:\Documents\code\test>node index.js
"use strict";
var a = function a() {
console.log('hello');
};
D:\Documents\code\test>
and my .babelrc
& package.json
files in case you're interested.
{
"presets": [
"@babel/preset-env"
]
}
I just followed along the usage guide. In this case, polyfill
and cli
are not required.
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"@babel/polyfill": "^7.2.5"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4"
}
}
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.