gulp
Multiple developers work together to develop a project. Each developer is responsible for different modules.
This results in a complete project consisting of many "code sections";
Some pre-processors such as less and sass are used to reduce the maintenance cost of CSS, and these pre-processors need to be parsed eventually.
Merging css, javascript and compressing html, css, javascript and images can speed up the opening of web pages and improve the performance.
This series of tasks is almost impossible to accomplish by hand, and can be easily accomplished by building tools.
The so-called construction tools refer to software tools that can help us achieve a series of tasks such as merging, compression, validation, preprocessing and so on through simple configuration.
Common building tools include: Grunt, Gulp, F.I.S. (Baidu), webpack
1. Introduction to gulp
- gulp is written in nodejs.
- gulp is a flow-based automated build tool
- After the website development is completed, we need to do project construction before we can go online.
- Project construction:
- Code compression. html css js
- Code obfuscation
- Document merging
- Other automation work. sass to css.
- That's what gulp does.
2. gulp installation
- First, you need to install gulp globally. If you have installed gulp globally before, you can omit this step.
npm install gulp -g
The globally installed plug-ins can be used anywhere.
Locally installed plug-ins can only be used in current projects.
- Local installation is also required, in the project directory.
--
3. gulp usage
- Create a gulpfile.js file in the project directory.
- Write the build code in this file.
- Introduce the local gulp module first
var gulp = require('gulp');
3.1 Create Tasks
- gulp executes each build in the form of a task.
- Tasks can be created by calling the task method of gulp objects
Parametric 1: Task name
Parametric 2: Callback function, what to do when performing the task.
gulp.task("testTask",function(){
console.log();
});
- At this point, we create a task testTask, which does the callback function.
3.2 Task Implementation
- How to perform this gulp task?
- Open the cmd tool and switch the working path to the project directory
- Use gulp task name in command line window to execute specified tasks.
gulp testTask
- At this point, a task called testTask will be executed.
4. Compressing CSS gulp-cssmin
- You need to create a task to compress css.
var cssmin = require("gulp-cssmin");
gulp.task("yscss",function () {
gulp.src("./src/assets/css/reset.css")
.pipe(cssmin())
.pipe(gulp.dest("./dist/assets/css"));
});
5. File monitoring watch
- gulp object provides a watch method
- The function of this method is to monitor the changes of specified files.
- Once changed, perform the assigned tasks
gulp.task("watchCss",function () {
gulp.watch("./src/assets/css/reset.css",["yscss"]);
})
- The second parameter of the watch method can also be a callback. When the file changes, the callback is executed.
5.gulp-uglify compression js
- Compressing obfuscated js code requires gulp-uglify plug-in support. Install the plug-in using npm.
var gulp = require("gulp");
var uglify = require("gulp-uglify");
gulp.task("ysjs",function(){
gulp.src("./js/app.js")
.pipe(uglify())
.pipe(gulp.dest("./dist/js"));
});
6.gulp-concat merge file
- File merging requires the use of gulp-concat plug-in support. Install the plug-in using npm.
gulp.task("concatFile",function(){
gulp.src(["./src/js/app.js","./src/js/demo.js"])
.pipe(concat("all.js"))//Merge the files to be processed, and the name of the new file after merging is all.js
.pipe(uglify())//Re-confusion
.pipe(gulp.dest("./dist/js"))
})
7.gulp-htmlmin compresses html files
- Compressing html code requires the use of gulp-htmlmin plug-in support. Install the plug-in using npm.
gulp.task("yshtml",function(){
gulp.src("./src/index.html")
.pipe(htmlmin({
collapseWhitespace:true,
removeComments:true
}))
.pipe(gulp.dest("./dist"))
});
8.gulp-sass converts sass to css
- You need to use gulp-sass plug-in support and install it using npm
- less is the same.
gulp.task("sass2css",function(){
gulp.src("./src/css/index.scss")
.pipe(sass())
.pipe(cssmin())
.pipe(gulp.dest("./dist/css"))
})
9. Call the src method to specify the path of the file to be processed. The path can use wildcards.
- . / src/.js represents the. js file that matches the src direct directory.
- JS files in any directory under. / src/*/.js src directory.
gulp.task("hejss",function(){
gulp.src("./src/**/*.js")
.pipe(concat("big.js"))
.pipe(gulp.dest("./dist"))
})
gulp.task("yasuoCss",function(){
gulp.src("./src/assets/css/*.css",
{base:"./src"}) //The base here specifies the basic road strength (with folders)
.pipe(cssmin())
.pipe(gulp.dest("./dist"))
})
Five methods for daily work of gulp objects
- The gulp object itself has only five methods.
- The task method creates a task.
- src sets the path of the file to be processed.
- pipe Setting the Gate in Pipeline
- dest method. Store the path of the file after processing.
- watch method. Monitor. Automatically execute tasks once files have changed.