How to delete compiled JS files from previous typescript(.ts) files?
Solution 1
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?"
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 5
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
![ksharifbd](https://i.stack.imgur.com/eJAXa.jpg?s=256&g=1)
ksharifbd
Hello! I've 5 years of experience working with diverse web technologies that include both the frontend and the backend to successfully generate values in fast-paced and highly collaborative agile product teams. The technologies include but not limited to HTML5, CSS3, JavaScript, React, Redux, Jest, Enzyme, Typescript, NodeJS, ExpressJS, RabbitMQ, Redis, Elasticsearch, Docker, and Kubernetes. Currently, I'm making significant contributions to North America's largest field service solutions provider. I'm interested in taking on a role that will enable me to have end-to-end ownership of the product feature and to move across the tech stack in a highly engaging, collaborating, and constantly improving team. If you are looking for a highly motivated, self-starter, collaborative, and skillful person who can greatly contribute to the product growth, then shoot an email at [email protected].
Updated on May 18, 2021Comments
-
ksharifbd about 3 years
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 tojs/
directory. I'm using gulp-typescript.The problem is when I, for example, rename the file
boot.ts
tobootstrap.ts
and compile again, correspondingbootstrap.js
file is created but the oldboot.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? -
samthecodingman almost 7 yearsIf you are unlucky enough to be using a windows dev environment, you can also use
rm
if you addcash-rm
as a dev dependency. Similarly, you can userimraf
(aptly named forrm -rf
) instead if saved as a dev dependency. -
Jeremy Thille over 6 years@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.
-
Rupesh Kumar Tiwari over 6 years
rimraf
is for windows machine."prebuild":"rimraf dist", "build": "tsc -p tsconfig.release.json",
-
Elias almost 5 yearsNope, does not work. At least not for me.
tsc --version: Version 3.5.3
andtsc --build --clean
does not do what is asked. -
BMW over 4 yearsi did it as daily task, not sure why it doesn't work in your environment, any details for your issue?
-
Felix K. over 4 yearscannot see that "--clean" is part of the compiler options. typescriptlang.org/docs/handbook/compiler-options.html
-
BMW over 4 yearsSeems it is hide option? did you try with it in your environment?
-
ZiiMakc over 4 years@Elias --clean flag is part of project-references and works only with it.
-
Lazarus Rising over 4 yearsWorks also for server-side projects and projects that are not using gulp.
-
seeker_of_bacon over 4 yearsI've always wondered, why add another package
rimraf
whenrm -rf
is a faster native alternative present literally everywhere? -
GabrielBB over 4 years@seeker_of_bacon because it doesn’t work when using Windows
-
Aure77 about 4 years--clean doesn't clean all outDir (deleted TS files remain compiled in outDir, but not cleaned...)
-
zippycoder over 3 yearsUpvote for clear answer, but also for enlightening me about pre scripts - thanks!
-
Xanlantos about 3 yearsThis seems to be MUCH more complicated than stackoverflow.com/a/42838151/4693430. Why do you recommend this anyway?
-
toskv about 3 yearsbecause 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. :)
-
Jayem over 2 yearsDO NOT RUN THIS. This command deleted all my files
-
Vsevolod Golovanov about 2 yearsEven if you have Cygwin, for some reason
rimrar node_modules
is faster thanrm -rf .\node_modules
according to my tests.