gpt4 book ai didi

javascript - gulp-concat 内容的两倍

转载 作者:行者123 更新时间:2023-11-30 08:31:49 24 4
gpt4 key购买 nike

这对我来说很奇怪,我有一个任务是连接我的 .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']);

无论您调用任务多少次,bootstrap-build 任务的内容都不会翻倍,但 js-build 会。

这里是要连接的测试分离脚本和结果:

文件 1:

(function() {

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

}).call(this);

文件 2:

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

Concated 文件(呃,哦文件在 watcher 被解雇后重新保存):

(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

在每次重新保存时,concat 内容的两倍......我真的不明白这个问题。任何想法?

提前致谢。

最佳答案

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

然而,您的 js-build 任务将所有 .js 文件连接到您的 path.js 文件夹中,并放置生成的 站点。 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}`));
})

关于javascript - gulp-concat 内容的两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36782807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com