gulp-concat twice the content

gulp-concat twice the content

本文关键字:content the twice gulp-concat      更新时间:2023-09-26

这对我来说是一件奇怪的事情,我有一个任务是连接我的.js文件,然后用观察程序将它们丑化,但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']);

无论您调用任务多少次,bootstrap-build任务都不会使内容增加一倍,但js-build会这样做。

以下是要连接的测试分离脚本和结果:

文件 1:

(function() {
  console.log("oh!")
  console.log("uh!")
}).call(this);

文件 2:

(function() {
  console.log("hey")
}).call(this);

合并文件(呃,哦,文件在观察程序被触发后重新保存):

(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

在每次重新保存中,连接的内容都是两倍......我真的不明白这个问题。知道吗?

谢谢。

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

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

这意味着首次运行时js-build文件file1.jsfile2.js将连接成site.min.js。在第二次运行时,文件file1.jsfile2.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}`));
})