【问题标题】:How do you load typescript modules from node_modules using browserify?如何使用 browserify 从 node_modules 加载 typescript 模块?
【发布时间】:2016-02-29 18:20:08
【问题描述】:

当我运行tsc 时,一切运行良好。但是,我无法理解您打算如何从节点模块导入其他打字稿模块。

这是我的 gulp 文件的重要部分:

gulp.task('compile-ts', ['clean'], function(){
  var sourceTsFiles = [
    config.allTs,
    config.typings
  ];

  var bundler = browserify({
    basedir : "src",
    debug : true})
    .add("app.ts")
  //.add("typings/tsd.d.ts")
  .plugin(tsify);

  return bundler.bundle()
        .pipe(source("bundle.js"))
        .pipe(gulp.dest("build"))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(sourcemaps.write({includeContent: false, sourceRoot: 'src'}));

});

当我使用时,

import {DataRepository, List} from "tsmvc";

tsmvc 是一个打字稿模块节点模块,我得到cannot find module tsmvc. Atom 不会抱怨并显示智能感知,tsc 不会抱怨,但 tsify 会。 谁能指出我做类似事情的 gulp 文件或解释这个过程?

这里是 github 仓库:https://github.com/Davste93/typescript-mvc-consumer/blob/master/gulpfile.js

【问题讨论】:

标签: javascript node.js typescript gulp tsify


【解决方案1】:

tsify 的 0.15.3 版之前,无法从 node_modules 中导入 TypeScript 文件。

在内部,tsify 插件会创建一个转换,Browserify 不会为非全局转换转换node_modules 下的文件。在 tsify 的 0.15.3 版本中,添加了 global 选项,可以按如下方式指定:

var bundler = browserify({
    basedir: "src",
    debug: true
})
.add("app.ts")
.plugin(tsify, { global: true });

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 2016-05-02
    • 1970-01-01
    • 2020-06-13
    • 2012-10-10
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多