如何使用Gulp将多个jQuery文件合并成一个?

如何使用Gulp将多个jQuery文件合并成一个?,gulp,Gulp,我只想加载一个使用jQuery代码的JS文件,但我对如何最好地加载感到困惑。我担心的是,为了解决在$document.readyfunction{}下加载所有脚本的问题,我会做一些像下面这样草率的事情 其中initialization.js和end.js用于包装document.ready函数,我知道lol,因此询问 有更好的方法吗?编写一个gulp文件,让我们称之为“jquery noconflict.js” var through = require('through2'); var gut

我只想加载一个使用jQuery代码的JS文件,但我对如何最好地加载感到困惑。我担心的是,为了解决在$document.readyfunction{}下加载所有脚本的问题,我会做一些像下面这样草率的事情

其中initialization.js和end.js用于包装document.ready函数,我知道lol,因此询问


有更好的方法吗?

编写一个gulp文件,让我们称之为“jquery noconflict.js”

var through = require('through2');
var gutil = require('gulp-util');
var fs = require('fs');

module.exports = function(){
  var stream = through.obj(function(file, enc, cb) {
    if (file.isStream()) {
      this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
      return cb();
    }

    if (file.isBuffer()) {
        var contents = file.contents.toString();
        file.contents = Buffer.concat([new Buffer('jQuery(document).ready(function(){'), file.contents, new Buffer('})')]);
    }

    cb(null, file);
  }, function(){

  })

  return stream;
};
您可能需要“通过2安装npm”

现在在您的gulpfile.js中

var gulp = require('gulp');
var concat = require('gulp-concat');
var jquery = require('./jquery-noconflict');

gulp.task('compile-js', function(){
    gulp.src('./stuff.js')
        .pipe(concat('script.js'))
        .pipe(jquery())
        .pipe(gulp.dest('./public/javascripts/'))
})
var gulp = require('gulp');
var concat = require('gulp-concat');
var jquery = require('./jquery-noconflict');

gulp.task('compile-js', function(){
    gulp.src('./stuff.js')
        .pipe(concat('script.js'))
        .pipe(jquery())
        .pipe(gulp.dest('./public/javascripts/'))
})