gpt4 book ai didi

javascript - gulpfile 中任务的排序

转载 作者:行者123 更新时间:2023-11-28 04:44:32 29 4
gpt4 key购买 nike

我想通过 Gulp 在我的项目中实现非常简单的任务链:

  1. 复制所有文件;
  2. 用值替换一些占位符;
  3. 缩小一些文件;

为了这些目的,我创建了 gulpfile 来完成这样的主要任务:

gulp.task(tasks.build, [
tasks.simplyCopy,
tasks.minifyXml,
tasks.minifyJs,
tasks.subst]);

这是一个非常简单且可以 self 描述的。

下面我写了完整的 gulpfile.js:

var gulp       = require('gulp');
var prettyData = require('gulp-pretty-data');
var uglify = require('gulp-uglify');
var renvy = require('gulp-renvy');

var tasks = {
simplyCopy: "simply-copy",
minifyXml: "minify-xml",
minifyJs: "minify-js",
subst: "renvy-subst",
build: "build"
};

// Collection of tasks
gulp.task(tasks.build, [tasks.simplyCopy, tasks.minifyXml, tasks.minifyJs,
tasks.subst]);

// By this task sources simply copy to the destination
var destination = 'dist/';
gulp.task(tasks.simplyCopy, function () {
gulp.src(['Source/**/*.*', '!Source/www/res/strings/*.*'], {base:
'Source/www'})
.pipe(gulp.dest(destination));
});

var stringsDestPath = 'dist/res/strings/';
var stringSrcPath = 'Source/www/res/strings/';

// By this task some xml files minify
gulp.task(tasks.minifyXml, [tasks.simplyCopy], function() {
gulp.src(stringSrcPath + '*.xml')
.pipe(prettyData({
type: 'minify',
preserveComments: true,
extensions: {
'xlf': 'xml',
'svg': 'xml'
}
}))
.pipe(gulp.dest(stringsDestPath))
});

var placeholder = {
'%version%': {'prod':'010.00', 'dev':'010.00'}
};


// By this task in some files placeholders replaces with value
gulp.task(tasks.subst, [tasks.minifyXml, tasks.minifyJs], function(){
return gulp.src(stringsDestPath + '*.*')
.pipe(renvy(placeholder, 'dev'))
.pipe(gulp.dest(stringsDestPath));
});


// By this task some js files minify
gulp.task(tasks.minifyJs, [tasks.simplyCopy], function () {
gulp.src(stringSrcPath + '*.js')
.pipe(uglify())
.pipe(gulp.dest(stringsDestPath))
});

但我有这样意想不到的行为:

占位符的替换没有发生,但它会执行。

[16:29:51] Using gulpfile C:\PDDirectory\Workspace\src\some_workbench\User_Part\gulpfile.js
[16:29:51] Starting 'simply-copy'...
[16:29:51] Finished 'simply-copy' after 17 ms
[16:29:51] Starting 'minify-xml'...
[16:29:51] Finished 'minify-xml' after 7.49 ms
[16:29:51] Starting 'minify-js'...
[16:29:51] Finished 'minify-js' after 5.84 ms
[16:29:51] Starting 'renvy-subst'...
[16:29:51] Finished 'renvy-subst' after 28 ms
[16:29:51] Starting 'build'...
[16:29:51] Finished 'build' after 5.66 ?s
单独执行的任务 tasks.subst 工作正常,但在与其他任务的链中,我只看到执行复制和缩小的结果。为什么会这样?

最佳答案

地点 tasks.subst 具有 tasks.build 的唯一依赖项

gulp.task(tasks.build, [tasks.subst]);

由于 tasks.subst 需要所有其他任务,因此排序应该正确,添加所有其他任务可能会导致排序问题。

来自gulp.task documentation :

Note: Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.

为了确保任务依赖关系在执行之前得到满足,Gulp 需要每个任务返回 stream或 promise ,或调用task function callback parameter .

在您的情况下,以下任务应该只返回流:

  • tasks.minifyXml
  • tasks.minifyJs
  • tasks.simplyCopy

例如:

gulp.task(tasks.minifyXml, [tasks.simplyCopy], function(done) {
// just return the task stream here
return gulp.src(stringSrcPath + '*.xml')
.pipe(prettyData({
// ...
}))
.pipe(gulp.dest(stringsDestPath));
});

或者在无法返回流时使用回调:

gulp.task('somename', function(done) {

// async function which does not return a stream like other gulp functions
getFilesAsync(function(err, res) {
// pass any errors to the callback
if (err) return done(err);

var stream = gulp.src(res)
.pipe(minify())
.pipe(gulp.dest('build'))
.on('end', done); // use the callback when it's done
});
});

关于javascript - gulpfile 中任务的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502069/

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