12

I Have the following files:

  • ActiveTask.ts
  • ActiveTask.js
  • Controller.ts
  • Controller.js
  • _config.js
  • otherFile.js

And I want to commit only:

  • ActiveTask.ts
  • Controller.ts
  • _config.js
  • otherFile.js

How to ignore .js files that are the same name as the .ts files?

3 Answers 3

13
  1. Change the outDir entry of your tsconfig.json file. This is conventionally called build but you can name it whatever you'd like.
// tsconfig.json (mine is in the root directory of my project)
{
    "compilerOptions": {  
      ...
      "outDir": "myAwesomeSuperCoolBuildDirectoryThatIPromiseToChangeAndNotJustCopyBlindlyFromStackOverflowBecauseThatWouldBeBad",  
      "sourceMap": true,  
      ...
    }
}
  1. Add outDir followed by a forward slash --> / to your .gitignore file
// .gitignore (mine is in the root directory of my project)
myAwesomeSuperCoolBuildDirectoryThatIPromiseToChangeAndNotJustCopyBlindlyFromStackOverflowBecauseThatWouldBeBad/
  1. Delete old js.map and js files generated by TypeScript and build them again. This time, they will be built in the specified directory and will be ignored by git.
1

For your particular case, I suggest:

In .gitignore

*.js
!_*.js

This will ignore all js files except what starting with underscore.

6
  • Sorry for misunderstanding, but the other .js files I want to commit are not always starting with underscore.
    – Humberd
    Commented Sep 24, 2016 at 18:50
  • 1
    Then, I suggest that put all js files generated from ts files and ts files into one directory. And ignore that directory.
    – Jeon
    Commented Sep 24, 2016 at 18:51
  • Second suggestion is, get filenames with ls *.ts or dir *.ts then, remove extension (.ts). And append .js and write into .gitignore. This should be executed whenever there are new or renamed files.
    – Jeon
    Commented Sep 24, 2016 at 18:53
  • Thanks, I did your first suggestion.
    – Humberd
    Commented Sep 24, 2016 at 19:15
  • 1
    Yeah, try dir_to_be_ignored/*.js
    – Jeon
    Commented Sep 24, 2016 at 19:17
0

I found this working for me:

/FrontendApp/src/*/**/*.js
/FrontendApp/src/*/**/*.map

This excluded all js and map files from the /FrontendApp/src and its subfolders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.