Javascript 如何从以前的 typescript(.ts) 文件中删除已编译的 JS 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34565872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete compiled JS files from previous typescript(.ts) files?
提问by ksharifbd
I am following Angular 2 quick starttutorial. Following is my folder structure -
我正在关注Angular 2 快速入门教程。以下是我的文件夹结构 -
├── 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.
我的打字稿文件在source/目录中。我正在编译它到js/目录。我正在使用 gulp-typescript。
The problem is when I, for example, rename the file boot.tsto bootstrap.tsand compile again, corresponding bootstrap.jsfile is created but the old boot.jsfile still remains in the js/ directory.
问题是,例如,当我将文件重命名boot.ts为bootstrap.ts并再次编译时,bootstrap.js会创建相应的文件,但旧boot.js文件仍保留在 js/ 目录中。
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.jsautonomically via gulp task. How to achieve this?
我想boot.js通过 gulp 任务自动删除它。如何实现这一目标?
采纳答案by toskv
Install gulp del.
安装 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.
您可以在此处找到有关 gulp del 的更多信息。
回答by Akash Kurian Jose
I came here seeing the title, and adding gulp into the mix was not a solution. So here is how I solved it.
我是看到标题来到这里的,在混合中加入 gulp 并不是一个解决方案。所以这就是我解决它的方法。
Prefix your npm build script with rm -rf js &&
为您的 npm 构建脚本添加前缀 rm -rf js &&
"scripts": {
...
"build": "rm -rf js/ && tsc",
...
},
Hope someone will find this useful.
希望有人会发现这很有用。
回答by BMW
with the latest tsc, you should be fine to do the clean with below command
使用最新的 tsc,您应该可以使用以下命令进行清理
tsc --build --clean
My tsc version for your reference
我的 tsc 版本供您参考
$ tsc --version
Version 3.5.3
Note that --clean flag is part of project-referencesand works only with it.
请注意, --clean 标志是项目引用的一部分,仅适用于它。
回答by GabrielBB
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:
这个解决方案的灵感来自@Akash Kurian Jose's answer。由于他的解决方案取决于您使用的操作系统,我添加了这个适用于任何操作系统的解决方案:
Add rimrafas a dev dependency:
将rimraf添加为开发依赖项:
"devDependencies": {
"rimraf": "^3.0.1"
}
Then add these 3 scripts to your package.json:
然后将这 3 个脚本添加到您的 package.json 中:
"rimraf": "./node_modules/rimraf/bin.js",
"clean" : "rimraf js/",
"compile": "npm run clean && tsc -p ./"
When executing npm run compilethe js/folder will be cleaned before compiling.
当执行npm run compile该js/文件夹将在编译之前进行清洗。
回答by RTW
I use script from here.
我使用这里的脚本。
In package.json add script:
在 package.json 添加脚本:
"build": "node jsDelete.js && npx tsc -w"
And add jsDelete.js:
并添加 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')

