简体   繁体   English

如何为打字机实时重载快递配置Gulp

[英]How to configure Gulp for, typescript live-reload express

I am trying to create the following workflow: 我正在尝试创建以下工作流程:

  1. Watch File Changes 观看文件更改
  2. Compile TypeScript, after that is complete: 完成后,编译TypeScript:
  3. Restart Express App, after that is complete: 完成后,重新启动Express App:
  4. LiveReload Browser LiveReload浏览器

Here is my current approach : 这是我目前的做法:

gulp.task('default', function() {
   //1. run your script as a server
   var server = gls.new('./bin/www');
   server.start();

   gulp.watch('./**/*.js', function(file) {
      var promise = server.start.bind(server)(); //restart my server
          promise.then(function(result) {
             console.log("restarted!");
             setTimeout( function() { 
                server.notify.apply(server, [file]);
             }, 500);

          });
    });
});

This works up to console.log("restarted!"); 这适用于console.log(“ restarted!”);

I added the setTimeout since the reload would happen before teh server has been started. 我添加了setTimeout,因为重新加载将在服务器启动之前进行。 But with it nothing happens no reload at all. 但是,没有任何反应,根本没有重新加载。


EDIT 编辑

Here my first working solution: 这是我的第一个可行的解决方案:

var gulp = require('gulp'),
    nodemon = require('gulp-nodemon'),
    livereload = require('gulp-livereload'),
    ts = require('gulp-typescript');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('typescript', function() {
   console.log('Compiling TypeScript');

    var tsResult = tsProject.src({base: './'}) 
        .pipe(ts(tsProject));
    return tsResult.js.pipe(gulp.dest('./'));
});

gulp.task('serve', ['typescript'], function () {

    gulp.watch('./**/*.ts', ['typescript']);

    livereload.listen();

    nodemon({
        script: './bin/www',
        ext: 'js',
    }).on('restart', function () {
        setTimeout(function () {
            console.log("reload!");
            livereload.reload();
        }, 500);
    });

});

But I still would love to get rid of that ugly setTimeout. 但是我仍然很想摆脱丑陋的setTimeout。

Bonus Question Is it possible to solve the same without Gulp with just a Shell Script ? 额外的问题如果没有Gulp,仅使用Shell脚本就能解决相同的问题吗? Compared to the command line TSC command the gulp version takes ages to compile. 与命令行TSC命令相比,gulp版本需要花费很多时间才能编译。

Compile typescript and tsx (react jsx), browserify, minified the bundle, and reload the browser on frontend changes. 编译打字稿和tsx(反应jsx),浏览器缩小,缩小包,并在前端更改时重新加载浏览器。

Compile typescript to js, restart the server and reload browser when Backend changes. 后端更改时,将打字稿编译为js,重新启动服务器并重新加载浏览器。

Gulp 4 Typescript+React+JSX+Browserify+ Seperate Frontend Watch+ Nodemon + Livereload Gulp 4 Typescript + React + JSX + Browserify +单独的前端Watch + Nodemon + Livereload

That's all you will need. 这就是您所需要的。

The gulpfile.js . gulpfile.js

Cheers, kataras. 欢呼声,片语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM