Search code examples
node.jsbabeljs

Can I tell babel to replace an output directory?


The Situation

I'm working on a project in Node.js and using babel to transpile my code. My package.json has a build command defined like this:

  "scripts": {
    "build": "yarn run babel src -d lib",
  },

The Problem

This transpiles fine, taking the content of src and outputing the result to lib, but there are two issues:

  1. lib will contain old files from past transpiles even if they no longer have a matching file in src.
  2. Babel will not rename files with a changed case if my OS is case insensitive. For example, if I had transpiled a file named src/Foo.js and later renamed it to src/foo.js then future transpiles will still be named lib/Foo.js

The Question

Can I tell babel to wipe away the contents of the lib directory before transpiling or do I need to just insert a rm into the build script?


Solution

  • Babel does not have functionality to do this. It is very common to use a rimraf or some other means to delete the directory before running Babel. rm directly is certainly also an option, but that does get more complicated if you want to support Windows too, hence the rimraf usage.