Detailed explanation of gulp installation and packaging and merging
This article mainly introduces you to the tutorial on how to implement a packaging and merging method in gulp, and shares the solution to the order when gulp packages js/css and merges them into one file. The article introduces it in detail through the example code. It needs to be Friends can refer to it, and follow the editor to learn together. Hope it helps everyone.
Preface
gulp is a tool for building code in the front-end development process. It is a tool for building automation projects; it can not only optimize website resources, but also eliminate many repetitive tasks during the development process. Tasks can be completed automatically using the right tools; using her, we can not only happily write code, but also greatly improve our work efficiency.
Installation, packaging and merging
1. Install node.js Download address: http://nodejs.cn/
Open the node.js command line and enter: node - v , if there is a version number, it is installed correctly.
2. Install Taobao image: Command line input:
npm install -g cnpm --registry=http://registry.npm.taobao.org
Purpose: Make downloading faster.
3. Install gulp globally
cnpm install --global gulp
4. Create a directory, open the F drive, and create a gulp folder.
Command line input:
f: cd gulp
5. Install local gulp
cnpm install --save-dev gulp
6. Create package.json file
cnpm init
Just enter all the way
7. Open this gulp directory in a web editor, such as hbuilder and webstorm.
Create the gulpfile.js file in the gulp directory, the entry point for gulp running
8. Determine what kind of packaging and compression, html, js, css, img
9.js packaging
var gulp = require('gulp'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); gulp.task('default',function(){ gulp.src('js/*.js') // 路径问题:gulpfile.js为路径的起点。此路径表示js文件下的所有js文件。 .pipe(concat('all.js')) //合并成的js文件名称 .pipe(uglify()) //压缩 .pipe(gulp.dest('build')); //打包压缩在build目录下。 });
10. Run; node.js input
gulp
will report an error, prompting the gulp-concat component Not installed. Start the installation: cnpm install gulp-concat --save-dev
Run again: gulp
reports an error again, indicating that the gulp-uglify component is not installed. Start the installation: cnpm install gulp-uglify --save-dev
Run again: gulp
. . . . . . . . . . . . . . .
After success,
You will see finished 'default' here. 'default' is the default entry for starting the gulp.task task. If you create multiple task tasks and modify the task name such as:
var gulp = require('gulp'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); gulp.task('default',function(){ gulp.src('js/*.js') // 路径问题:gulpfile.js为路径的起点。此路径表示js文件下的所有js文件。 .pipe(concat('all.js')) //合并成的js文件名称 .pipe(uglify()) //压缩 .pipe(gulp.dest('build')); //打包压缩在build目录下。 }) //css 打包压缩 var autoprefix = require('gulp-autoprefixer'); var minifyCSS = require('gulp-minify-css'); gulp.task('style', function() { //task 任务名称为style gulp.src('.css/*.css') .pipe(concat('styles.css')) .pipe(autoprefix('last 2 versions')) .pipe(minifyCSS()) .pipe(gulp.dest('styles')); });
Rerun: gulp
Result:
You will find that only The default task task was run. Because this is the only default gulp execution entry.
Modify as follows
var gulp = require('gulp'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); gulp.task('js',function(){ gulp.src('js/*.js') // 路径问题:gulpfile.js为路径的起点。此路径表示js文件下的所有js文件。 .pipe(concat('all.js')) //合并成的js文件名称 .pipe(uglify()) //压缩 .pipe(gulp.dest('build')); //打包压缩在build目录下。 }) //css 打包压缩 var autoprefix = require('gulp-autoprefixer'); var minifyCSS = require('gulp-minify-css'); gulp.task('style', function() { //task 任务名称为style gulp.src('.css/*.css') .pipe(concat('styles.css')) .pipe(autoprefix('last 2 versions')) .pipe(minifyCSS()) .pipe(gulp.dest('styles')); }); <br>gulp.task('default',function(){ gulp.run(['js','style']); //这里开始执行多个task任务 });
If you encounter any component that is not installed, I think you should know how to operate it.
11. Image compression
var imagemin = require('gulp-imagemin'); gulp.task('img', function() { return gulp.src('imgs/*.png') .pipe(imagemin()) .pipe(gulp.dest('miniImg')); });
12.html compression
var htmlmin = require('gulp-htmlmin'); gulp.task('html', function() { return gulp.src('../*.html') .pipe(htmlmin({collapseWhitespace: true})) .pipe(gulp.dest('../')); });
13. Modify the path problem yourself
Gulp merges js/css into one file when packaging it When solving the order
1, you can use insert gulp-order.
2. You can write it like this:
return gulp.src(['js/common.js','js/**/*.js']) .pipe(concat('build.js'))//合成到一个js .pipe(gulp.dest(buildBasePath+'js'))//输出到js目录 .pipe(uglify())//压缩js到一行 .pipe(concat('build.min.js'))//压缩后的js .pipe(gulp.dest(buildBasePath+'js'));//输出到js目录
Related recommendations:
Gulp implements static web page modularization example sharing
Detailed explanation of simple gulp packaging in nodejs
How to use gulp to achieve file compression and browser hot loading
The above is the detailed content of Detailed explanation of gulp installation and packaging and merging. For more information, please follow other related articles on the PHP Chinese website!

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
