如何从以前的打字稿(.ts)文件中删除已编译的JS文件?

ksh*_*fbd 22 javascript typescript gulp angular

我正在关注Angular 2快速入门教程.以下是我的文件夹结构 -

??? gulpfile.js
??? index.html
??? js
|   ??? app.component.js
|   ??? boot.js
??? source
|   ??? app.component.ts
|   ??? boot.ts
??? node_modules
    ??? module 1
    ??? module 2
Run Code Online (Sandbox Code Playgroud)

我的打字稿文件在source/目录中.我正在编译它到js/目录.我正在使用gulp-typescript.

问题是,当我举个例子,重命名文件boot.tsbootstrap.ts和重新编译,相应的bootstrap.js文件被创建,但旧的boot.js文件仍然保留在js /目录.

现在文件夹结构如下所示 -

??? gulpfile.js
??? index.html
??? js
|   ??? app.component.js
|   ??? bootstrap.js
|   ??? boot.js
??? source
|   ??? app.component.ts
|   ??? bootstrap.ts
??? node_modules
    ??? module 1
    ??? module 2
Run Code Online (Sandbox Code Playgroud)

我想boot.js通过gulp任务自动删除这个.怎么做到这一点?

Aka*_*ose 27

我来到这里看到了标题,并在混合物中添加一大堆不是解决方案.所以这就是我如何解决它.

用你的npm构建脚本作为前缀 rm -rf js &&

"scripts": {
    ...
    "build": "rm -rf js/ && tsc",
    ...
},
Run Code Online (Sandbox Code Playgroud)

希望有人会觉得这很有用.

按照评论中的建议进行编辑

作为一种更通用的解决方案,请使用rm -rf js &&而不是rm -rf js &&. rm -rf js &&是一个npm模块,旨在跨平台(包括Windows)工作.要使其工作,首先将其安装为dev依赖项.

  • rimraf用于Windows机器。```“ prebuild”:“ rimraf dist”,“ build”:“ tsc -p tsconfig.release.json”,``` (4认同)
  • @samthecodingman 作为记录,我选择了 Windows 开发人员 :) 我尝试过 Mac 和 Linux,我对它们中的任何一个都感到不自在。我只是不喜欢它们,即使是 MacOS。Windows 中的一切都很顺利(至少对我而言),不管大多数开发人员似乎讨厌它。 (3认同)
  • @Choco`rm`是用于删除文件的unix命令`rm -rf js /`递归地删除`js /`中的所有文件和目录。“ &&”部分附加了另一个命令,以在“ rm”完成后执行。 (2认同)
  • 如果你不幸使用windows dev环境,如果添加`cash-rm`作为dev依赖项,你也可以使用`rm`.类似地,如果保存为dev依赖项,则可以使用`rimraf`(恰当地命名为`rm -rf`). (2认同)
  • 也适用于服务器端项目和不使用 gulp 的项目。 (2认同)

Gab*_*lBB 9

这个解决方案的灵感来自@Akash Kurian Jose's answer。由于他的解决方案取决于您使用的操作系统,我添加了这个适用于任何操作系统的解决方案:

rimraf添加为开发依赖项:

"devDependencies": {
   "rimraf": "^3.0.1"
}
Run Code Online (Sandbox Code Playgroud)

然后将这 3 个脚本添加到您的 package.json 中:

"rimraf": "./node_modules/rimraf/bin.js",
"clean" : "rimraf js/",
"compile": "npm run clean && tsc -p ./"
Run Code Online (Sandbox Code Playgroud)

当执行npm run compilejs/文件夹将在编译之前进行清洗。

  • @seeker_of_bacon 因为它在使用 Windows 时不起作用 (8认同)

tos*_*skv 8

安装gulp del.

$ npm install --save-dev gulp del
Run Code Online (Sandbox Code Playgroud)

创建任务.

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

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

gulp.task('default', ['clean:output']);
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到更多关于gulp del的信息.

  • 因为当时我不太了解。我同意另一种解决方案更简单、更好。由 OP 来更改该问题已接受的答案。我希望他能做到。:) (2认同)

Lle*_*ins 8

该解决方案是GabrielBB解决方案的改进。

安装 rimraf npm i rimraf -D

添加以下 npm 脚本

"clean": "rimraf js/",
"prebuild": "npm run clean",
"build": "tsc",
Run Code Online (Sandbox Code Playgroud)

这利用了 npms 自动Pre 脚本

  • 支持明确的答案,同时也启发我关于预脚本的知识 - 谢谢! (2认同)

BMW*_*BMW 7

使用最新的tsc,您可以使用以下命令进行清理

tsc --build --clean
Run Code Online (Sandbox Code Playgroud)

我的TSC版本供您参考

$ tsc --version
Version 3.5.3
Run Code Online (Sandbox Code Playgroud)

请注意,-clean标志是项目引用的一部分,并且仅与它一起使用。

  • 不,不起作用。至少不适合我。`tsc --version: Version 3.5.3` 和 `tsc --build --clean` 不执行所要求的操作。 (10认同)
  • 看不到“--clean”是编译器选项的一部分。https://www.typescriptlang.org/docs/handbook/compiler-options.html (7认同)
  • --clean 不会清除所有 outDir (已删除的 TS 文件仍保留在 outDir 中编译,但未清除...) (6认同)
  • @Elias --clean 标志是项目引用的一部分,并且仅适用于它。 (4认同)
  • 我将其作为日常任务完成,不确定为什么它在您的环境中不起作用,您的问题有任何详细信息吗? (2认同)