gpt4 book ai didi

javascript - Gulp 连接两个任务中的依赖关系

转载 作者:行者123 更新时间:2023-11-27 23:04:25 25 4
gpt4 key购买 nike

我有这个项目结构。

enter image description here

我想这样做,以便当我运行 inject-bowergulp 任务 时,它将获取 Bower 文件夹并注入(inject)所有必需的文件。然后,当我执行 inject-app 时,它将获取所有脚本并按原样注入(inject)它。

我将这两个任务放入一个并发任务流中,如下所示:

gulp.task('inject', ['inject-bower', 'inject-app'], function() {
console.log('Injection done.');
})

但是,它似乎只注入(inject)了 Bower 文件夹。但是,当我依次运行 gulpject-bower 和 gulpinject-app 时......它起作用了。

这是我的 gulpfile:

var gulp = require('gulp'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var cache = require('gulp-cache');
var minifycss = require('gulp-minify-css');
var sass = require('gulp-sass');
var wiredep = require('wiredep').stream;
var inject = require('gulp-inject');

var runSequence = require('run-sequence').use(gulp);
var del = require('del');

gulp.task('inject', ['inject-bower', 'inject-app'], function() {
console.log('Injection done.');
})

gulp.task('inject-bower', ['clean'], function() {
gulp.src('./index.html')
.pipe(wiredep({}))
.pipe(gulp.dest('./src'));
});

gulp.task('clean', function() {
return del('src/index.html');
});

gulp.task('inject-app', function () {
gulp.src('./src/index.html')
.pipe(inject(
gulp.src(['./src/js/client/*.js'], {read: true})
))
.pipe(gulp.dest('./src'));
});

// For production/dev
gulp.task('styles', function(){
gulp.src(['src/stylesheets/*.scss'])
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(sass())
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('src/stylesheets/'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('src/stylesheets/'))
});

// For production.
gulp.task('build-scripts', function(){
return gulp.src('src/js/client/**/**/*.js')
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(concat('bundle.js'))
.pipe(gulp.dest('src/js/'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('src/js/'))
});

gulp.task('default', ['inject'], function(){
//gulp.watch("src/stylesheets/*.scss", ['styles']);
//gulp.watch("src/js/client/**/**/*.js", ['scripts']);
gulp.watch("*.html", ['inject']);
});

这是控制台输出:

/usr/local/bin/node /usr/local/lib/node_modules/gulp/bin/gulp.js --color --gulpfile /Users/dan/Projects/AngularJSApp/gulpfile.js inject [12:21:53] Using gulpfile ~/Projects/AngularJSApp/gulpfile.js [12:21:53] Starting 'clean'... [12:21:53] Starting 'inject-app'... [12:21:53] Finished 'inject-app' after 18 ms [12:21:53] Finished 'clean' after 40 ms [12:21:53] Starting 'inject-bower'... [12:21:53] Finished 'inject-bower' after 1.89 ms [12:21:53] Starting 'inject'... Injection done. [12:21:53] Finished 'inject' after 19 μs [12:21:53] gulp-inject 1 files into index.html.

Process finished with exit code 0

完成后,这是我在 src/index.html 中留下的内容:

<!DOCTYPE html ng-app="chat">
<html>
<head>
<title>Hello Chat</title>

<!-- bower:css -->
<link rel="stylesheet" href="bower_components/animate.css/animate.css" />
<!-- endbower -->

<!-- inject:css -->
<!-- endinject -->
</head>
<body>
<h1>Ready to play?</h1>

<section>
Hey man.

<div ng-controller="ChatCtrl"></div>
</section>

<!-- bower:js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/moment/moment.js"></script>
<!-- endbower -->

<!-- inject:js -->
<!-- endinject -->
</body>
</html>

我做错了什么?

最佳答案

我认为您需要在“inject-app”任务中指定对“clean”任务的依赖关系,如下所示。

gulp.task('inject-app',  ['clean'], function () { ...

如果没有这个,您会遇到在“inject-app”和“inject-bower”之间运行“clean”的问题。它在您的日志输出中:

[12:21:53] Starting 'clean'... 
[12:21:53] Starting 'inject-app'...
[12:21:53] Finished 'inject-app' after 18 ms
!!! [12:21:53] Finished 'clean' after 40 ms !!!
[12:21:53] Starting 'inject-bower'...

我认为这是您应用程序中问题的根源。

<小时/>

作为旁注,Gulp 4 将引入“gulp.series”和“gulp.parallel”依赖项,它们允许明确说明您希望看到的依赖项运行的哪种行为。描述here .

关于javascript - Gulp 连接两个任务中的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36725325/

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