首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何设置gulp将多个文件捆绑成一个文件?

如何设置gulp将多个文件捆绑成一个文件?
EN

Stack Overflow用户
提问于 2016-03-05 19:56:40
回答 3查看 14.6K关注 0票数 9

这似乎是一个非常简单的问题,但在过去的3个小时里研究了它,发现如果不使用watchify,每次保存新文件时都会很慢。

这是我的目录树:

代码语言:javascript
复制
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,这一点有点夸张。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2016-03-06 03:17:03

首先,您应该监听所需目录中的更改:

代码语言:javascript
复制
watch(['toBundletheseJs/**/*.js'], function () {
        gulp.run('bundle-js');
    });

然后,bundle-js任务应该捆绑您的文件。推荐的方式是gulp-concat

代码语言:javascript
复制
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/'));
});
票数 10
EN

Stack Overflow用户

发布于 2018-06-09 20:46:34

代码语言:javascript
复制
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();
});
票数 1
EN

Stack Overflow用户

发布于 2019-06-28 17:48:10

使用此代码将多个文件捆绑成一个文件。

代码语言:javascript
复制
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
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35813804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档