7

I have a quick question on the tsc command being used with the arguments --build --clean, which I understand is used for cleaning /wiping off the .js files generated earlier by the Transpiler (tsc).

What is the speciality or significance of this command? If at all I need to remove all the .js files, I can easily accomplish it through rm -rf *.js or del *.js in the directory.

Can someone educate me on the missing pieces if any?

ruohola
  • 21,987
  • 6
  • 62
  • 97
itsraghz
  • 857
  • 1
  • 11
  • 25

2 Answers2

11

The difference is that rm will happily delete any files, even if they weren't generated by transpiling TypeScript.

ruohola
  • 21,987
  • 6
  • 62
  • 97
  • aah.. Thank you. You mean any other random `.js` files in the directory? But I suppose I don't have any arbitrary .js files than the ones which are transpiled by my tsc. Lemme check that out - by having my own .js file for testing purpose and see what `tsc --build --clean` does. But any other significant differences? – itsraghz Jun 26 '21 at 17:31
  • that is a great magic :) i just quickly tested it out. Any idea how does it differentiate from the .js files it generated and from the rest? – itsraghz Jun 26 '21 at 17:33
  • 1
    @itsraghz `tsc` knows what `.ts` files your project has, so it knows what `.js` files it would create when building. Those are the ones `--clean` will then delete. – ruohola Jun 26 '21 at 17:49
  • Thank you @ruohola. Technically, how does it achieve? does it store a metadata somewhere in order to delete the right .js files - OR only the files corresponds to the .ts files it deletes? Just curious to know the orchestration being used by tsc. – itsraghz Jun 26 '21 at 17:59
  • 2
    I just made a quick test. Looks like it deletes the .js files matching with the .ts files. I created a dummy.ts and ran `tsc` to get a dummy.js and then deleted the original `dummy.tsc` before I ran the `tsc --build --clean` just to find out the fact that the `dummy.js` file is left as it is :) so this must be it? Thank you for the good scenario to play around! Are there any other differences - or is that all? – itsraghz Jun 26 '21 at 18:04
  • 1
    Yeah, it's completely stateless. If you create `dummy.ts` and `dummy.js`, `--clean` will delete the `.js` file even if it wasn't actually created by building the TypeScript. – ruohola Jun 26 '21 at 19:54
  • 1
    `--clean` is ofc always preferable since it's platform agnostic (`rm` won't likely work on Windows), and it cannot accidentally delete some important files. – ruohola Jun 26 '21 at 19:55
  • 1
    Perfect. Thank you @ruohola, I agree with your words. – itsraghz Jul 04 '21 at 08:15
  • but this only deletes files that would be generated in the outdir folder. Is there a way to make it also delete folders that were previously compiled?? – hane Smitter Mar 24 '22 at 10:28
9

The difference is --clean is useless in general. If you have ever renamed, moved, or deleted a source file, clean will not clean up the transpiled output.

Randy Hudson
  • 1,600
  • 1
  • 12
  • 11