Babel CLI is extremely slow

2020-08-17 07:37发布

问题:

So I follow the installation here, but babel takes very long time to compile, even small files:

app.js

let app = 1;

.babelrc

{ "presets": ["es2015"] }

package.json

"scripts": {
    "build": "babel app.js -o dist/app.js"
},
"devDependencies": {
    "babel-cli": "^6.4.5",
    "babel-preset-es2015": "^6.3.13"
}

Then npm run build will take ~30s to compile.

I'm using npm@3.3.12

回答1:

You might be compiling node_modules and bower_components too.

You can try adding the ignore property in your projects .babelrc like so:

{
  ...
  "ignore": /(node_modules|bower_components)/
  ...
}

Hope this solves your issue



回答2:

Update September 2019

Found upgrading to Babel 7 solved this. Perhaps try:

$ npm install --save-dev @babel/core @babel/node @babel/preset-env

Your package.json should contain something like:

 "devDependencies": {
    "@babel/core": "^7.6.0",
    "@babel/node": "^7.6.1",
    "@babel/preset-env": "^7.6.0"
  }

My .babelrc file is as follows:

{
  "presets": ["@babel/preset-env"]
}

Now, when I run:

npx babel-node src/index.js

the performance is almost instantaneous (it was taking 20+ seconds with babel 6).

See the babel 7.5 docs for more details on this.

Also, for reference on the upgrade, see this stackoverflow question & answer.