How to watch and compile all TypeScript sources?

后端 未结 9 1244
挽巷
挽巷 2020-12-04 06:53

I\'m trying to convert a pet project to TypeScript and don\'t seem to be able to use the tsc utility to watch and compile my files. The help says I should use t

相关标签:
9条回答
  • 2020-12-04 07:31

    The tsc compiler will only watch those files that you pass on the command line. It will not watch files that are included using a /// <sourcefile> reference. If your working with the bash, you could use find to recursively find all *.ts files and compile them:

    find . -name "*.ts" | xargs tsc -w
    
    0 讨论(0)
  • 2020-12-04 07:32

    Technically speaking you have a few options here:

    If you are using an IDE like Sublime Text and integrated MSN plugin for Typescript: http://blogs.msdn.com/b/interoperability/archive/2012/10/01/sublime-text-vi-emacs-typescript-enabled.aspx you can create a build system which compile the .ts source to .js automatically. Here is the explanation how you can do it: How to configure a Sublime Build System for TypeScript.

    You can define even to compile the source code to destination .js file on file save. There is a sublime package hosted on github: https://github.com/alexnj/SublimeOnSaveBuild which make this happen, only you need to include the ts extension in the SublimeOnSaveBuild.sublime-settings file.

    Another possibility would be to compile each file in the command line. You can compile even multiple files at once by separating them with spaces like so: tsc foo.ts bar.ts. Check this thread: How can I pass multiple source files to the TypeScript compiler?, but i think the first option is more handy.

    0 讨论(0)
  • 2020-12-04 07:32

    EDIT: Note, this is if you have multiple tsconfig.json files in your typescript source. For my project we have each tsconfig.json file compile to a differently-named .js file. This makes watching every typescript file really easy.

    I wrote a sweet bash script that finds all of your tsconfig.json files and runs them in the background, and then if you CTRL+C the terminal it will close all the running typescript watch commands.

    This is tested on MacOS, but should work anywhere that BASH 3.2.57 is supported. Future versions may have changed some things, so be careful!

    #!/bin/bash
    # run "chmod +x typescript-search-and-compile.sh" in the directory of this file to ENABLE execution of this script
    # then in terminal run "path/to/this/file/typescript-search-and-compile.sh" to execute this script
    # (or "./typescript-search-and-compile.sh" if your terminal is in the folder the script is in)
    
    # !!! CHANGE ME !!!    
    # location of your scripts root folder
    # make sure that you do not add a trailing "/" at the end!!
    # also, no spaces! If you have a space in the filepath, then
    # you have to follow this link: https://stackoverflow.com/a/16703720/9800782
    sr=~/path/to/scripts/root/folder
    # !!! CHANGE ME !!!
    
    # find all typescript config files
    scripts=$(find $sr -name "tsconfig.json")
    
    for s in $scripts
    do
        # strip off the word "tsconfig.json"
        cd ${s%/*} # */ # this function gets incorrectly parsed by style linters on web
        # run the typescript watch in the background
        tsc -w &
        # get the pid of the last executed background function
        pids+=$!
        # save it to an array
        pids+=" "
    done
    
    # end all processes we spawned when you close this process
    wait $pids
    

    Helpful resources:

    • bash: interpret string variable as file name/path
    • A variable modified inside a while loop is not remembered
    • https://www.cyberciti.biz/faq/search-for-files-in-bash/
    • https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
    • https://linuxize.com/post/bash-concatenate-strings/
    • https://www.cyberciti.biz/faq/bash-for-loop/
    • https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
    • https://unix.stackexchange.com/questions/144298/delete-the-last-character-of-a-string-using-string-manipulation-in-shell-script
    • What are the special dollar sign shell variables?
    0 讨论(0)
  • 2020-12-04 07:35

    Create a file named tsconfig.json in your project root and include following lines in it:

    {
        "compilerOptions": {
            "emitDecoratorMetadata": true,
            "module": "commonjs",
            "target": "ES5",
            "outDir": "ts-built",
            "rootDir": "src"
        }
    }
    

    Please note that outDir should be the path of the directory to receive compiled JS files, and rootDir should be the path of the directory containing your source (.ts) files.

    Open a terminal and run tsc -w, it'll compile any .ts file in src directory into .js and store them in ts-built directory.

    0 讨论(0)
  • 2020-12-04 07:42

    tsc 0.9.1.1 does not seem to have a watch feature.

    You could use a PowerShell script like the one:

    #watch a directory, for changes to TypeScript files.  
    #  
    #when a file changes, then re-compile it.  
    $watcher = New-Object System.IO.FileSystemWatcher  
    $watcher.Path = "V:\src\MyProject"  
    $watcher.IncludeSubdirectories = $true  
    $watcher.EnableRaisingEvents = $true  
    $changed = Register-ObjectEvent $watcher "Changed" -Action {  
      if ($($eventArgs.FullPath).EndsWith(".ts"))  
      {  
        $command = '"c:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.exe" "$($eventArgs.FullPath)"'  
        write-host '>>> Recompiling file ' $($eventArgs.FullPath)  
        iex "& $command"  
      }  
    }  
    write-host 'changed.Id:' $changed.Id  
    #to stop the watcher, then close the PowerShell window, OR run this command:  
    # Unregister-Event < change Id >  
    

    Ref: Automatically watch and compile TypeScript files.

    0 讨论(0)
  • 2020-12-04 07:50

    TypeScript 1.5 beta has introduced support for a configuration file called tsconfig.json. In that file you can configure the compiler, define code formatting rules and more importantly for you, provide it with information about the TS files in your project.

    Once correctly configured, you can simply run the tsc command and have it compile all the TypeScript code in your project.

    If you want to have it watch the files for changes then you can simply add --watch to the tsc command.

    Here's an example tsconfig.json file

    {
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false
    },
    "include": [
        "**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]}
    

    In the example above, I include all .ts files in my project (recursively). Note that you can also exclude files using an "exclude" property with an array.

    For more information, refer to the documentation: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

    0 讨论(0)
提交回复
热议问题