首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点中传输TypeScript的最快方法

节点中传输TypeScript的最快方法
EN

Stack Overflow用户
提问于 2018-05-09 19:40:18
回答 3查看 5.5K关注 0票数 8

什么是最好的(实时)?节点转换类型记录的方法?

我使用的是WebStorm和gulp,任务backend:watch在监听更改的后台运行。因此,当我在WebStorm中单击"save“时,它确实会将TS转到JS中,并存储在/build目录下。

我的方法很好,但是转换是时间消耗,每次运行需要2到3秒,秒变成分钟,等等。

有没有办法优化它,一个更好的选择?

  • https://www.npmjs.com/package/ts-node是另一种选择,但我不确定它是否比我目前拥有的更好。
  • 另外,听说新的基于电子的VisualStudio,但它保存在同一位置的JS文件,这在我看来是混乱的。

代码语言:javascript
复制
    //////////////////////////////////////////////
    // Backend tasks
    //////////////////////////////////////////////
    
    const appSourceDir = path.join(dir, '/app');
    const appSourceGlob = `${appSourceDir}/**/*.*`;
    const appSourceRelativeGlob = 'app/**/*.*';
    const appCodeGlob = `${appSourceDir}/**/*.ts`;
    const appCodeRelativeGlob = 'app/**/*.ts';
    const appFilesGlob = [appSourceGlob, `!${appCodeGlob}`];
    const appFilesRelativeGlob = [appSourceRelativeGlob, `!${appCodeRelativeGlob}`];
    const appBuildDir = path.join(dir, '/build');
    
    
    gulp.task('backend:symlink', [], function (done) {
      const appTargetDir = path.join(dir, '/node_modules/app');
      // symlink for app
      fs.exists(appTargetDir, function (err) {
        if (!err) {
          fs.symlinkSync(appBuildDir, appTargetDir, 'dir');
        }
      });
    
      done();
    });
    
    gulp.task('backend:clean', function (done) {
      clean([appBuildDir + '/**/*', '!.gitkeep'], done);
    });
    
    gulp.task('backend:compile', function (done) {
      tsCompile([appCodeGlob], appBuildDir, appSourceDir, done);
    });
    
    gulp.task('backend:files', function () {
      // copy fixtures and other non ts files
      // from app directory to build directory
      return gulp
        .src(appFilesGlob)
        .pipe(plugin.cached('files'))
        .pipe(gulp.dest(appBuildDir));
    });
    
    gulp.task('backend:build', function (done) {
      sequence(
        'backend:clean',
        'backend:compile',
        'backend:files',
        done
      );
    });
    
    gulp.task('backend:watch:code', function () {
      const watcher = gulp.watch([appCodeRelativeGlob], ['backend:compile']);
    
      watcher.on('change', function (event) {
        // if a file is deleted, forget about it
        if (event.type === 'deleted') {
          // gulp-cached remove api
          delete plugin.cached.caches.code[event.path];
          delete plugin.event.caches.lint[event.path];
          // delete in build
          del(getPathFromSourceToBuild(event.path, appSourceDir, appBuildDir));
        }
      });
    });
    
    gulp.task('backend:watch:files', function () {
      const watcher = gulp.watch([appFilesRelativeGlob], ['backend:files']);
    
      watcher.on('change', function (event) {
        // if a file is deleted, forget about it
        if (event.type === 'deleted') {
          // gulp-cached remove api
          delete plugin.cached.caches.files[event.path];
          delete plugin.event.caches.lint[event.path];
          // delete in build
          del(getPathFromSourceToBuild(event.path, appSourceDir, appBuildDir));
        }
      });
    });
    
    gulp.task('backend:watch', ['backend:build'], function (done) {
      // first time build all by backend:build,
      // then compile/copy by changing
      gulp
        .start([
          'backend:watch:code',
          'backend:watch:files'
        ], done);
    });
    
    //////////////////////////////////////////////
    // Helpers
    //////////////////////////////////////////////
    
    /**
     * remaps file path from source directory to destination directory
     * @param {string} file path
     * @param {string} source directory path
     * @param {string} destination directory path
     * @returns {string} new file path (remapped)
     */
    function getPathFromSourceToBuild(file, source, destination) {
      // Simulating the {base: 'src'} used with gulp.src in the scripts task
      const filePathFromSrc = path.relative(path.resolve(source), file);
      // Concatenating the 'destination' absolute
      // path used by gulp.dest in the scripts task
      return path.resolve(destination, filePathFromSrc);
    }
    
    /**
     * @param  {Array}    path - array of paths to compile
     * @param  {string}   dest - destination path for compiled js
     * @param  {string}   baseDir - base directory for files compiling
     * @param  {Function} done - callback when complete
     */
    function tsCompile(path, dest, baseDir, done) {
      const ts = plugin.typescript;
      const tsProject = ts.createProject('tsconfig.json');
    
      gulp
        .src(path, {base: baseDir})
        // used for incremental builds
        .pipe(plugin.cached('code'))
        .pipe(plugin.sourcemaps.init())
        .pipe(tsProject(ts.reporter.defaultReporter())).js
        .pipe(plugin.sourcemaps.write('.'))
        .pipe(gulp.dest(dest))
        .on('error', done)
        .on('end', done);
    }
    
    /**
     * Delete all files in a given path
     * @param  {Array}   path - array of paths to delete
     * @param  {Function} done - callback when complete
     */
    function clean(path, done) {
      log('Cleaning: ' + plugin.util.colors.blue(path));
      del(path).then(function (paths) {
        done();
      });
    }
    
    /**
     * Log a message or series of messages using chalk's blue color.
     * Can pass in a string, object or array.
     */
    function log(msg) {
      if (typeof (msg) === 'object') {
        for (let item in msg) {
          if (msg.hasOwnProperty(item)) {
            plugin.util.log(plugin.util.colors.blue(msg[item]));
          }
        }
      } else {
        plugin.util.log(plugin.util.colors.blue(msg));
      }
    }

也在GitHub上发布,请参阅https://github.com/ivanproskuryakov/loopplate-node.js-boilerplate

EN

回答 3

Stack Overflow用户

发布于 2018-05-12 18:35:17

你试过的只是tsc --watch,没有吞咽,中间也没有npm?这就是我发现的最快的监视和编译我的项目的方法。它需要1-2秒的第一次--但之后几乎是瞬间。如果你的目标是尽可能快--即使npm也需要半秒时间--我想你会更快地抛弃所有你能做到的技术。

另外,如果您正在处理多个类型记录项目,请确保您使用的是新的typescript功能组合项目、https://www.typescriptlang.org/docs/handbook/project-references.html --在我的例子中,我使用的是单回购和需要编译的几个项目,这个特性大大提高了速度,简化了编译工作流程。

请记住,TypeScript不仅仅是一个编译器,它还是一个语言服务--这就是为什么-watch会比tsc做得更好的原因--它将执行部分编译

票数 14
EN

Stack Overflow用户

发布于 2018-11-23 22:29:19

更新我的问题-我已经得到了快速的结果,用更少的行简单地切换到‘as节点如下。

代码语言:javascript
复制
{
  ...
  "scripts": {
    "start": "ts-node server.ts",
    "dev": "ts-node-dev --respawn --transpileOnly server.ts",
    "test": "./node_modules/.bin/mocha --compilers ts:ts-node/register ./test/**/**/**/*.ts",
  },
  ...
}

package.json内容,更多https://github.com/ivanproskuryakov/express-typescript-boilerplate

票数 1
EN

Stack Overflow用户

发布于 2020-11-09 17:59:13

  1. 安装Nodemon:npm i -g nodemon
  2. 创建文件nodemon.json {“观察”:"src","ext":".ts,.js",“忽略”:[],"exec":"ts-node ./src/server.ts“}
  3. package.json中添加命令 “开始:开发”:“无恶魔”,
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50260901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档