gulp-concat内容的两倍

Nan*_*ano 1 javascript gulp

这对我来说是一个奇怪的事情,我有一个任务是连接我的.js文件,然后用观察者对它们进行uglify,但concat任务只是每次调用内容的两倍......

这是我的gulpfile:

'use strict';

let gulp        = require('gulp');
let stylus      = require('gulp-stylus');
let sourcemaps  = require('gulp-sourcemaps');
let concat      = require('gulp-concat');
let uglify      = require('gulp-uglify');
let plumber     = require('gulp-plumber');
let bootstrap   = require('bootstrap-styl');
let rupture     = require('rupture');
let copy        = require('gulp-copy2');

/*
  Prepare the paths
*/
let base = './theme';
let themeName = 'own-theme';

let paths = {
  stylus    : `${base}/${themeName}/css`,
  js        : `${base}/${themeName}/js`,
  vendor    : `${base}/${themeName}/js/vendor`
}

/*
  Stylus compile
*/
gulp.task('stylus-compile', () => {
  return gulp.src([`${paths.stylus}/dev/*.styl`, `${paths.stylus}/!**/_*.styl`])
    .pipe(plumber())
    .pipe(stylus({
      use: [bootstrap(), rupture()],
      compress: true
    }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(`${paths.stylus}`));
});

/*
  Get the bootstrap-styl js files and concat/uglify them
*/

gulp.task('bootstrap-build', () => {
  return gulp.src([
    'node_modules/bootstrap-styl/js/transition.js',
    'node_modules/bootstrap-styl/js/alert.js',
    'node_modules/bootstrap-styl/js/button.js',
    'node_modules/bootstrap-styl/js/carousel.js',
    'node_modules/bootstrap-styl/js/collapse.js',
    'node_modules/bootstrap-styl/js/dropdown.js',
    'node_modules/bootstrap-styl/js/modal.js',
    'node_modules/bootstrap-styl/js/tooltip.js',
    'node_modules/bootstrap-styl/js/popover.js',
    'node_modules/bootstrap-styl/js/scrollspy.js',
    'node_modules/bootstrap-styl/js/tab.js',
    'node_modules/bootstrap-styl/js/affix.js'
  ])
  .pipe(sourcemaps.init())
  .pipe(concat('bootstrap.min.js'))
  .pipe(uglify())
  .pipe(sourcemaps.write('./'))
  .pipe(gulp.dest(`${paths.vendor}`));

});

/*
  Get the js assets from NPM
*/
gulp.task('js-copy', () => {
  let dirs = [
    { src: 'node_modules/jquery/dist/jquery.min.js', dest: `${paths.vendor}/jquery.min.js` },
    { src: 'node_modules/sweet-scroll/sweet-scroll.min.js', dest: `${paths.vendor}/sweet-scroll.min.js` }
  ]
    return copy(dirs);
});

/*
  Concat/Uglify the JS files
*/
gulp.task('js-build', () => {
return gulp.src(`${paths.js}/*.js`)
  .pipe(sourcemaps.init())
  .pipe(concat('site.min.js'))
  // .pipe(uglify())
  .pipe(sourcemaps.write('./'))
  .pipe(gulp.dest(`${paths.js}`));
})

/*
  Watch
*/
gulp.task('watch', () => {
  gulp.watch(`${paths.js}/*.js`, ['js-build']);
  gulp.watch(`${paths.stylus}/dev/*.styl`, ['stylus-compile']);
});

gulp.task('default', ['bootstrap-build', 'js-copy', 'watch']);
Run Code Online (Sandbox Code Playgroud)

bootstrap-build无论你调用任务多少次,任务都不会是内容的两倍,但js-build确实如此.

以下是用于连接的测试分隔脚本和结果:

档案1:

(function() {

  console.log("oh!")
  console.log("uh!")

}).call(this);
Run Code Online (Sandbox Code Playgroud)

文件2:

(function() {
  console.log("hey")
}).call(this);
Run Code Online (Sandbox Code Playgroud)

连接文件(呃,在观察者被解雇后重新保存的文件):

(function() {

  console.log("oh!")
  console.log("uh!")

}).call(this);
(function() {

  console.log("oh!")
  console.log("uh!")

}).call(this);
(function() {
  console.log("hey")
}).call(this);
//# sourceMappingURL=site.min.js.map

(function() {
  console.log("hey")
}).call(this);
//# sourceMappingURL=site.min.js.map
Run Code Online (Sandbox Code Playgroud)

在每次重新保存时,连续两次内容......我真的没有遇到问题.任何的想法?

谢谢你的支持.

Sve*_*ung 5

您的bootstrap-build工作原因是因为它将结果bootstrap.min.js放在与源文件不同的文件夹中.

js-build但是,您的任务会连接.js文件path.js夹中的所有文件,并将结果site.min.js放在同一个文件夹中.

这意味着首次运行js-build文件file1.jsfile2.js连接到site.min.js.在第二次运行文件file1.js,file2.jssite.min.js连接到site.min.js.每次运行js-build任务都会site.min.js增长.

您需要做的是排除site.min.js与其他文件的连接:

gulp.task('js-build', () => {
  return gulp.src([
      `${paths.js}/*.js`,
      `!${paths.js}/site.min.js`
    ])
    .pipe(sourcemaps.init())
    .pipe(concat('site.min.js'))
    // .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(`${paths.js}`));
})
Run Code Online (Sandbox Code Playgroud)