7

我正在为我的应用程序创建 3 个缩小的捆绑包。我有 2 个任务来执行此操作,缩小和捆绑。Minify 依赖于 bundle。如果我运行 minify,这两个任务都运行没有错误。捆绑包已创建,但缩小的文件未创建。如果我删除了对 bundle 的依赖,我可以自己运行 minify 并成功创建缩小文件。这使我相信当缩小任务触发时文件可能正在使用中(因为捆绑尚未完成?)。如何让它等到文件完全准备好?我可以通过流吗?或者也许将这些组合成一个任务?它们目前不是单个任务的原因是因为它们每个包输出 2 个文件(一个未缩小的包和一个缩小的包)。

var outFolder = __dirname + '\\Scripts\\dist';
var appBundles = [
    { scripts: ['Scripts/Common/**/*.js'], output: 'eStore.common.js' },
    { scripts: ['Scripts/Checkout/**/*.js'], output: 'eStore.checkout.js' },
    { scripts: ['Scripts/ProductDetail/**/*.js'], output: 'eStore.product.js' }
];

gulp.task('bundle', bundle);
gulp.task('minify', ['bundle'], minify);  // this one doesn't work
gulp.task('minifyOnly', minify);          // this one works

function bundle() {
    appBundles.forEach(function (appBundle) {
        gulp.src(appBundle.scripts)
            .pipe(concat(appBundle.output))
            .pipe(sourcemaps.init())
            .pipe(sourcemaps.write(outFolder + '\\maps'))
            .pipe(gulp.dest(outFolder))
            .on('error', errorHandler);
    });
}

function minify() {
    appBundles.forEach(function(appBundle) {
        var bundleSrc = outFolder + '\\' + appBundle.output;
        gulp.src(bundleSrc)
            .pipe(rename({ extname: '.min.js' }))
            .pipe(uglify())
            .pipe(gulp.dest(outFolder))
            .on('error', errorHandler);
    });
}
4

1 回答 1

3

让 minify 任务使用与 bundle 任务相同的源文件。'concat' 将用于这两个任务。这种方式 minify 不依赖于 bundle 任务的输出。

function minify() {
    appBundles.forEach(function (appBundle) {
        console.log('Creating minified bundle for: ' + appBundle.output);
        gulp.src(appBundle.scripts)
            .pipe(concat(appBundle.output))
            .pipe(rename({ extname: '.min.js' }))
            .pipe(uglify())
            .pipe(gulp.dest(outFolder))
            .on('error', errorHandler);
    });
}

function bundle() {
    appBundles.forEach(function (appBundle) {
        console.log('Creating bundle and sourcemaps: ' + appBundle.output);
        gulp.src(appBundle.scripts)
            .pipe(concat(appBundle.output))
            .pipe(sourcemaps.init())
            .pipe(sourcemaps.write(outFolder + '\\maps'))
            .pipe(gulp.dest(outFolder))
            .on('error', errorHandler);
    });
}
于 2015-09-18T18:24:02.620 回答