Babel doesn't compile all the files

Viewed 3244

I'm running a new project with Babel 7 + Typescript. I'm learning Typescript and it was quite a challenge to combine the two of them. I managed to do it, but I still have one issue.

When I compile my project, all .ts files from ./src/ are converted to .js files in ./dist/. All dotfiles are ignored, which is expected. However, all files which don't have the .ts extension in ./src/ are also ignored.

Here are some of the many commands that I've tried:

// package.json
"scripts": {
    // build1 compiles only the .js files
    "build1": "babel src --out-dir dist",

    // build2 compiles only the .ts files
    "build2": "babel src --out-dir dist --extensions \".ts\"",

    //build 3 compiles only the .ts files
    "build3": "babel src --out-dir dist --extensions \".ts, .js\"",

    //build 4 compiles only the .js files
    "build4": "babel src --out-dir dist --extensions \".js, .ts\""
},

Here is my babel config file

// babel.config.js
module.exports = {
  presets: [
    "@babel/env",
    "@babel/typescript",
  ],
  plugins: [
    "transform-dynamic-import",
    "@babel/proposal-class-properties",
    "@babel/proposal-object-rest-spread"
  ],
};

Any idea?

Many thanks!

Update: Removing the space from the --extension argument works, but now I have a side effect which I didn't observe before. When running the command "babel src --out-dir dist --extensions \".ts,.js,.json\"", I see an issue with the .json files which are also converted to .js. I would like babel to transform .ts to .js, compile the .js files and just copy any other file as is.

1 Answers

I finally found the solution. I had to add the --copy-files argument to the command.

babel src --out-dir dist --extensions \".ts,.js\" --copy-files

--extensions \".ts,.js\" tells babel to compile all .ts and .js files.

--copy-files tells babel to copy all the other files.

Related