Search code examples
typescriptgrunt-ts

grunt-ts watch not working when file changed


I am using grunt:

  "dev-build": {
                src: ["src/**/*.ts", "typings/vendors.d.ts","typings/tsd.d.ts", "!libs/**/*.ts"],
                    outDir: "artifacts/dev",
                    watch: "src/**/*",
                    options: {
                        // comments: true,
                  //      baseDir: 'src/',
                        module: "amd",
                        target: "es5",
                        sourceMap: true,
                        declaration: true, //inlineSourceMap :true,
                        //emitDecoratorMetadata:true,
                        //experimentalDecorators:true,

                    }               

            }

and everything compiles fine:

"TypeScript compilation complete: 8.97s for 256 TypeScript files."

but then after changing a file (adding a linebreak) without changing the code the compile breaks with the watch:

### changed  >>src/FxsPortal/FxsBaseItemViewModel.ts
Compiling...
### Fast Compile >>src/FxsPortal/FxsBaseItemViewModel.ts
Using tsc v1.6.2
C:/dev/AscendXYZ Portal/src/FxsPortal/FxsBaseItemViewModel.ts(2,21): error TS2307: Cannot find module 'knockout'.

I know there are no errors since I can compile it all manually. I dont seem to understand why it do not work when the watch of ts-grunt triggers.


Solution

  • For reference I just wanted to share how I solved this using an external watch.

     watch: {
                devBuildWatch: {
                    files: ['src/**/*'],
                    tasks: ['ts:devBuild'],
                    options: {
                        spawn: false,
                    },
                }
            },
    

    and a onchange action

     var changedFiles = {};
    
        var onChange = grunt.util._.debounce(function () {
            grunt.config('ts.devBuild.src', Object.keys(changedFiles).concat([ "typings/vendors.d.ts","typings/tsd.d.ts", "!libs/**/*.ts"]));
            changedFiles = Object.create(null);
        }, 50);
    
        grunt.event.on('watch', function (action, filepath) {
            changedFiles[filepath] = action;
            onChange();
        });