简体   繁体   中英

How do you load typescript modules from node_modules using browserify?

When I run tsc , everything runs perfectly fine. However, I cannot understand how you are meant to import other typescript modules from node modules.

This is the important part of my gulp file:

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'}));

});

When I use,

import {DataRepository, List} from "tsmvc";

Where tsmvc is a typescript module node module, I get cannot find module tsmvc. Atom doesn't complain and shows me intellisense, tsc doesn't complain, but tsify does. Can anyone point me to a gulp file doing something similar or explain the process?

Here's the github repo: https://github.com/Davste93/typescript-mvc-consumer/blob/master/gulpfile.js

Prior to version 0.15.3 of tsify , it was not possible to import TypeScript files from within node_modules .

Internally, the tsify plugin creates a transform and Browserify does not transform files under node_modules for non-global transforms. In version 0.15.3 of tsify , the global option was added and can be specified as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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