[英]How to use a stream as input for Browserify?
In Gulp, I'm trying to compile TypeScript, concatenate it, then run it through Browserify to handle the require
s (then uglify after if in production mode). 在Gulp中,我正在尝试编译TypeScript,将其连接起来,然后通过Browserify运行它来处理
require
s(然后在生产模式下进行uglify)。
This sample code is the closest I've found to what I'm trying to do, however it uses an intermediary file. 这个示例代码是我发现的最接近我正在尝试做的事情,但是它使用了一个中间文件。 I'd much rather keep things to the stream to avoid the overhead of the intermediary file if at all possible.
我宁愿把事情保留在流中,以避免中间文件的开销,如果可能的话。
Since Browserify outputs a stream, it seems like it should know how to accept one as well. 由于Browserify输出一个流,它似乎应该知道如何接受一个流。
var gulp = require('gulp');
var browserify = requ
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var transform = require('vinyl-transform');
var typeScript = require('gulp-typescript');
gulp.task('scripts', function () {
return gulp.src([mySrcDir,'typings/**/*.d.ts'])
.pipe(sourcemaps.init())
.pipe(typeScript(typeScriptProject))
.pipe(concat('main.js'))
.pipe(transform(function (filename) {
return browserify(filename).bundle();
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(ns.outDir))
// Reload, notify...
;
Error: Cannot find module 'C:\\path\\to\\project\\root\\src\\main.js' in 'C:\\path\\to\\project\\root'
When I omit concatenation, the result is the same, except with foobar.js
instead of main.js
where foobar.ts
is one of the input files. 当我省略连接时,结果是相同的,除了使用
foobar.js
而不是main.js
,其中foobar.ts
是输入文件之一。
gulp.task('scripts', function () {
var stream = gulp.src([mySrcDir,'typings/**/*.d.ts'])
.pipe(sourcemaps.init())
.pipe(typeScript(typeScriptProject))
.pipe(concat('main.js'));
var bundleStream = ns.browserify(stream).bundle().on('error', errorHandler);
// and then...
C:\path\to\project\root\_stream_0.js:1
[object Object]
^
ParseError: Unexpected token
You can't pass a vinyl
stream to browserify. 您无法将
vinyl
流传递给browserify。 It only accepts text
or buffer
streams. 它只接受
text
或buffer
流。 The only solution is to transform the input vinyl
stream to a text
stream that browserify can grasp: 唯一的解决方案是将输入的
vinyl
流转换为浏览器可以掌握的text
流:
var gutil = require('gulp-util')
var through = require('through2')
var intoStream = require('into-stream')
// ...
.pipe(through.obj(function(file, encoding, next) {
bundle = browserify(intoStream(file.contents))
this.push(new gutil.File({
path: 'index.js',
contents: bundle.bundle()
}))
next()
}))
take a look at gulp-browserify , it a gulp plugin for browserify. 看看gulp-browserify ,它是browserify的一个gulp插件。
Example: 例:
gulp.src([mySrcDir,'typings/**/*.d.ts'])
.pipe(sourcemaps.init())
.pipe(typeScript(typeScriptProject))
.pipe(concat('main.js'))
.pipe(browserify(options)
.pipe(sourcemaps.write())
.pipe(gulp.dest(ns.outDir))
// Reload, notify...
;
for options you may refer to the link posted above 对于选项,您可以参考上面发布的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.