> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/gulp-combining-tasks-ok.md).

# \[GULP] Gulp - Combining Tasks (ok)

> Chú ý: Để kết hợp được các task với nhau từ phiên bản 4.x trở đi chúng ta phải
>
> gulp.task('default', gulp.series('css','js'));
>
> Chú ý: Nếu thì sử dụng &#x20;
>
> ```
> gulp.task('default', gulp.series('a', 'b', 'c'));
> ```
>
> Thì sử dụng
>
> ```
> gulp.task('default', gulp.parallel('a', 'b', 'c'));
> ```

C:\xampp\htdocs\gulp\package.json

```
{
  "name": "gulp",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "gulp": "gulp default"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "gulp": "^4.0.2",
    "gulp-livereload": "^4.0.2"
  },
  "devDependencies": {
    "browser-sync": "^2.27.5",
    "gulp-concat": "^2.6.1",
    "gulp-minify-css": "^1.2.4",
    "gulp-sass": "^5.0.0",
    "gulp-uglify": "^3.0.2",
    "node-sass": "^6.0.1",
    "sass": "^1.38.0"
  }
}

```

C:\xampp\htdocs\gulp\gulpfile.js

```
var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
gulp.task('default', function() {
});
gulp.task('js', function(){
   gulp.src('src/scripts/*.js')
   .pipe(concat('script.js'))
   .pipe(uglify())
   .pipe(gulp.dest('build/scripts/'));
});
gulp.task('css', function(){
   gulp.src('src/styles/*.css')
   .pipe(concat('styles.css'))
   .pipe(minify())
   .pipe(gulp.dest('build/styles/'));
});
gulp.task('default', gulp.series('css','js'));
```

```
gulp.task('task-name', function() {
   //do stuff here
});
```

Trong đó “task-name” là tên chuỗi và “function ()” thực hiện nhiệm vụ của bạn. “Gulp.task” đăng ký chức năng như một tác vụ trong tên và chỉ định các phần phụ thuộc vào các tác vụ khác.

### Installing Plugins

```
npm install gulp-minify-css --save-dev
```

To work with “gulp-minify-css plugin”, you need to install another plugin called “gulp-autoprefixer” as shown in the following command −

```
npm install gulp-autoprefixer --save-dev
```

Để nối các tệp CSS, hãy cài đặt gulp-concat như được hiển thị trong lệnh sau:

```
npm install gulp-concat --save-dev
```

After installation of plugins, you need to write dependencies in your configuration file as follows

```
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var concat = require('gulp-concat');
```

### Adding Task to Gulp file

```
gulp.task('styles', function() {
   gulp.src(['src/styles/*.css'])
   .pipe(concat('styles.css'))
   .pipe(autoprefix('last 2 versions'))
   .pipe(minifyCSS())
   .pipe(gulp.dest('build/styles/'));
});
```

Plugin ‘concat’ nối các tệp CSS và plugin ‘autoprefix’ cho biết phiên bản hiện tại và phiên bản trước của tất cả các trình duyệt. Nó thu nhỏ tất cả các tập lệnh CSS từ thư mục src và sao chép vào thư mục xây dựng bằng cách gọi phương thức ‘dest’ với một đối số, đại diện cho thư mục đích.

Tương tự, chúng tôi sẽ sử dụng một plugin khác có tên là ‘gulp-imagemin’ để nén tệp hình ảnh, có thể được cài đặt bằng lệnh sau:

```
npm install gulp-imagemin --save-dev
```

```
var imagemin = require('gulp-imagemin');
```

```
gulp.task('imagemin', function() {
   var img_src = 'src/images/**/*', img_dest = 'build/images';
   gulp.src(img_src)
   .pipe(changed(img_dest))
   .pipe(imagemin())
   .pipe(gulp.dest(img_dest));
});
```

### Combining Multiple Tasks

```
You can run multiple tasks at a time by creating default task in the configuration file as shown in the following code −

gulp.task('default', ['imagemin', 'styles'], function() {

});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/gulp-combining-tasks-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
