简体   繁体   English

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

[英]How to set up gulp to bundle several files into one?

This seems like a very simple question, but spent the last 3 hours researching it, discovering it can be slow on every save on a new file if not using watchify.这似乎是一个非常简单的问题,但花了最后 3 个小时研究它,发现如果不使用 watchify,每次保存新文件时都会很慢。

This is my directory tree:这是我的目录树:

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

Requirements.要求。 On every creation or save of a file within the folder toBundleTheseJs/ I want this file to be rebundled into toBundleJsHere/toBundleTheseJs/文件夹中每次创建或保存文件时,我希望将此文件重新绑定到toBundleJsHere/

What do I need to include in my package.json file?我需要在package.json文件中包含什么?

And whats the minimum I need to write into my gulp file?我需要写入我的 gulp 文件的最低要求是什么?

This should be as fast as possible so think I should be using browserify and watchify.这应该尽可能快,所以我认为我应该使用 browserify 和 watchify。 I want to understand the minimum steps so using package manager like jspm is overkill a this point.我想了解最少的步骤,因此在这一点上使用像 jspm 这样的包管理器是多余的。

thanks谢谢

First you should listen to changes in the desired dir:首先,您应该听取所需目录中的更改:

watch(['toBundletheseJs/**/*.js'], function () {
        gulp.run('bundle-js');
    });

Then the bundle-js task should bundle your files.然后bundle-js任务应该捆绑您的文件。 A recommended way is gulp-concat :推荐的方法是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/'));
});

The right answer is: there is no legit need for concatenating JS files using gulp.正确答案是:使用 gulp 连接 JS 文件没有合法的必要。 Therefore you should never do that.因此,您永远不应该这样做。

Instead, look into proper JS bundlers that will properly concatenate your files organizing them according to some established format, like commonsjs, amd, umd, etc.相反,请查看适当的 JS 打包器,这些打包器会根据某种既定格式(如 commonsjs、amd、umd 等)正确连接您的文件。

Here's a list of more appropriate tools:以下是更合适的工具列表:

Note that my answer is around end of 2020, so if you're reading this in a somewhat distant future keep in mind the javascript community travels fast so that new and better tools may be around.请注意,我的答案是在 2020 年底左右,因此如果您在某个遥远的未来阅读本文,请记住 javascript 社区发展迅速,因此可能会出现新的和更好的工具。

Use this code to bundle several files into one.使用此代码将多个文件捆绑成一个。

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
    });
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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM