How to delete compiled JS files from previous typescript(.ts) files?
Asked Answered
S

11

71

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?

Seale answered 2/1, 2016 at 12:27 Comment(1)
github.com/gulpjs/gulp/blob/master/docs/recipes/…Pipistrelle
F
77

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?"

Fitch answered 16/3, 2017 at 15:20 Comment(4)
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.Antiproton
@Antiproton 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.Argentinaargentine
rimraf is for windows machine. "prebuild":"rimraf dist", "build": "tsc -p tsconfig.release.json",Sunstroke
Works also for server-side projects and projects that are not using gulp.Concessionaire
B
97

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.

Behan answered 4/8, 2019 at 5:37 Comment(7)
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.Wellnigh
i did it as daily task, not sure why it doesn't work in your environment, any details for your issue?Behan
cannot see that "--clean" is part of the compiler options. typescriptlang.org/docs/handbook/compiler-options.htmlOestriol
Seems it is hide option? did you try with it in your environment?Behan
@Wellnigh --clean flag is part of project-references and works only with it.Serpentine
--clean doesn't clean all outDir (deleted TS files remain compiled in outDir, but not cleaned...)Padrone
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.Lacasse
F
77

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?"

Fitch answered 16/3, 2017 at 15:20 Comment(4)
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.Antiproton
@Antiproton 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.Argentinaargentine
rimraf is for windows machine. "prebuild":"rimraf dist", "build": "tsc -p tsconfig.release.json",Sunstroke
Works also for server-side projects and projects that are not using gulp.Concessionaire
S
24

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.

Statfarad answered 31/1, 2020 at 3:49 Comment(3)
I've always wondered, why add another package rimraf when rm -rf is a faster native alternative present literally everywhere?Syndetic
@Syndetic because it doesn’t work when using WindowsStatfarad
Even if you have Cygwin, for some reason rimrar node_modules is faster than rm -rf .\node_modules according to my tests.Clump
H
9

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

Hyunhz answered 9/2, 2021 at 23:37 Comment(1)
Upvote for clear answer, but also for enlightening me about pre scripts - thanks!Berriman
N
8

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.

Niall answered 2/1, 2016 at 14:35 Comment(2)
This seems to be MUCH more complicated than https://mcmap.net/q/274324/-how-to-delete-compiled-js-files-from-previous-typescript-ts-files. Why do you recommend this anyway?Pro
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. :)Niall
S
2

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

Shulock answered 1/3, 2021 at 5:20 Comment(0)
L
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.

Lunneta answered 29/8, 2022 at 13:14 Comment(0)
S
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')
Serpentine answered 19/11, 2019 at 23:26 Comment(0)
M
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
Malvie answered 9/3, 2021 at 13:36 Comment(1)
DO NOT RUN THIS. This command deleted all my filesOrleanist
D
0

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

cd src
rm -rf */**/*.js
Deonnadeonne answered 9/11, 2022 at 9:24 Comment(0)
G
0

For that folks who accidentally run just tsc for new package (with e.g. copied files) and want to remove all the .js and .d.ts files recursively:

find . -type f \( -name "*.js" -o -name "*.d.ts" \) -print -prune -exec rm -rf {} \;

It should do the job.

Glasgow answered 1/12, 2023 at 17:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.