63

I am following Angular 2 quick start tutorial. Following is my folder structure -

├── gulpfile.js
├── index.html
├── js
|   ├── app.component.js
|   └── boot.js
├── source
|   ├── app.component.ts
|   └── boot.ts
├── node_modules
    ├── module 1
    └── module 2

My typescript files are in source/ directory. I'm compiling it to js/ directory. I'm using gulp-typescript.

The problem is when I, for example, rename the file boot.ts to bootstrap.ts and compile again, corresponding bootstrap.js file is created but the old boot.js file still remains in the js/ directory.

Now the folder structure looks like following-

├── gulpfile.js
├── index.html
├── js
|   ├── app.component.js
|   └── bootstrap.js
|   └── boot.js
├── source
|   ├── app.component.ts
|   └── bootstrap.ts
├── node_modules
    ├── module 1
    └── module 2

I want to delete this boot.js autonomically via gulp task. How to achieve this?

ksharifbd
  • 5,102
  • 2
  • 18
  • 23

11 Answers11

83

with the latest tsc, you should be fine to do the clean with below command

tsc --build --clean

My current tsc version for your reference

$ tsc --version
Version 3.5.3

Note that --clean flag is part of project-references and works only with it.

BMW
  • 42,880
  • 12
  • 99
  • 116
  • 28
    Nope, does not work. At least not for me. `tsc --version: Version 3.5.3` and `tsc --build --clean` does not do what is asked. – Elias Aug 23 '19 at 06:47
  • 2
    i did it as daily task, not sure why it doesn't work in your environment, any details for your issue? – BMW Oct 16 '19 at 23:19
  • 8
    cannot see that "--clean" is part of the compiler options. https://www.typescriptlang.org/docs/handbook/compiler-options.html – Felix K. Nov 04 '19 at 21:55
  • 1
    Seems it is hide option? did you try with it in your environment? – BMW Nov 05 '19 at 03:07
  • 4
    @Elias --clean flag is part of project-references and works only with it. – ZiiMakc Nov 12 '19 at 22:21
  • 23
    --clean doesn't clean all outDir (deleted TS files remain compiled in outDir, but not cleaned...) – Aure77 Apr 14 '20 at 17:42
  • 1
    This is not correct, --clean will not remove the transpiled files from a previous run it will remove any files generated as part of this transpile according to its documentation. – Ed Bishop Feb 22 '23 at 13:39
75

I came here seeing the title, and adding gulp into the mix was not a solution. So here is how I solved it.

Prefix your npm build script with rm -rf ./js/ &&

"scripts": {
    ...
    "build": "rm -rf ./js/ && tsc",
    ...
},

rm -rf ./js/ forcefully removes recursively all files and dirs below ./js/ doku rm in bash

&& says, if successful do the next command && in bash

Title at the time of answering: "How to delete compiled JS files from previous typescript(.ts) files?"

Akash
  • 13,107
  • 1
  • 25
  • 19
  • 5
    If you are using a windows dev environment, you can also use `rm` if you add `cash-rm` as a dev dependency. Similarly, you can use `rimraf` (aptly named for `rm -rf`) instead if saved as a dev dependency. – samthecodingman Aug 09 '17 at 07:55
  • 8
    @samthecodingman For the record, I am a Windows developer by choice :) I have tried Mac and Linux, I really don't feel at home on either of them. I just don't like them, even MacOS. Everything is smooth (for me, at least) in Windows, no matter how bad most developers seem to hate it. – Jeremy Thille Jan 11 '18 at 14:56
  • 14
    `rimraf` is for windows machine. ```"prebuild":"rimraf dist", "build": "tsc -p tsconfig.release.json",``` – Rupesh Kumar Tiwari Mar 14 '18 at 14:43
  • 2
    Works also for server-side projects and projects that are not using gulp. – Lazarus Rising Dec 30 '19 at 10:53
23

This solution is inspired in @Akash Kurian Jose's answer. Since his solution depends on the OS you're using, I'm adding this one which will work on any OS:

Add rimraf as a dev dependency:

"devDependencies": {
   "rimraf": "^3.0.1"
}

Then add these 3 scripts to your package.json:

"rimraf": "./node_modules/rimraf/bin.js",
"clean" : "rimraf js/",
"compile": "npm run clean && tsc -p ./"

When executing npm run compile the js/ folder will be cleaned before compiling.

GabrielBB
  • 2,479
  • 1
  • 35
  • 49
9

Install gulp del.

$ npm install --save-dev gulp del

Create the task.

var gulp = require('gulp');
var del = require('del');

gulp.task('clean:output', function () {
  return del([
    'js/'
  ]);
});

gulp.task('default', ['clean:output']);

You can find more info on gulp del here.

toskv
  • 30,680
  • 7
  • 72
  • 74
  • 1
    This seems to be MUCH more complicated than https://stackoverflow.com/a/42838151/4693430. Why do you recommend this anyway? – Xanlantos May 17 '21 at 08:24
  • 2
    because at the time I didn't know better. I agree that the other solution is a lot simpler and better. It's up to the OP to change the accepted answer to that one. I hope he does. :) – toskv May 19 '21 at 11:05
7

This solution is an improvement on GabrielBB solution.

Install rimraf npm i rimraf -D

Add the following npm scripts

"clean": "rimraf js/",
"prebuild": "npm run clean",
"build": "tsc",

This makes use of npms automatic Pre scripts

Llewellyn Collins
  • 2,243
  • 2
  • 23
  • 37
2

Windows users can add the following prebuild script on package.json - a batch cmd script - which will run automatically before build command, just as @Llewellyn Collins stated:

package.json:
{
  "scripts": {
    "prebuild": "if exist <outDir> (rmdir /Q /S <outDir>)",
    "build": "tsc"
  },
}

Replace "<outDir>" with the actual outDir declared in your tsconfig.json.

If you omit the if exist command, it will throw an error.

It should run correctly even if your main terminal application is PowerShell.

Dami
  • 119
  • 1
  • 3
1

I use script from here.

In package.json add script:

"build": "node jsDelete.js && npx tsc -w"

And add jsDelete.js:

const fs = require('fs');
const Path = require('path');

const deleteFolderRecursive = function(path) {
  if (fs.existsSync(path)) {
    fs.readdirSync(path).forEach((file, index) => {
      const curPath = Path.join(path, file);
      if (fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
};

deleteFolderRecursive('path_to_folder')
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
1

Just if someone will come across this question: I was trying to use tsc --build --clean but sometimes if you renamed a lot of .ts files this command is working on mac:

to delete .js files if you renamed a lot of .ts (this is for Mac users):

find . -name '*.js' ! -path './node_modules/*' -delete

if declaration: true in your tsconfig file you may want to:

find . -name '*.d.ts' ! -path './node_modules/*' -delete

don't forget to git commit before deleting files

Anatoli Klamer
  • 2,279
  • 2
  • 17
  • 22
0

Hack using Git and Bash

git add . && git stash &&  find . \\( -name '*.js' -o -name '*.js.map' \\) ! -path './node_modules/*' -delete  && git restore . && git stash pop
Singh Lalit
  • 471
  • 5
  • 9
0

simple move to ur src file and the run the remove file with this pattern

cd src
rm -rf */**/*.js
AhmedZeno
  • 21
  • 3
-1

You can run: tsc -b YOUR_ENTRYPOINT_LIST --clean as mentioned here.

Example:

tsc -b src test --clean will build src and test directories according to their respective tsconfig.json and then clean (remove) the generated .js files.

You could have a main tsconfig.json and simply use {extend: "path_to_main_tsconfig"} in each of the subfolder config files.

Suggestion:

Before running this command, try it with --dry to see what will happen.

lbragile
  • 7,549
  • 3
  • 27
  • 64