这似乎是一个非常简单的问题,但在过去的3个小时里研究了它,发现如果不使用watchify,每次保存新文件时都会很慢。
这是我的目录树:
gulpfile.js
package.json
www/
default.htm
<script src="toBundleJsHere/file123.js"></script>
toBundletheseJs/
componentX/
file1.js
componentY/
file2.js
componentZ/
file3.js
toPutBundledJsHere/
file123.js
需求。每次创建或保存文件夹toBundleTheseJs/
中的文件时,我都希望将此文件重新捆绑到toBundleJsHere/
中
我需要在package.json
文件中包含什么内容?
我至少需要在我的gulp文件中写什么?
这应该是尽可能快的,所以我想我应该使用browserify和watchify。我想了解最少的步骤,所以使用包管理器,如jspm,这一点有点夸张。
谢谢
发布于 2016-03-06 03:17:03
首先,您应该监听所需目录中的更改:
watch(['toBundletheseJs/**/*.js'], function () {
gulp.run('bundle-js');
});
然后,bundle-js
任务应该捆绑您的文件。推荐的方式是gulp-concat
var concat = require('gulp-concat');
var gulp = require('gulp');
gulp.task('bundle-js', function() {
return gulp.src('toBundletheseJs/**/*.js')
.pipe(concat('file123.js'))
.pipe(gulp.dest('./toPutBundledJsHere/'));
});
发布于 2018-06-09 20:46:34
var gulp = require('gulp');
var concat = require('gulp-concat');
gulp.task('js', function (done) {
// array of all the js paths you want to bundle.
var scriptSources = ['./node_modules/idb/lib/idb.js', 'js/**/*.js'];
gulp.src(scriptSources)
// name of the new file all your js files are to be bundled to.
.pipe(concat('all.js'))
// the destination where the new bundled file is going to be saved to.
.pipe(gulp.dest('dist/js'));
done();
});
发布于 2019-06-28 17:48:10
使用此代码将多个文件捆绑成一个文件。
gulp.task('scripts', function() {
return gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js']) //files separated by comma
.pipe(concat('script.js')) //resultant file name
.pipe(gulp.dest('./dist/')); //Destination where file to be exported
});
https://stackoverflow.com/questions/35813804
复制相似问题