gulp : browserify 然后连接文件
gulp : browserify then concat files
我需要浏览一个文件,然后将捆绑包与另一个文件连接起来。我尝试了下面的 gulp 代码,但它无法正常工作。
当我在 mymodule.js 和 运行 gulp 中进行更改时,这些更改会出现在捆绑文件中但不会出现在串联文件中,除非我 运行 gulp 秒时间.
这就像 concat 步骤没有等待 bundle 步骤完成并采用先前浏览器化的 bundle。
由于我是 gulp 的初学者,我确定我的 gulp 逻辑有问题...
我的gulp文件是:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
gulp.task('default', function() {
var b = browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
gulp.src([
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
])
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
谢谢
It's like if the concat step is not waiting for the bundle step to finish and take the previously browserified bundle
事实就是如此。 Gulp 异步工作并具有最大并发性,因此除非您告诉它等待某事,否则一切都会立即开始 运行。
您可以将您的任务分成两个任务并提示Gulp一个依赖于另一个:
gulp.task('bundle', function() {
return browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
});
gulp.task('default', ['bundle'], function() {
return gulp.src([
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
])
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
试试这个:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var paths = [
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
];
gulp.task('default', function() {
return browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
});
gulp.task('default2', function() {
return gulp.src(paths)
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
gulp.task('final', ['default', 'default2']);
并且 运行 每次您要创建捆绑包时任务结束。
实际上,您不需要像其他答案中提到的那样拆分您的任务:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var merge = require('merge-stream');
var buffer = require('vinyl-buffer');
gulp.task('default', function () {
var b = browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(buffer())
// remove this line if you don't need 'mymodule-bundle.js' to be saved at all
.pipe(gulp.dest('src'));
return merge(b, gulp.src('bower_components/porthole/src/porthole.min.js'))
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
我需要浏览一个文件,然后将捆绑包与另一个文件连接起来。我尝试了下面的 gulp 代码,但它无法正常工作。
当我在 mymodule.js 和 运行 gulp 中进行更改时,这些更改会出现在捆绑文件中但不会出现在串联文件中,除非我 运行 gulp 秒时间.
这就像 concat 步骤没有等待 bundle 步骤完成并采用先前浏览器化的 bundle。
由于我是 gulp 的初学者,我确定我的 gulp 逻辑有问题...
我的gulp文件是:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
gulp.task('default', function() {
var b = browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
gulp.src([
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
])
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
谢谢
It's like if the concat step is not waiting for the bundle step to finish and take the previously browserified bundle
事实就是如此。 Gulp 异步工作并具有最大并发性,因此除非您告诉它等待某事,否则一切都会立即开始 运行。
您可以将您的任务分成两个任务并提示Gulp一个依赖于另一个:
gulp.task('bundle', function() {
return browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
});
gulp.task('default', ['bundle'], function() {
return gulp.src([
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
])
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
试试这个:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var paths = [
'bower_components/porthole/src/porthole.min.js',
'src/mymodule-bundle.js'
];
gulp.task('default', function() {
return browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(gulp.dest('src'));
});
gulp.task('default2', function() {
return gulp.src(paths)
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});
gulp.task('final', ['default', 'default2']);
并且 运行 每次您要创建捆绑包时任务结束。
实际上,您不需要像其他答案中提到的那样拆分您的任务:
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var merge = require('merge-stream');
var buffer = require('vinyl-buffer');
gulp.task('default', function () {
var b = browserify('src/mymodule.js')
.bundle()
.pipe(source('mymodule-bundle.js'))
.pipe(buffer())
// remove this line if you don't need 'mymodule-bundle.js' to be saved at all
.pipe(gulp.dest('src'));
return merge(b, gulp.src('bower_components/porthole/src/porthole.min.js'))
.pipe(concat('app.js'))
.pipe(gulp.dest('dist'));
});