As we all know, Gulp.js is an automated build tool that developers can use to automatically perform common tasks during project development. The following article mainly introduces you to the relevant information about Gulp's implementation of static web page modularization. The article introduces it in detail through sample code. Friends in need can refer to it.
Preface
In the process of developing purely static pages, it is inevitable that you will encounter some embarrassing problems. For example: the entire set of code has 50 pages, of which 40 pages have the same top and bottom modules. Then we copied the same two pieces of code 40 times (the most uncomfortable method). Then, the problem was solved. Then, after looking at it a few times, the product manager suddenly said that a certain part of the top part needed to be redesigned. . . Suddenly I feel so embarrassed~~ (My heart is full of horses galloping~), what next? Then I look forward to the next galloping of thousands of horses! ! !
Although there are many solutions to similar problems, if it is purely front-end and does not require various frameworks, a more reliable solution than iframe is to use a construction tool like gulp to complete it. Although there may be some minor flaws in the experience (every time you want to preview a file after modifying it, you need to gulp it first), it is not unbearable. After all, what we want is to solve 40 pages by just changing a certain public module.
Introduction to gulp
gulp is an automated build tool. In developed projects, gulp can be used to automatically build the project, greatly improving work efficiency.
Installing gulp
Before installing gulp, you must first confirm that node.js has been installed correctly, and then install gulp in the project root directory:
$ npm install gulp
Example development environment configuration:
Windows: Build the project in iis to facilitate previewing in the browser; use the webstorm editor (Once you get used to it, it feels much easier to use than Notepad, and you can also ignore certain folders).
MacOs: Build the project in apache to facilitate previewing in the browser; use the webstorm editor (get used to it, it feels more useful than sublime, and you can ignore certain folders).
Required plug-in:
gulp-file-include
Skill description:
Put the HTML code that needs to be modularized into a separate HTML file. For example: head.html
Then use it where you need to use it: @@include('./head.html')
Customize the file path~~
Finally Configure gulp and execute
Use Demo:
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"> <title></title> <link rel="stylesheet" href="css/comm.css?v=1.13" rel="external nofollow" > <link rel="stylesheet" href="css/template.css" rel="external nofollow" > <link rel="stylesheet" href="css/news.css" rel="external nofollow" > </head> <body> @@include('../Layout/head.html') <p class="news"> </p> @@include('../Layout/foot.html') </body>
gulp:
var gulp = require('gulp'), fileinclude = require('gulp-file-include'); gulp.task('prew', function () { gulp.src(['*.html','pages/*.html', '!node_modules/**/*.html']) .pipe(fileinclude({ prefix: '@@', basepath: '@file' })) .pipe(gulp.dest('prew')); gulp.src(['**/**.js', '!node_modules/**/*']).pipe(gulp.dest('prew/')); gulp.src(['**/**.css', '!node_modules/**/*']).pipe(gulp.dest('prew/')); gulp.src(['**/*.jpg', '**/*.jpge', '**/*.png', '**/*.gif', '**/*.bmp', '!prew/**/*', '!node_modules/**/*']).pipe(gulp.dest('prew/')); }); gulp.task('watch',function () { gulp.watch(['pages/*.html','css/*.css','img/**/*','!prew/**/*.html','!prew/**/*.css','!prew/img/**/*'], ['prew']); })
Skill introduction:
Execute gulp prew
will copy the file to the prew directory , and generate the corresponding complete html file. (Note: Here the files in the pages folder are placed directly in the prew root directory, not prew/pages. Change the configuration as needed)
Execute gulp watch
Afterwards, gulp will establish a listening process. Every time a file is modified during development, gulp will automatically execute prew, so that there is no need to manually execute it every time. gulp prew
And then Go refresh the browser. (This is a practical skill)
Finally:
The key point of this set of skills is not how to use gulp, but how to divide it module. In addition to html code, each module can also have js, css code or code that introduces js and css files, so that it can be more modular.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to use node.js and other technologies to implement the login and registration function?
How to get the value of SessionStorage using JS
Issues about compatible browsers in JS
Configuration steps about Axios (detailed tutorial)
Detailed interpretation of modular organization in Vuex
How to implement chat function using nodejs?
The above is the detailed content of How to implement modularization of static web pages using Gulp?. For more information, please follow other related articles on the PHP Chinese website!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use