Browserify + TypeScript

Ale*_*rov 5 browserify typescript gulp

今天是个好日子.

我尝试使用Gulp构建我的Web项目.我想使用TypeScript,我想用browserify统治依赖.

但我遇到了一些麻烦.

我使用下一代码进行构建:

var path = {
    build: {
        js: 'build/js/',
    },

    src: {
        ts: './src/ts/*.ts',
    },

};

gulp.task("ts:build", function(){
    glob(path.src.ts, {}, function(err, files){
        var b = browserify({
            cache: {},
            packageCache: {},
            debug: true
        });

        _.forEach(files, function(file){
            b.add(file);
        });

        b.plugin('tsify', { noImplicitAny: true })
            .bundle()
            .pipe(source('app.js'))
            .pipe(gulp.dest(path.build.js))
        });    
    });
});
Run Code Online (Sandbox Code Playgroud)

我无法理解我必须如何声明依赖项.例如,我有两个*.ts文件:main.ts和someclass.ts.在someclass.ts中我写了一些类:

class SomeClass {
    // bla-bla-bla
}

export = SomeClass;
Run Code Online (Sandbox Code Playgroud)

我想在main.ts中使用这个类.我用browserify试试这个:

var settingsPanel = require("./someclass");
Run Code Online (Sandbox Code Playgroud)

gulp build工作正常,但在浏览器控制台中我看到了

node_modules\browserify \node_modules\browser-pack_prelude.js:1 Uncaught错误:找不到模块'./someclass';

对于这方面的任何建议,我都会非常有帮助.特别是对于gulp + browserify + typescript的一些代码示例的链接.

Rem*_*sen 5

我现在正在制作一个非常类似的版本.

在我的构建中,我有一个任务用于使用commonjs模块从TS编译到JS .在你的gulpfile.js,当你将TS编译成JS时,你需要指示编译器使用commonjs模块:

var tsProject = ts.createProject({
    removeComments : true,
    noImplicitAny : true,
    target : 'ES3',
    module : 'commonjs', // !IMPORTANT
    declarationFiles : true
});
Run Code Online (Sandbox Code Playgroud)

我有第二个任务,它指示应用程序JS入口点(main.js)到Browserify,然后它将生成捆绑文件,uglify它并生成源映射.

我的main.ts文件包含以下内容:

///<reference path="./references.d.ts" />

import headerView = require('./header_view');
import footerView = require('./footer_view');
import loadingView = require('./loading_view');

headerView.render({});
footerView.render({});
loadingView.render({});
Run Code Online (Sandbox Code Playgroud)

我的gulpfile.js看起来像这样(请注意我这里只复制了相关部分)

// ....

var paths = {
  ts : './source/ts/**/**.ts',
  jsDest : './temp/js/',
  dtsDest : './temp/definitions/',
  scss : './source/scss/**/**.scss',
  scssDest : './temp/css/',
  jsEntryPoint : './temp/js/main.js',
  bowerComponents : './bower_components',
  nodeModules : 'node_modules',
  temp : './temp',
  dist : './dist/'
};

var tsProject = ts.createProject({
    removeComments : true,
    noImplicitAny : true,
    target : 'ES3',
    module : 'commonjs',
    declarationFiles : true
});

// Build typescript

gulp.task('tsc', function(){
  var tsResult = gulp.src(paths.ts).pipe(ts(tsProject));
  tsResult.dts.pipe(gulp.dest(paths.dtsDest));
  tsResult.js.pipe(gulp.dest(paths.jsDest));
});

// Browserify, uglify and sourcemaps

gulp.task('minify-js', function () {
  // transform regular node stream to gulp (buffered vinyl) stream
  var browserified = transform(function(filename) {
    var b = browserify({ entries: filename, debug: true });
    return b.bundle();
  });

  return gulp.src(paths.jsEntryPoint)
    .pipe(browserified)
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(paths.dist));
});

// ....
Run Code Online (Sandbox Code Playgroud)

完整的源代码(正在进行的工作)可以在https://github.com/remojansen/modern-workflow-demo上找到

  • 浏览器似乎从磁盘中获取(在您的示例中为jsEntryPoint).这不像Grunt吗?我以为Gulp都是关于溪流的. (2认同)