Transpile server files recursively with babel

2019-08-12 05:06发布

问题:

My folder structure is such:

functions
-- dist
-- private
-- server
---- controllers
---- middleware
------- clientAuth.js
------- someOtherAuth.js
---- index.js
---- model.js
---- router.js

I want to transpile all the .js files in my server folder into .dist. At present in my package.json I have

 "scripts": {
    "prepare": "babel ./server/**/*.js --retain-lines -d ./dist"
  },

This only transpiles the files in the subdirectories in server, but not the files in the root of server. What can I use to transpile and place into .dist all the files in the root and the subdirectories?

回答1:

If you are only going to have .js files in your server, you could replace ./server/**/*.js with ./server/

So you would end up with

"scripts": {
  "prepare": "babel ./server/ --retain-lines -d ./dist"
}

in case you still need just the .js extension, there should be an -x flag to archive that

"scripts": {
  "prepare": "babel ./server/ -x '.js'  --retain-lines -d ./dist"
}