大家好。
我试着用Gulp构建我的网络项目。我想要使用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))
});
});
});我不明白如何声明依赖项。例如,我有两个*.ts文件: main.ts和someclass.ts。在someclass.ts中,我编写了一些类:
class SomeClass {
// bla-bla-bla
}
export = SomeClass;我想在main.ts中使用这个类。我试着用browserify:
var settingsPanel = require("./someclass");gulp已正常工作,但在浏览器控制台中我看到了
node_modules\browserify\node_modules\browser-pack_prelude.js:1Uncaught错误:找不到模块'./someclass';
任何关于这方面的建议,我都会很有帮助的。特别是关于gulp+browserify+typescript代码示例中的链接。
发布于 2015-03-16 03:03:57
我现在正在做一个非常相似的构建。
在我的构建中,我有一个使用commonjs模块从TS编译到JS的任务。在您的gulpfile.js中,当您将TS编译成JS时,需要向编译器指示使用commonjs模块:
var tsProject = ts.createProject({
removeComments : true,
noImplicitAny : true,
target : 'ES3',
module : 'commonjs', // !IMPORTANT
declarationFiles : true
});我还有第二个任务,它指示应用程序JS入口点(main.js)到Browserify,然后它将生成包文件、使其丑陋并生成源映射。
我的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({});我的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));
});
// ....完整的源代码(工作正在进行中)可在https://github.com/remojansen/modern-workflow-demo上获得。
https://stackoverflow.com/questions/28702247
复制相似问题