This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Common GULP apis
- We’re going to learn some first
gulp
The commonly used API - Because we’re going to use these apis to
gulpfile.js
The configuration is packaged in the file - These apis are the capabilities we get after we install the project dependencies
1. gulp.task()
-
This method is used to create tasks
-
Our configuration file is actually writing tasks one by one
-
This is done by performing these tasks on the command line
-
Grammar:
// gulp.task(' task name ', function () {}) // Create a task to package HTML files gulp.task('heml', function () {// write the operation to package HTML files})Copy the code
- In the future when we execute
html
For this task, you can puthtml
The file is packaged
- In the future when we execute
2. gulp.src()
-
Used to find source files
-
Which is to find the files that we’re packing
-
Grammar:
// gulp.src('./ SRC /pages/*.html')Copy the code
- Which path above is to find
src
Under folderpages
All the suffixes in the folders are.html
The file
- Which path above is to find
3. gulp.pipe()
-
It’s a way to help us do things
-
We also call this the pipe function
-
That’s the pipe function that helps us compress HTML files
-
Grammar:
// gulp.pipe(htmlmin()) // execute the compressed HTML file.Copy the code
- When the above code is executed, it will
html
The file is compressed
- When the above code is executed, it will
4. gulp.dest()
-
It is used to write to files, that is, to which directory we package the files to write to
-
Grammar:
// gulp.dest('./dist/pages')Copy the code
5. gulp.watch()
-
Used to monitor files
-
Once the monitored file changes, the task you specified will be performed
-
Grammar:
Gulp.watch ('./ SRC /pages/*.html', HTML) gulp.watch('./ SRC /pages/*.html', HTML)Copy the code
- when
pages
Any one of themhtml
When the file is changed, it is automatically executedhtml
The task is packaged again
- when
6. gulp.series()
-
Used to perform tasks one by one
-
That is, multiple task names can be passed, and the next task will be executed after the previous one has completed
-
Grammar:
// gulp.series(Task 1, Task 2,...) Gulp.series (HTML, CSS, js)Copy the code
- He’ll do what he’s ready to do
html
Task, and then executecss
Task, and then executejs
task
- He’ll do what he’s ready to do
7. gulp.parallel()
-
Used to execute tasks in parallel
-
You can also pass more than one task name and it will start executing at the same time
-
Grammar:
// gulp.parallel(task 1, task 2,...) Gulp.parallel (HTML, CSS, js)Copy the code
- He’ll get it ready
html
/css
/js
All three tasks start at the same time
- He’ll get it ready
Gulp configuration
- Then we can go
gulpfile.js
It’s configured in there - Make our tasks one by one, and then let
gulp
The tools are here to help us pack
Create a task to package CSS
-
The simplest thing is to remove the whitespace from the CSS file
-
We can’t delete them one by one, nor can we write a regular to replace them
- Because one is too much trouble, and one is that it’s hard to write the re ourselves
-
So we need a third party to do it for us
-
The dependency is downloaded directly from the project
$ npm install --save-dev gulp-cssmin
-
Let’s start with the configuration
- So let’s write one here
gulp 3.x.x
The configuration of the - In writing one
gulp 4.x.x
The configuration of the - Since the configuration of the two versions is different, let’s study both
- As far as you’re concerned, use one, depending on your version of Gulp
- So let’s write one here
-
We just use CSS as an example here
- The other tasks are the same, just using different third-party packages
gulp 3.x.x
Const gulp = require('gulp') // 2. Const cssmin = require('gulp-cssmin') // create a gulp.task(' CSS ', Function () {return gulp. SRC ('. / SRC/CSS / *. CSS ') / / to the compressed file. The pipe (cssmin ()) / / compression. The pipe (gulp. Dest ('. '/ dist/CSS)) / / Write the result of compression to the dist directory})Copy the code
- This task is finished, we go to the command line to execute the task can be
$ gulp css
- Just give me a second. I’ll just
src/css/*.css
All the files are packed and put indist/css
Under this directory
gulp 4.x.x
gulp4
You don’t need it in theretask
I’m creating the task, so I can just write it as a function
Const gulp = require('gulp') // 2. Const cssmin = require('gulp-cssmin') // A handler for packaging CSS files const cssHandler = () => {return Gulp.src ('./ SRC/CSS /*.css') // Find the CSS file to compress.pipe(cssmin()) // compress.pipe(gulp.dest('./dist/ CSS ')) // write the result to dist directory } // Since this is a function, not a task, we need to export to use module.exports. CSS = cssHandlerCopy the code
- And then you can go to the command line and execute something called
css
The task $ gulp css
Create a monitoring task
- We create a monitor file file
- When files change, task repackaging is triggered
- We also get a share
gulp 3.x.x
和gulp 4.x.x
For example writing
gulp 3.x.x
gulp.task('watch', function () {
gulp.watch('./src/css/*.css', gulp.series('css'))
})
Copy the code
- When you execute this
watch
After the task - So as long as
./src/css/*.css
All of thecss
If the file changes, it will be executed againcss
This task
gulp 4.x.x
gulp4
I’m going to write it as a function
Const watchHandler = () => {gulp.watch('./ SRC/CSS /*.css', CSS)} // Export this task module.exports.watch = watchHandlerCopy the code